Predixion SQL CLR Stored Procedures Predixion Insight In-Database Scoring Overview Starting with version Predixion Insight 3.0, the Machine Learning Semantic Model ™ (MLSM) allows defining and executing complex predictive workflows on top of large amounts of data. The Deployment Guide below is a step by step procedure intended for database administrators that want to enable Predixion in-database scoring inside Microsoft SQL Server. These workflows can be executed by Predixion Insight server fetching the data, operating on that data, and then pushing the results to a destination. If the data source and destination are the same (for example, tables in the same SQL Server database or HDFS files on the same Hadoop cluster), then it makes sense to push the workflow execution to the place where data lives. The actual execution engine needs only be deployed once, while actual workflows are relatively small and are usually represented in the order of tens or hundreds of kilobytes. Predixion SQL CLR Stored Procedure is a managed .Net 3.5 stored procedure, which can be deployed on SQL Server 2008 R2 and SQL 2012 databases. The stored procedures allow Predixion Insight to score data inside those databases without having to move the data out to the Predixion server. The scored data is written back as a new table into the same database. This minimizes data transfer while scoring and allows large quantities of data to be scored very fast. Predixion SQL CLR Stored Procedures Deployment Guide instructs you on how to deploy the Predixion Insight code component required for executing an MLSM inside your database. The Predixion Insight execution component deploys as a SQL CLR stored procedure. Consequently, the execution of SQL CLR needs to be enabled at the server level. The stored procedure needs to be deployed in each SQL Server database which contains data. The source data will be processed through the MLSM independent of the source data origin. The in-database execution will only occur when the source data and the destination for the processed data are inside the same SQL Server database. Alternately, data will be streamed for processing through the Predixion server. Deployment of Predixion SQL CLR Stored Procedures Prerequisites: The following are prerequisites for installing the Predixion CLR stored procedures: Microsoft SQL Server 2008 R2 or Microsoft SQL Server 2012 User with sysadmin rights to the SQL server, so that the user is able to deploy CLR stored procedures. Machine with SQL Client tools and PowerShell installed from where the user will deploy the package. How do I deploy the Predixion CLR? 1. Copy the Predixion.SQLCLR.Install.zip file from the Predixion server location to the machine where you’ll run the install and expand it to a folder. The package contains the following files: CommonLibrary.ps1 PowerShell scripts for installing and InstallSQLCLR.ps1 uninstalling. UninstallSQLCLR.ps1 Predixion Software | 31910 Del Obispo #120, San Juan Capistrano, CA 92675 | +1.949.373.4900 | www.PredixionSoftware.com Predixion.AnalyticsCore.dll Predixion.AnalyticsCore.XmlSerializers.dll Predixion.SQLCLR.SQL2008.dll Predixion.SQLCLR.SQL2012.dll Predixion.SQLCLR.SQL2008.dacpac Predixion.SQLCLR.SQL2012.dacpac InstallTemplate.sql Uninstall.sql Predixion binaries which are deployed as managed assemblies to the target SQL server database DACPAC files for installing through SQL Server Data tools Installation SQL script Uninstall SQL script 2. Open a PowerShell script 3. For each database you want to install the Predixion CLR Package and run the following command: SQL 2008 R2: InstallSQLCLR.ps1 <sqlServerName> <databaseName> 2008 SQL 2012 : InstallSQLCLR.ps1 <sqlServerName> <databaseName> 2012 These commands will enable Predixion CLR and therefore In-Database scoring from Predixion Insight for that database. Please note that the input data must also be in the same database for In-database scoring to work. How do I uninstall the Predixion CLR? To uninstall the Predixion CLR, run the following command from a PowerShell window: UninstallSQLCLR.ps1 <sqlServerName> <databaseName> What does the script do? The following section describes the different sections of the SQL script used for install. This allows an advanced user to modify the script manually if required: 1. Enables managed CLR on the target SQL server: PRINT N'Enabling SQLCLR...'; GO EXEC sp_configure 'show advanced options' , '1'; go reconfigure; go EXEC sp_configure 'clr enabled' , '1' go reconfigure; -- Turn advanced options back off EXEC sp_configure 'show advanced options' , '0'; Go 2. Cleans existing Predixion assemblies from the SQL server: PRINT N'Cleaning existing assemblies...'; GO IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PredictionSQLCLRExists]')) DROP FUNCTION [dbo].[PredictionSQLCLRExists]; GO IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PXPredict]')) DROP PROCEDURE [dbo].[PXPredict]; GO IF EXISTS (SELECT [name] FROM sys.assemblies WHERE [name] = N'Predixion.SQLCLR') DROP ASSEMBLY [Predixion.SQLCLR]; GO IF EXISTS (SELECT [name] FROM sys.assemblies WHERE [name] = N'Predixion.AnalyticsCore.XmlSerializers') DROP ASSEMBLY [Predixion.AnalyticsCore.XmlSerializers] GO IF EXISTS (SELECT [name] FROM sys.assemblies WHERE [name] = N'Predixion.AnalyticsCore') DROP ASSEMBLY [Predixion.AnalyticsCore] GO Predixion Software | 31910 Del Obispo #120, San Juan Capistrano, CA 92675 | +1.949.373.4900 | www.PredixionSoftware.com 3. Sets TRUSTWORTHY=ON on the target database to allow deployment of the assemblies PRINT N'Installing Predixion.SQLCLR on database ' + db_name() + '...'; DECLARE @SQL nvarchar(4000) SELECT @SQL ='ALTER DATABASE ' + db_name() + ' SET TRUSTWORTHY ON' exec sp_executesql @SQL GO 4. Deploys the assemblies. PACKAGE_LOCATION should be replaced by the actual path where the assemblies have been unpackaged and SQL_CLR_NAME by the name of the 2008 or 2012 DLL. CREATE ASSEMBLY [Predixion.AnalyticsCore] FROM N'PACKAGE_LOCATION\Predixion.AnalyticsCore.dll' WITH PERMISSION_SET = UNSAFE GO CREATE ASSEMBLY [Predixion.AnalyticsCore.XmlSerializers] FROM N'PACKAGE_LOCATION\Predixion.AnalyticsCore.XmlSerializers.dll' WITH PERMISSION_SET = UNSAFE GO PRINT N'Creating [Predixion.SQLCLR]...'; GO CREATE ASSEMBLY [Predixion.SQLCLR] AUTHORIZATION [dbo] FROM N'PACKAGE_LOCATION\SQL_CLR_NAME.dll' WITH PERMISSION_SET = UNSAFE; GO 5. Creates managed stored procedures PRINT N'Creating [dbo].[PredictionSQLCLRExists]...'; GO CREATE FUNCTION [dbo].[PredictionSQLCLRExists] () RETURNS NVARCHAR (4000) AS EXTERNAL NAME [Predixion.SQLCLR].[UserDefinedFunctions].[PredictionSQLCLRExists] GO PRINT N'Creating [dbo].[PXPredict]...'; GO CREATE PROCEDURE [dbo].[PXPredict] @querySpec XML, @sourceDataQuery NVARCHAR (4000) AS EXTERNAL NAME [Predixion.SQLCLR].[StoredProcedures].[PXPredict] GO In-Database Scoring Prerequisites: 1. You need a predictive model, created using Insight Analytics. 2. In-database scoring requires that you have the same predictor (explanatory) attributes in a table in the database. You can monitor the SQL Server Database using SSMS and determine if such a database and table exists. Process: 1. 2. 3. 4. 5. Use the Query Button of the Insight Analytics Ribbon to start the Query Data Wizard. In the Select Predictive Model page of the Query Data wizard, select a predictive model. In the Select Input Data page of the Query Data wizard: select the input type: SqlServer Data Provider. use the Data Source text box to select the proper server and database where the predictor attributes reside. Predixion Software | 31910 Del Obispo #120, San Juan Capistrano, CA 92675 | +1.949.373.4900 | www.PredixionSoftware.com 6. specify the predictor or explanatory attributes through a query or a table. 7. In the Specify Relationship page of the Query Data wizard, align the model application columns with the input columns for the explanatory attributes. 8. In the Choose result page of the Query Data wizard choose the desired output results 9. In the Select Destination page of the Query Data wizard: 10. choose to "Add the result to external provider". 11. select under Destination: SqlServer Data Provider 12. specify under Data Source the server and database. 13. select under Table Name or Query the name of a table for the query output including the predictions. 14. Finish ©2013 Predixion Software. All Rights Reserved. All trademarks and registered trademarks are the property of their respective owners. Predixion Software | 31910 Del Obispo #120, San Juan Capistrano, CA 92675 | +1.949.373.4900 | www.PredixionSoftware.com