sql server - Why is selecting date slow -
this question has answer here:
- performance of sql server 2005 query 3 answers
i have table 1.5 million rows date_run indexed non cluster. query #1 takes 0 second finish , query #2 takes 3 seconds. can please explain why query #2 runs slower. included execution plans both. sql server version 2014.
query #1
select avg14gain stocktrack date_run >= '2013-3-21' , date_run < '2013-3-22'
valid xhtml http://biginkz.com/pics/datehardcoded.jpg.
query #2
declare @today date declare @yesterday date set @today='2013-3-22' set @yesterday='2013-3-21' select avg14gain stocktrack date_run >= @yesterday , b.date_run <@today
i not sure why query not picking index, can use index hint.
try this:
declare @today date declare @yesterday date set @today='2013-3-22' set @yesterday='2013-3-21' select avg14gain stocktrack date_run >= @yesterday , b.date_run <@today (index([stocktrack].[ix_drun]))
also can try suggested in post: tsql not using indexes. see @justin dearing's answer (rebuild index / update statistics).
Comments
Post a Comment