SQL优化where子句

use*_*575 2 sql-server sql-server-2008

我们可以做一些不同的事情,而不是在where子句中使用函数.

DateAdd花时间表现不佳我猜...

如何优化这个sql

SELECT cust_id, order_date, price
FROM customers
WHERE DATEADD(DD,50,order_date)>=GETDATE()
Run Code Online (Sandbox Code Playgroud)

Sql*_*Zim 7

不要运行你的功能order_date,getdate()而是运行反向运行

select cust_id, order_date, price
from customers
where order_date>=dateadd(Day,-50,getdate())
Run Code Online (Sandbox Code Playgroud)

函数调用order_date将导致索引扫描,如果您改为在过滤条件上运行函数,则getdate()可以在此列上保留索引搜索.(如果它有索引).

SQL Server中的SARGable函数 - Rob Farley

  • 是的,如果您在order_date列上创建索引. (3认同)