We are working on a UDF solution for MSSQL Server. Currently we have scalar TSQL UDFs which calls CLR functions. CLR functions (written in C#) further calls C functions present in a DLL file which is imported by C# layer using DLLImport. Also due to some requirement, our CLR function needs database access therefore we have added following properties to them [SqlFunction(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
With this design we observe very less performance for the UDFs.
We further investigated and found that when we submit a query involving our UDF, like following
*insert into test_destination(id,name) select id, master.dbo.my_udf1(name) from test_source; (*test_source have 1 million records)
then our UDF is called sequentially one by one for each record i.e., 1 million times. We believe SQL server is not enabling parallelism here for processing the query. Due to this performance is bad.
To enable parallelism for our scalar UDFs, we tried
- Inlining our UDFs
- Converting them to inline table valued function
- Using MAXDOP query hint
but SQL server is not enabling parallelism with these as well.
We did not tried QUERYTRACEON query hint as it is not recommended for production environments.
So is there any way by which we can enable parallelism in SQL server for scalar UDFs which calls a CLR function?