开发者

Optimising DATEDIFF

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜