I am using AdventureWorks2012 database.
I created the following index on Sales.SalesOrderHeader table
create index i1 on
sales.salesorderheader(purchaseordernumber,salespersonid)
include(orderdate,shipdate)
where purchaseordernumber is not null
and salespersonid is not null
When I run the below query:
select
PurchaseOrderNumber,
OrderDate,
ShipDate,
SalesPErsonId
from sales.salesorderheader
where purchaseordernumber like '%po5%' and salespersonid is not null
I expect an Index seek not an index scan, as all of the columns which are part of the query are already part of the index.
I read somewhere that if the query optimizer feels that index scan is cheaper than index seek, then we will index scan.
But, I don’t understand why it’s happening in this case. We don’t have a lookup at all.
Can someone explain me please.
An index works just like a phonebook. Where would you look for
%po5%in a phonebook? You’d have to read the entire phonebook, because you don’t know the first character.That’s why SQL Server scans the entire index. It does not have enough information to seek.