Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Two different plans

Author  Topic 

Sachin.Nand

2937 Posts

Posted - 2010-12-11 : 07:56:23
I have a table with a single Non clustered Index on say col1 and another column which is not a part of any index ex col2.

Now when I query the table with this query

Select col2 from table where col1='Some Value'

I get a straight Clustered Index Scan which is expected.

But the same query when written like this
Select col2 from table where col1 like 'Some Value'


The optimiser uses a Index seek but with a Key Lookup.

Can anyone explain this behaviour?

From what I think is it that for the each row the like clause touches it uses a keylookup to get the value of col2.If thats the case then why is optmizer using a seek on col1 to traverse through all the rows ?

Is it right or am I missing something?



PBUH

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-12-11 : 13:53:36
Have you tried DBCC FREEPROCACHE to ensure that you don't have a bad plan in cache?

If we were to only optimize your query, then I'd suggest adding col2 as an include column to your existing index.

What's the selectivity of the value?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -