SQL Server Machine Learning Services (ML Services) was originally released as SQL Server R Services with SQL Server 2016.
Since then, Microsoft added support for Python in SQL Server 2017 and renamed the product accordingly.
With SQL Server 2019, Microsoft has added Java support as well and has opened the door to other languages too.
My goals in this talk:
Machine Learning Services brings ML code to the data, reducing data movement and extending functionality inside SQL Server.
We can use Resource Governor to limit CPU and memory utilization. See http://CSmore.info/on/mlservices for more information on this.
These kinds of scenarios work really well with SQL Server ML Services:
PREDICT
operator.Avoid these scenarios:
Microsoft's Azure SQL Edge is a version of SQL Server intended for IoT scenarios. It includes support for ONNX, the Open Neural Network Exchange. This lets you perform native scoring of neural networks, but SQL Server 2019 does not currently support native prediction via ONNX.
Once you have enabled external scripts, be sure to restart the Launchpad service. To be safe, you might want to restart the SQL Server service as well.
There are several techniques for installing packages in R and Python, but we will cover the three best approaches.
The sqlmlutils
package allows you to install R or Python code remotely. This is the best way to install packages for SQL Server 2019 (R or Python) or 2017 (R only).
If you have administrative access to the machine running SQL Server, you can open up the R console and run install.packages()
in the console. Similarly, you can run pip install
from the command line for Python.
By default, the R console is installed in the MSSQL{##}.{MSSQLSERVER}\R_SERVICES\bin
folder, and pip.exe is installed in the MSSQL{##}.{MSSQLSERVER}\PYTHON_SERVICES\Scripts\
folder.
The CREATE EXTERNAL LIBRARY
requires GRANT ALTER ANY EXTERNAL LIBRARY
and allows you to install a package from a zip file.
CREATE EXTERNAL LIBRARY
: cases when sqlmlutils fails to load a custom package.SQL Server has a built-in stored procedure to run R and Python code: sp_execute_external_script
. It functions similarly to sp_executesql
, which we use to run dynamic SQL.
Our organization unraveled a case of expense report fraud among several employees.
Employees were allowed to submit expense reports of up to $40 without a receipt, but needed to include a receipt for everything above. Several employees began submitting fraudulent claims under that amount.
Management would like to know how much we believe employees took above and beyond the expected amounts.
We now want to apply machine learning techniques to the problem, so we will write R and Python code to build models which will help us generate expected amounts.
With RStudio and Rtools installed, go to File -> New Project -> New Directory -> R Package.
After adding in the code, we will build the package.
File -> New Project
Just as with packages from repositories, we can use sqlmlutils to deploy our custom code.
This is also straightforward.
SQL Server Machine Learning Services offers the ability to operationalize R or Python effectively and can serve as a nice way to bring machine learning models to production.
To learn more, go here:
https://CSmore.info/on/mlservicesdev
And for help, contact me:
feasel@catallaxyservices.com | @feaselkl
Catallaxy Services consulting:
https://CSmore.info/on/contact