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)
 Execution Plans - Step By Step

Author  Topic 

Lijo Cheeran Joseph
Posting Yak Master

123 Posts

Posted - 2010-02-16 : 08:34:44
Hi Team,

I am working with SQL Server 2005. I am trying to learn the art of ‘query execution plan analysis’ to find bottlenecks. Though I am good in writing SQL, I have never looked execution plans. Can you please suggest some articles that give STEP-BY_STEP guidelines for learning execution plan. It will be good if it is an article that starts with simple examples and build progressively.

Thanks
Lijo Cheeran Joseph


Kristen
Test

22859 Posts

Posted - 2010-02-16 : 09:03:14
Here's my two-pennywortrh:

Comment in the SHOWPLAN to see the Query Plan, OR the STATISTICS to see the logical stats

-- SET SHOWPLAN_TEXT ON
GO
-- SET STATISTICS IO ON; SET STATISTICS TIME ON

... *** YOUR QUERY HERE *** ...

SET STATISTICS IO OFF; SET STATISTICS TIME OFF
GO
SET SHOWPLAN_TEXT OFF
GO

Check the Logical I/O and number of scans first (ignore the physical I/O - that will change each time you run the query, the Logical I/O is therefore more representative)

You can re-check the Logical I/O and Scans when you make a change to see if it has improved things

Focus on any table that has large numbers of scans and/or logical I/O

NOTE: Using Statistics actually RUNS the query - be careful if using an UPDATE or DELETE etc., or use BEGIN TRANSACTION ... ROLLBACK so you can repeat it any number of times.

Then run the SHOWPLAN (this will NOT actually run the query)

Look for Table Scan, Index Scan or Index Seek

o Table Scan and Index Scan "thumb through all the pages" looking for the entry.
o Index Seek goes straight to the entry(ies) - and thus is better.

Also check that the Index being used for each part of the query is the one you expect. In particular, the Clustered Index (usually the Primary Key) is used when there is no other choice.

Examples:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=138788#542010
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=138385#540109
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=83395#306129
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=84347#310486
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2010-02-16 : 09:19:55
Two resources for execution plans.

The first and most comprehensive is Grant's book: http://www.sqlservercentral.com/articles/books/65831/ The eBook is free.
The second, far more of a summary, is my blog: http://sqlinthewild.co.za/index.php/category/sql-server/execution-plans/

I wouldn't syuggest ShowPlan Text on SQL 2005 or above. There's much more information available in the graphical plan and it;s a lot easier to read. You can toggel the 'include actual execution plan' button in Management Studio's toolbar.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-02-16 : 10:18:09
Personally I think there is too much information in the Graphical Plan for a first-cut, and its particularly hard to wade through for an SProc that starts off with a few SET NOCOUNT type statements and a few Assignments etc. Each gets its own tiny graphical window with scroll bars all around - or is there a way to explode a single part out of the Execution Plan tab that I haven't found yet?

IMHO First priority is:

1) Get a baseline Logic I/Os and Scans to know if enhancements improve things or not (percentages are not much use for basep-line comparison)
1) Is the query using Table or Index Scans, or is it using Index Seek.
2) Is it using the expected index (or, indeed "No index" )

The graphical query is laid out in a way that looks extremely impressive, but it is hard to see those things without mouse waving to get the mouse-over yellow data-tips, and Horizontal / Vertical scrolling; plus all the Sorts / Filters etc. make it even more spread out

The Sorts / Filters may, themselves, indicate inefficiency (and thus can be helpful of course), but mostly that is easily spotted when No Index, or an Unexpected-index, is seen to be used.

But that's just how I go about it ...
Go to Top of Page

Lijo Cheeran Joseph
Posting Yak Master

123 Posts

Posted - 2010-02-21 : 09:08:40
quote:


The first and most comprehensive is Grant's book: http://www.sqlservercentral.com/articles/books/65831/ The eBook is free.
The second, far more of a summary, is my blog: http://sqlinthewild.co.za/index.php/category/sql-server/execution-plans/




Thank you.. Let me go through the articles and get some start. Then I will be in a better position position to understand other tips given in this post..

Thanks
Lijo
Go to Top of Page

Lijo Cheeran Joseph
Posting Yak Master

123 Posts

Posted - 2010-02-21 : 09:08:40
quote:


The first and most comprehensive is Grant's book: http://www.sqlservercentral.com/articles/books/65831/ The eBook is free.
The second, far more of a summary, is my blog: http://sqlinthewild.co.za/index.php/category/sql-server/execution-plans/




Thank you.. Let me go through the articles and get some start. Then I will be in a better position position to understand other tips given in this post..

Thanks
Lijo
Go to Top of Page
   

- Advertisement -