I have been researching SQL Server CLR UDFs and parallelism for some time. The general consensus seems to be that in SQL Server 2008 and later, a scalar value CLR UDF with DataAccessKind.None should allow parallel execution.
However, when I use my scalar value UDF in my view in SQL Server 2012, it still kills parallel execution in joins and the like.
Is there something special I need to add to my C# code or the T-SQL UDF definition to indicate that it is safe for parallel execution?
Thanks.
According to the MSDN forum that is linked to in the first comment on the question, your C# code roughly starts out as:
[Microsoft.SqlServer.Server.SqlFunction()]
public static SqlString MyUDF(SqlString data)
and according to the question you have added DataAccessKind.None making it:
[Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.None)]
public static SqlString MyUDF(SqlString data)
To start with, both DataAccess and SystemDataAccess default to DataAccessKind.None, so setting them explicitly to DataAccessKind.None, while a good practice, shouldn't have made any noticeable difference.
There are two other properties that need to be set: IsDeterministic and IsPrecise. These properties are meta-data that the Query Optimizer uses and are both false by default. Hence, it is best to set one or both of them to true (assuming, of course, that the settings accurately reflect the code within that particular function).
IsDeterministic = true.IsPrecise = true if you are not using any floating point (i.e. Double or Single) values (i.e. FLOAT or REAL in T-SQL terms).The SqlFunction attribute should look as follows:
[Microsoft.SqlServer.Server.SqlFunction(SystemDataAccess = DataAccessKind.None,
DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
public static SqlString MyUDF(SqlString data)
UPDATE:
PERMISSION_SET of SAFE.IsPrecise property of the SqlFunction attribute set to true in order to get the UDF to work in a parallel execution plan.It might not be a problem with the CLR UDF but with SQL Server's query plan decision. You can force a parallel plan by using OPTION (QUERYTRACEON 8649) as explained here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With