我正在比较我的自定义 CLR 聚合与 AVG (SQL Server 2017)。我的查询是:
SELECT groupId, Helpers.CustomCLR(value)
FROM table
group by groupId
SELECT groupId, AVG(value)
FROM table
group by groupId
而CLR是
[Serializable]
[SqlUserDefinedAggregate(
Format.Native, //use clr serialization to serialize the intermediate result
IsInvariantToNulls = true, //optimizer property
IsInvariantToDuplicates = false, //optimizer property
IsInvariantToOrder = true)
]
[StructLayout(LayoutKind.Sequential)]
public class CustomCLR
{
float a = 2;
public void Init()
{
}
public void Accumulate(SqlSingle value)
{
}
public void Merge(CustomCLR other)
{
}
public double? Terminate()
{
return a;
}
}
但是,执行计划完全不同。CLR 查询执行行模式排序,而 AVG 查询执行批处理模式哈希匹配。如何使 CLR 聚合表现得像 AVG 一样?