join the MSSQLTips community

Today's Site Sponsor


 

SQL Compare quickly and easily compares and synchronizes SQL Server database schemas
 



Speed up SQL script deployment

Query Plans in SQL Server 2000 vs SQL Server 2005

Written By: Jeremy Kadlec -- 9/21/2006 -- read/post comments -- print -- Bookmark and Share

Rating: (not rated yet) Rate

Problem
With system performance on the mind of most business and technical teams (management, DBA, developer, network admin), determining the issues is the first major challenge. So once you have determined which queries are causing issues, now comes the time to determine how to improve the performance of the query while reducing the reads and writes.  In SQL Server 2000 the primary interface was Query Analyzer's graphical interface where it was necessary to hover over the object to determine the statistics.  Does SQL Server 2005 have a more elegant approach reviewing the query plans?

Solution

SQL Server 2000 - Query Plan Options

There are three primary interfaces in SQL Server 2000 to access the query plan.  First is via the SHOWPLAN_TEXT  command, this command offers a textual view of the SQL Server optimizer's query plan.  A more advanced command is the SHOWPLAN_ALL command.  This command provides the estimated IO, CPU, row size, parallelism, etc. A final option is use the graphical query plans from Query Analyzer which can be activated by CTRL + K.  Each component of the query is broken down and the read, writes, etc. statistics are available for each.

SQL Server 2000 - Query Plan Commands

ID Command Example
1 SHOWPLAN_TEXT - Simple view of the optimizer results SET SHOWPLAN_TEXT ON
GO
SELECT TOP 1 a.au_lname AS 'AuthorLastName', a.au_fname AS 'AuthorFirstName',
t.title AS 'Title', t.pubdate AS 'PublicationDate'
FROM dbo.Authors a
INNER JOIN dbo.TitleAuthor ta
ON a.au_id = ta.au_id
INNER JOIN dbo.Titles t
ON ta.title_id = t.title_id
WHERE a.state = 'CA'
 
    Corresponding Output
 
2 SHOWPLAN_ALL - Detailed view of the optimizer results SET SHOWPLAN_ALL ON
GO
SELECT TOP 1 a.au_lname AS 'AuthorLastName', a.au_fname AS 'AuthorFirstName',
t.title AS 'Title', t.pubdate AS 'PublicationDate'
FROM dbo.Authors a
INNER JOIN dbo.TitleAuthor ta
ON a.au_id = ta.au_id
INNER JOIN dbo.Titles t
ON ta.title_id = t.title_id
WHERE a.state = 'CA'
 
    Corresponding Output
 
3 Graphical Query Plan - Graphical view of the query plan data from the SQL Server optimizer

This feature is activated by CTRL + K in Query Analyzer

SELECT TOP 1 a.au_lname AS 'AuthorLastName', a.au_fname AS 'AuthorFirstName',
t.title AS 'Title', t.pubdate AS 'PublicationDate'
FROM dbo.Authors a
INNER JOIN dbo.TitleAuthor ta
ON a.au_id = ta.au_id
INNER JOIN dbo.Titles t
ON ta.title_id = t.title_id
WHERE a.state = 'CA'
 
   
 

SQL Server 2005 - Query Plan Related Dynamic Management Views

SQL Server 2005 continues to support the SQL Server 2000 options listed above in addition to the introduction of the dynamic management views below.  These new dynamic management views support reviewing the current query plan and the associated performance metrics: 

The query below provides a fairly complete view of the query plan metrics (IO, CPU, memory, etc.) as well as the query plan in XML format based on the cached query plans in your environment.  Try this query out on your SQL Server 2005 server:

USE master;
GO
SELECT * FROM sys.dm_exec_query_stats
qs
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle
);
GO

-- Reference - SQL Server 2005 Books Online

Next Steps

  • Once you have pinpointed the queries in your environment which have plagued your performance, begin to review the query plans and begin to tune these individual queries.
  • As a best practice have 2 separate Management Studio sessions to compare the results of the original query and the query as it is being tuned.
  • Stay tuned for additional tips from MSSQLTips.com on techniques to improve query performance.
  • Check out MSSQLTips.com for a complete list of performance tuning tips such as SQL Server 2000 Query Analyzer Short Cuts.
Readers Who Read This Tip Also Read Free Live Webcast Comment or Ask Questions About This Tip


Sponsor Information
Free SQL Server performance monitoring dashboard – Idera SQL check

Try Red Gate SQL Backup Pro for smaller, more robust SQL Server backups. Download a free trial now!

We fill in the gaps... SQL Server Training, Development, Performance Tuning, SSIS and more

All SQL Server, all the time! Sign-up for the MSSQLTips newsletter!

Free white paper - Top SQL Server Backup Mistakes and How to Avoid Them


Get Our Tips Newsletter

We keep 50,000+ SQL Server professionals informed.



Idera - SQL diagnostic manager

Idera SQL diagnostic manager is an award-winning performance monitoring solution for SQL Server that provides agent-less, real-time monitoring, customizable alerting, and extensive historical reporting. SQL diagnostic manager also puts must-have troubleshooting information at the DBA’s fingertips such as worst-performing code, long-running or frequently run queries, and blocking or blocked sessions.

Download now!

More SQL Server Tools
SQL comparison toolset

SQL secure

SQL Refactor

SQL compliance manager

SQL defrag manager




Copyright (c) 2006-2010 Edgewood Solutions, LLC All rights reserved
privacy statement | disclaimer | copyright | advertise | write for mssqltips | feedback | about
Some names and products listed are the registered trademarks of their respective owners.


CareerQandA.com | MSSharePointTips.com | MSSQLTips.com