开发者

Optimising DATEDIFF

开发者 https://www.devze.com 2022-12-12 19:23 出处:网络
I just want to check my logic there. Suppose I 开发者_Python百科want to find all new products in the last 30 days. My current procedure is this:

I just want to check my logic there.

Suppose I 开发者_Python百科want to find all new products in the last 30 days. My current procedure is this:

SELECT ProductName
FROM ProductTable
WHERE DATEDIFF( d, CreateDate, GETDATE() ) < 30 

However, I understand that functions like DATEDIFF will not use the non-clustered index I have created on CreateDate. So therefore, will my query run quicker using the following procedure:

SELECT ProductName
FROM ProductTable
WHERE CreateDate >= DATEADD( d, -30, GETDATE() ) AND CreateDate < GETDATE() 

BTW, I don't have SQL Server from where I am so I cant test this using the execution plan.


Yup, you are correct, the second query will be faster as it can use any indexes on CreateDate that are available.

0

精彩评论

暂无评论...
验证码 换一张
取 消