且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

一个where子句中的SQL DateDifference

更新时间:2023-02-06 07:54:08

DateDiff 非常快...您的问题是您正在数据库表列值上运行它,因此查询处理器必须在表中的每一行运行该函数,即使有该列的索引。这意味着它必须从磁盘加载整个表。

DateDiff is extremely fast... Your problem is you are running it on the database table column value, so the query processor must run the function on every row in the table, even if there was an index on this column. This means it has to load the entire table from disk.

而是在今天的日期使用 dateAdd 函数,并将数据库表列与该单个结果进行比较计算。现在它只运行 DateAdd()一次,它可以使用索引(如果存在),只加载与谓词标准匹配的行。

Instead, use the dateAdd function on todays date, and compare the database table column to the result of that single calculation. Now it only runs DateAdd() once, and it can use an index (if one exists), to only load the rows that match the predicate criterion.

其中a.DateValue> DateAdd(day,-3,getdate())

以这种方式使您的查询谓词 SARG-capable

doing this in this way makes your query predicate SARG-able