3

I have a Datetime column called CreatedOn in a table. CreatedOn is also part of a non-clustered index where the order is descending.

Previously in the where condition I had a condition as follows

WHERE DateDiff(d,CreatedOn,GetDate()) < 180 AND ... other conditions

I changed this to

WHERE CreatedOn > '2012-04-04 00:00:00.000' AND ... other conditions

where I am calculating the cutoff date in C# code and then putting that in the adhoc query.

According to me, the second condition should be faster but I do not yet see a significant change in query execution times. But as the size of the table grows, which one will run faster?

3
  • The standard answers are: A) Do you think it'll really matter? And B) If so, test it with your data, table structure, indexes, server, etc. :-) (But I'd expect the second to be more optimizable, which might be faster.) Apr 5, 2012 at 12:05
  • 1
    @T.J.Crowder: A it does (common SQL mistake). B no need because of A
    – gbn
    Apr 5, 2012 at 12:14
  • @gbn: The answer to A is not necessarily that it matters. It depends on the data set (e.g., is the index important). Common mistake. ;-) Apr 5, 2012 at 14:50

1 Answer 1

6

The second form.

Putting functions on columns invalidates use of indexes (in almost all cases, so simply follow this rule always). See "Ten Common SQL Programming Mistakes", number 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.