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.
精彩评论