query optimization - Index not getting used + sql server 2005 -


here query

declare @startdatetime datetime, @enddatetime datetime  select @startdatetime = '2010-11-15', @enddatetime = '2010-11-16'  select practicecode, accountno, proccd, modifier , chargedos, paid amt, createddate,      case when paid > 0 'p'            when paid = 0 , writtenoff = dueamt 'a'           else 'o'       end status      trn_postings      createddate >= @startdatetime , createddate <= @enddatetime             --and manualoverride in ('s','f','x','g','o')             , manualoverride in ('n','u') 

edit : created date datetime column contains date & time of record created

i have individual indexes on both createddate , manualoverride. execution plan shows clustered index scan. table has million record , can grow 4 5 times in near future.

the surprising part if change clause below, uses both indexes. dont know why.

     createddate >= @startdatetime , createddate <= @enddatetime             , manualoverride in ('s','f','x','g','o')             --and manualoverride in ('n','u') 

how make sql use indexes...

further if use not in clause wont index used.

i'd suggest creating composite index on createddate , manualoverride. when both conditions can satisfied using indexes, that's optimizer appears doing.

but once knows has use clustered index satisfy part of query, appear it's ignoring other indexes. has decided cost(use createddate index + ci lookup + condition) > cost(scan ci). it's wrong, happens. index on both columns (createddate first) used. or can try force it's hand using index query hint. may still choose ignore hint.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -