March 5, 2008 at 2:59 pm
Hi,
Facing a situation where the execution plan shows both an index seek as well as KeyLookUp. The cost for the Index seek is 50% where the rest 50% is for the KeyLookUp
SELECT
ph.ProductId
,ph.CLientID
, ph.PolicyType
FROM
PolicyHeaders ph
INNER JOIN Products pr
ON pr.ProductId = ph.ProductId
AND ph.ClientID = 'C0000818'
AND ph.PolicyType='PA'
The primary key of this table is PolicID and is not included in this select. I have a Non Clustered Index which is defined as follows
CREATE NONCLUSTERED INDEX [IX_ClientID] ON [PolicyHeaders]
(
[ClientID] ASC,
[PolicyType] ASC
)
If add the ProductID to the index I get a IndexScan. Please advice how I could remove the KeyLookup
Thanks
Vinoj
March 5, 2008 at 3:07 pm
You'll need to include the ProductID in the index to do that...
--Jeff Moden
Change is inevitable... Change for the better is not.
March 5, 2008 at 3:14 pm
Thanks. For some reason when i added the ProductID as the first column of the index and it gave an Index Scan. But when i added it as the last field in the index it gave me a full index seek. Thanks for the help
Vinoj
March 6, 2008 at 4:20 am
That's what "index tuning" is all about... getting the columns in the correct order to produce an Index Seek. Nice job.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 6, 2008 at 7:28 am
The index with ProductID first produces an index scan because ProductID is not one of the columns that are filtered on. A query can use an index seek if the columns in the where clause are a left-based subset of the columns in an index (a bit of a gneralisation, but true enough)
Edit: correctness.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
March 6, 2008 at 7:40 am
GilaMonster (3/6/2008)
The index with ProductID first produces an index scan because ProductID is not in the where clause.
Ummmm..... heh... What's that highlighted in red????
SELECT
ph.ProductId
,ph.CLientID
, ph.PolicyType
FROM
PolicyHeaders ph
INNER JOIN Products pr
ON pr.ProductId = ph.ProductId
AND ph.ClientID = 'C0000818'
AND ph.PolicyType='PA'
--Jeff Moden
Change is inevitable... Change for the better is not.
March 6, 2008 at 8:28 am
Perhaps I should have said 'not used in the filters' rather than not in the where clause (since there's no where clause)
The red highlight is a join to a lookup table on what I would assume is a foreign key relationship. Hence, not going to cut the rowset down, hence not useful for determining which rows in PolicyHeader are affected by the query.
Indexes are chosen to reduce the number of considered rows as fast as possible. The filters will do that, that join is highly unlikely to.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
March 6, 2008 at 2:37 pm
The join needs to be part of the covering index or you will only get a scan.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 7, 2008 at 8:30 am
The order of the columns is critical to optimal index performance. It is usually appropriate to put the columns in order of specificity, assuming all 3 columns (or at least the columns from left to right) are filtered on.
Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
March 7, 2008 at 1:45 pm
Jeff Moden (3/6/2008)
The join needs to be part of the covering index or you will only get a scan.
True, which is why I removed the suggestion about INCLUDE (and yes, I didn't see the join first time).
I would still suggest it as the third column in this case, because (and it's a guess without seeing the exec plan) I expect SQL to filter first and join second (since the filter reduces the rows which the join won't, if it's a join to a lookup table)
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply