|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, January 30, 2013 12:40 AM
Points: 12,
Visits: 100
|
|
I have a SQL 2008 R2 table with a few composite indexes and a few single column indexes. I've narrowed my problem down to this; if I run a query such as the following:
select * from tbl where col > getdate()
the execution plan shows that index built on col is used. However if I run the query this way:
declare @todaydate datetime set @todaydate = getdate() select * from tbl where col > @todaydate
then the execution plan shows that the index wasn't used.
I would like to know if this is how sql works or is the sql optimizer just recognizing that it doesn't need to use the index.
Thanks in advance for your help
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 2:52 AM
Points: 37,734,
Visits: 30,001
|
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, January 30, 2013 12:40 AM
Points: 12,
Visits: 100
|
|
Okay, this is because the engine simply ignores the local variable value and compiles a plan based on general statistics assumptions.
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 2:52 AM
Points: 37,734,
Visits: 30,001
|
|
Not ignores. At compile time (before any of the batch is executed), the variable doesn't have a value and hence any value it gets during execution can't be sniffed and used.
Gail Shaw Microsoft Certified Master: SQL Server 2008, MVP SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter We stand on the bridge and no one may pass
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Friday, April 26, 2013 2:58 PM
Points: 221,
Visits: 452
|
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, January 30, 2013 12:40 AM
Points: 12,
Visits: 100
|
|
Eh, just wonder why the equal operator can make the execution plan shows that the index was used.
declare @todaydate datetime set @todaydate = getdate()
select * from tbl where col = @todaydate However the execution plan shows that the index wasn't used in <, <>, >, >=, <=?
declare @todaydate datetime set @todaydate = getdate()
select * from tbl where col > @todaydate
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 2:52 AM
Points: 37,734,
Visits: 30,001
|
|
The estimations from an equality where there's no parameter sniffing is based on the average density of the column. The estimations for an inequality where there's no parameter sniffing is, if I recall, 30% of the table. The latter is far too high for seek + key lookup to be efficient, the former may be small enough.
Gail Shaw Microsoft Certified Master: SQL Server 2008, MVP SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter We stand on the bridge and no one may pass
|
|
|
|