SQL Server - CLR 在标量函数中存储过程作为过滤器在查询优化器中未正确加权 ==>糟糕的执行计划
我有一个如下所示的查询:
SELECT someString FROM
(
SELECT someString FROM someTable
WHERE someField = 1
) X
WHERE dbo.fnMyClrScalarFunction(X.someString) = 0
问题是查询优化器正在将 UDF 移动到子查询内,在相当严格的“someField = 1”条件之前应用它。不幸的是,UDF 速度并不快,这导致性能很差。有没有什么方法可以防止这种情况(除了使用临时表)或向 sql server 表明 UDF 很昂贵?
提前致谢
I have a query that looks like the following:
SELECT someString FROM
(
SELECT someString FROM someTable
WHERE someField = 1
) X
WHERE dbo.fnMyClrScalarFunction(X.someString) = 0
The problem is that the query optimizer is moving the UDF inside the subquery, where it is applied before the fairly restrictive 'someField = 1' condition. Unfortunately, the UDF is not exactly speedy, and this results in terrible performance. Is there any way to prevent this (aside from using a temp table) or to establish to sql server that the UDF is expensive?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“相当限制性的‘someField = 1’条件”
您在该列上有索引吗?如果 SQL 认为这样做会更快,则应仅应用标量函数。如果您的索引的第一列是“someField”,那么我建议您确保该表的统计信息是最新的。
"fairly restrictive 'someField = 1' condition"
Do you have an index on that column. SQL should only apply the scalar function if it thinks it would be faster to do so. If you have an index where the first column is 'someField', then I would recommend that you make sure the statistics for this table is up to date.
一种方法是使用临时表或表变量:
临时表看起来比实际更昂贵。事实上,大多数连接都是使用临时辅助表执行的:)
One way is to use a temporary table, or a table variable:
Temporary look more expensive than they are. In fact, most joins are executed using temporary helper tables :)