all groups > sql server programming > june 2007 >
You're in the

sql server programming

group:

Execution plan...another set of eyes please



Execution plan...another set of eyes please chad.mccune NO[at]SPAM gmail.com
6/17/2007 11:47:20 PM
sql server programming: I've been up all night writing some new codesmith templates and was
just looking over the execution plans on some of my queries and I
noticed this one was doing 3 index seeks and 1 indes scan. Does
anyone see what would be causing the index scan?

Select
[dbo].[LinkCategory].[LinkCategoryId],
[dbo].[LinkCategory].[ParentLinkCategoryId],
[dbo].[LinkCategory].[Name],
[dbo].[LinkCategory].[Directory],
[dbo].[LinkCategory].[FullDirectoryPath],
[dbo].[LinkCategory].[HeaderCopyId],
[dbo].[LinkCategory].[FooterCopyId],
[dbo].[LinkCategory].[_Descriptor],
[dbo].[LinkCategory].[_Rank],
[dbo].[LinkCategory].[_Created],
[dbo].[LinkCategory].[_Updated],
[dbo].[LinkCategory].[_UpdatedBy],
[dbo].[LinkCategory].[_UpdatedWith],
[dbo].[LinkCategory].[_Inactive],
[ParentLinkCategory].[LinkCategoryId]
[ParentLinkCategory_LinkCategoryId],
[ParentLinkCategory].[ParentLinkCategoryId]
[ParentLinkCategory_ParentLinkCategoryId],
[ParentLinkCategory].[Name] [ParentLinkCategory_Name],
[ParentLinkCategory].[Directory] [ParentLinkCategory_Directory],
[ParentLinkCategory].[FullDirectoryPath]
[ParentLinkCategory_FullDirectoryPath],
[ParentLinkCategory].[HeaderCopyId]
[ParentLinkCategory_HeaderCopyId],
[ParentLinkCategory].[FooterCopyId]
[ParentLinkCategory_FooterCopyId],
[ParentLinkCategory].[_Descriptor]
[ParentLinkCategory__Descriptor],
[ParentLinkCategory].[_Rank] [ParentLinkCategory__Rank],
[ParentLinkCategory].[_Created] [ParentLinkCategory__Created],
[ParentLinkCategory].[_Updated] [ParentLinkCategory__Updated],
[ParentLinkCategory].[_UpdatedBy] [ParentLinkCategory__UpdatedBy],
[ParentLinkCategory].[_UpdatedWith]
[ParentLinkCategory__UpdatedWith],
[ParentLinkCategory].[_Inactive] [ParentLinkCategory__Inactive],
[HeaderCopy].[CopyId] [HeaderCopy_CopyId],
[HeaderCopy].[Text] [HeaderCopy_Text],
[HeaderCopy].[_Descriptor] [HeaderCopy__Descriptor],
[HeaderCopy].[_Rank] [HeaderCopy__Rank],
[HeaderCopy].[_Created] [HeaderCopy__Created],
[HeaderCopy].[_Updated] [HeaderCopy__Updated],
[HeaderCopy].[_UpdatedBy] [HeaderCopy__UpdatedBy],
[HeaderCopy].[_UpdatedWith] [HeaderCopy__UpdatedWith],
[HeaderCopy].[_Inactive] [HeaderCopy__Inactive],
[FooterCopy].[CopyId] [FooterCopy_CopyId],
[FooterCopy].[Text] [FooterCopy_Text],
[FooterCopy].[_Descriptor] [FooterCopy__Descriptor],
[FooterCopy].[_Rank] [FooterCopy__Rank],
[FooterCopy].[_Created] [FooterCopy__Created],
[FooterCopy].[_Updated] [FooterCopy__Updated],
[FooterCopy].[_UpdatedBy] [FooterCopy__UpdatedBy],
[FooterCopy].[_UpdatedWith] [FooterCopy__UpdatedWith],
[FooterCopy].[_Inactive] [FooterCopy__Inactive]
From
[dbo].[LinkCategory]
Left Outer Join [dbo].[LinkCategory] [ParentLinkCategory] (nolock)
On [dbo].[LinkCategory].[ParentLinkCategoryId] = [ParentLinkCategory].
[ParentLinkCategoryId]
Left Outer Join [dbo].[Copy] [HeaderCopy] (nolock) On [dbo].
[LinkCategory].[HeaderCopyId] = [HeaderCopy].[CopyId]
Left Outer Join [dbo].[Copy] [FooterCopy] (nolock) On [dbo].
[LinkCategory].[FooterCopyId] = [FooterCopy].[CopyId]
Where
[dbo].[LinkCategory].[LinkCategoryId] = 1
Re: Execution plan...another set of eyes please chad.mccune NO[at]SPAM gmail.com
6/17/2007 11:48:52 PM
I guess it might help to include that the index scan is on the self
referencing join to LinkCategory
Re: Execution plan...another set of eyes please Roy Harvey
6/18/2007 12:36:36 PM
Just a thought on this.

The outermost table, LinkCategory, is only limited by the WHERE clause
test for LinkCategoryId = 1. Unless that is best satisfied by an
index lookup then the outer table - or an index on the outer table -
is going to be scanned. How else would it include all rows?

Roy Harvey
Beacon Falls, CT

[quoted text, click to view]
Re: Execution plan...another set of eyes please Erland Sommarskog
6/18/2007 10:13:31 PM
(chad.mccune@gmail.com) writes:
[quoted text, click to view]

What sort of join do you have? The optimizer could prefer a merge join
or a hash join over a nested-loops join, not necessarily a bad thing,
as it means that it reads the index only once.

Without seeing the plan or knowing the table and indexes, it's difficult
to say much more.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
AddThis Social Bookmark Button