简体   繁体   English

当where子句中存在动态条件时,SQL Server查询性能

[英]SQL Server query performance when there's a dynamic condition in where clause

Let's say we have query A: 假设我们有查询A:

SELECT count(1) FROM MyTable WHERE Date_Created < DATEADD(DD, 3, GETDATE())

AND query B: 和查询B:

SELECT count(1) FROM MyTable WHERE Date_Created < '2013-05-24'

When those queries are run, how does the compiler optimize query A? 运行这些查询时,编译器如何优化查询A? Does it re-evaluate DATEADD and GETDATE for each row in MyTable ? 它是否为MyTable中的每一行重新评估 DATEADD和GETDATE?

The reason I am asking is because I ran several tests to see which queries are faster and the result seems to indicate that there's no huge difference in the performance of the two, which is kinda counter-intuitive. 我问的原因是因为我运行了几个测试来查看哪些查询更快,结果似乎表明两者的性能没有太大差异 ,这有点违反直觉。 Thanks. 谢谢。

GETDATE is a runtime constant and won't be repeatedly re-evaluated. GETDATE是一个运行时常量 ,不会重复进行重复评估。

Chances are that the whole expression with DATEADD will be only evaluated once. 有可能使用DATEADD的整个表达式只会被评估一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM