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 *FROM
table t
WHERE
DateDiff(n, DateAdd(n, t.col1 – 1, t.coldate), @date) <= 0
AND
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) <= 0<br />AND <br />t.coldate < 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=’



]]>