sql server - Why is selecting date slow -


this question has answer here:

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 

valid xhtml http://biginkz.com/pics/dataasigned.jpg.

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

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -