SQL Server CLR UDF Parallelism redux

◇◆丶佛笑我妖孽 提交于 2019-11-30 23:14:48

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).

  • Determinism means that the same inputs are guaranteed to have the same output. So if your function will always return the same value for a particular set of inputs, then it is deterministic and should be marked as IsDeterministic = true.
  • You can set 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:

  • One additional item that is probably required is that the assembly containing this method has a PERMISSION_SET of SAFE.
  • It is probably not required to have the IsPrecise property of the SqlFunction attribute set to true in order to get the UDF to work in a parallel execution plan.
Michael Ganß

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!