Rewrite of suppressed index query | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

Rewrite of suppressed index query

I have the following query that needs to be rewritten (if possible) so that the clustered index on column "coldate" can be utilized. Any suggestions would be appreciated. SELECT *
table t
DateDiff(n, DateAdd(n, t.col1 – 1, t.coldate), @date) <= 0
DateDiff(n, t.coldate, DateAdd(n, @days, @date)) > 0
Just avoid using functions on the column.
Example change : DateDiff(n, t.coldate, DateAdd(n, @days, @date)) > 0 to t.coldate< dateadd(n, @days, @date) KH
So I did as you said and revised my query to:<br /><br />SELECT *<br />FROM <br />table t <br />WHERE <br />DateDiff(n, DateAdd(n, t.col1 – 1, t.coldate), @date) &lt;= 0<br />AND <br />t.coldate &lt; dateadd(n, @days, @date)<br /><br />The original statement resulted with a Clustered Index Scan with 360 logical reads.<br />The revised statement resulted in a Clustered Index Seek with 69 logical reads.<br /><br />So my question now…from what I have read, it seems like I should have still received a Clustered Index Scan, because the first part of the WHERE clause is using a function [DateDiff] on an indexed column. My explain plain from the revised statement is;<br /><br /> |–Clustered Index Seek(OBJECT<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[MyDB].[dbo].[Table].[IX_Table_ColDate]), SEEK<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[Table].[ColDate] &lt; dateadd(minute, Convert([@days]), [@date])), WHERE<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />datediff(minute, dateadd(minute, Convert([Table].[Col1])-1, [Table].[ColDate]), [@date])&lt;=0) ORDERED FORWARD)<br /><br />I do not want to argue with success, and there is room for improvement, but with that aside, why is there not an Index Scan on the DateDiff function used on an indexed column?