[英]TSQL query optimizer view on non-nullable ISNULL()
As part of some dynamic SQL (ick), I've implemented the 'sort NULLs last' solution described here: Sorting null-data last in database query 作为某些动态SQL的一部分,我实现了此处描述的'sort NULLs last'解决方案: 在数据库查询中对null数据进行最后排序
ORDER BY CASE column WHEN NULL THEN 1 ELSE 0 END, column
My question is: On non-nullable columns that have ISNULL() applied to them, will the query optimizer strip this out when it realises that it will never apply? 我的问题是:在已将ISNULL()应用于它们的非空列上,当查询优化器意识到它永远不会适用时,是否会将其删除?
It's not clear why your question mentions the ISNULL
function when that isn't in your code. 目前尚不清楚为什么您的问题在代码中没有提到
ISNULL
函数。
ORDER BY CASE column WHEN NULL THEN 1 ELSE 0 END, column
First of all this code doesn't work, it is equivalent to CASE WHEN column = NULL
which is not what you need. 首先,此代码不起作用,它等效于
CASE WHEN column = NULL
,这不是您所需要的。
It would need to be 它需要是
ORDER BY CASE WHEN column IS NULL THEN 1 ELSE 0 END, column
The optimisation question is easy to test. 优化问题很容易测试。
CREATE TABLE #T
(
X INT NOT NULL PRIMARY KEY
)
SELECT *
FROM #T
ORDER BY X
SELECT *
FROM #T
ORDER BY CASE WHEN X IS NULL THEN 1 ELSE 0 END, X
DROP TABLE #T
The plan shows a sort operation in the second plan indicating that this was not optimised out as you hoped and the pattern is less efficient than ORDER BY X
. 该计划在第二个计划中显示了一个排序操作,表明该操作未如您希望的那样进行优化,并且该模式的效率低于
ORDER BY X
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.