TheSQLGuru
3/27/2007 5:37:00 PM
1) The optimizer will ignore indexes if it estimates a query will access
between approx 8 and 15% of the rows in a given table. This alone could be
reason for not using indexes. Sequential I/O from table scans is SO much
more efficient than async I/O from index/bookmark lookup activity.
2) Get in the habit of NEVER modifying/functioning a field that is indexed.
3) TSQL's syntax for date comparison is using DATEDIFF or DATEADD.
DATEDIFF ( datepart , startdate , enddate )DATEADD (datepart , number,
date )Taking 2 and 3 into consideration, you might rewrite your query
thusly:SELECT top 10 * FROM dbo.DataWHERE Created < DATEADD(dd, 365,
GETDATE())AND Modified < DATEADD(dd, 60, GETDATE())ORDER BY myprimarykeyBut
still do NOT expect index usage if there are lots of old data in your
system. If you want to see how bad it can be, force the use of the index on
created by using the (INDEX=...) syntax.
--
TheSQLGuru
President
Indicium Resources, Inc.
"Dan English" <nospam@cox.net> wrote in message
news:uCn7kRJcHHA.4836@TK2MSFTNGP03.phx.gbl...
> This query is slow on my production server:
>
> Select Top 10 * From Data
> Where
> (GetDate()-Created > 365) And
> (GetDate()-Modified > 60)
> Order By MyPrimaryKey
>
> Data has about a million rows (each record is not very large). Created
> and Modified are datetime columns. I have separate indexes defined for
> each of them, but SQL Server 2005 insists on doing a full index scan on
> MyPrimaryKey. Any suggestions for improving this query or making it use
> the indexes?
>
> Some names have been changed to protect their identity ;)
>
> Thanks!