Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 4

How to troubleshoot a slow running query in SQL Server

08 November,2012 by Jack HYPERLINK "http://www.sqlserver-dba.com/contact-me/"Vamvas


HYPERLINK "http://www.sqlserver-dba.com/contact-me/"
 “ I have a slow running query , what steps can I take to speed up the query and achieve an
optimised execution plan?” .
Troubleshooting database server performance is an in-depth topic. A methodical and repeatable
approach solving the root cause is the perfect situation for any DBA.   There are different approaches to
SQL Server Tuning. In reality, the root cause may not be a SQL query issue, it may be SAN  , SQL
Server configuration issue or workload has suddenly increased.
But of course, you’re under pressure and the query is causing delays for the end users. The steps below
expand on earlier post :SQL Server Rapid Tuning, providing a very simple emergency approach.
The steps below are straightforward approaches to troubleshooting sub optimal SQL Execution Plans.
Follow the steps and repeat as necessary.
Step 1)       Statistics – Is Auto HYPERLINK "http://www.sqlserver-dba.com/2012/08/sql-server-
auto-create-statistics.html"Create HYPERLINK "http://www.sqlserver-dba.com/2012/08/sql-
server-auto-create-statistics.html" and Update Statistics Enabled? If Auto Create statistics is
disabled, this may indicate out of date statistics. If Disabled, than proceed to Update Statistics use
sp_updatestats HYPERLINK "http://www.sqlserver-dba.com/2011/05/sql-server-sp_updatestats-and-
update-statistics.html" to update all statistics on the database. Run the query and check if any
improvement.
Step 2)       If Auto Create and Update Statistics is Enabled, Identify the longest running queries or
highest impact queries.( If the queries have high CPU usage go to step 6). Long duration and highest IO
should be a priority.
Step 3)       Place every query in the SSMS and analyse the execution plan. First – check for tables
or index scans.  If large table \ index scans are occurring – progress with Query Analysis.  The Query
Analysis should ask questions such as: Are all JOINS valid ? Are the JOINS returning excessive data?
Search argument validity?   Functions in predicate? 
Step 4)       If no table or index scans exist and the query is complex, for example a large transaction
managing a booking process – check for: excessive joins, temp tables, DDL changes, sub – queries ,
no set based approach to writing the queries. Review the query, break it down into smaller parts or
analyse the JOINS to invoke a new execution plan, ensuring similar transaction integrity is retained
Step 5)       if the query is simple and no no index \table scans exists and executing in SSMS
responds with acceptable performance - analyse the Application and how it processes the
resultset.   Ask the right question a) are just relevant results returned?  Talk to the application
developers.
Step 6)       If the query is no faster in SSMS, more complex query tuning is required,  research
other methods or contact a performance tuning expert
Step 7)       if in Step 2 you identified queries with high CPU usage, analyse in SSMS for Hash Joins,
Sorts, Filters.  If any of these exists, progress with Query Analysis.
Step 8)       Repeat until the problem disappears.
SQL Server – AUTO CREATE STATISTICS

15 August, 2012 by Jack HYPERLINK "http://www.sqlserver-dba.com/contact-me/"Vamvas


HYPERLINK "http://www.sqlserver-dba.com/contact-me/"

Question: Usually when I look at the statistics information on an index, SQL statistics  are available. 
I was troubleshooting a query performance issue today – and noticed no statistics under heavily used
objects. What could be causing this problem, as I understand the DB engine maintains the statistics
about the key value distribution, and uses the statistics for query plan compilation?
 

Answer: Check the status of AUTO CREATE STATISTICS. If it’s OFF, then most likely someone
has turned it off.   When AUTO CREATE STATISTICS is set at OFF, a manual process must be set up
to create statistics.
Normally I have AUTO CREATE STATISTICS at ON.  This means the SQL Query Optimizer creates
the statistics on individual columns for cardinality improvement.
View HYPERLINK "http://www.sqlserver-dba.com/2012/08/sql-server-auto-create-statistics.html" source

Print HYPERLINK "http://www.sqlserver-dba.com/2012/08/sql-server-auto-create-statistics.html"?

1.--turn on AUTO CREATE STATISTICS

2. ALTER DATABASE MyDBName SET AUTO_CREATE_STATISTICS ON

3. 

4.--turn off AUTO CREATE STATISTICS

5. ALTER DATABASE MyDBName SET AUTO_CREATE_STATISTICS OFF

SQL Server Query Optimizer and Statistics

16 May, 2011 by Jack HYPERLINK "http://www.sqlserver-dba.com/contact-me/"Vamvas


HYPERLINK "http://www.sqlserver-dba.com/contact-me/"
Maintaining statistics is important for SQL Server performance. The SQL Server query optimizer uses
statistics to calculate \ estimate the number of rows (also known as cardinality) for a sql query.
Cardinality influences the SQL Server query plan. A practical example is the choice between an
efficient Index Seek and an inefficient Index Scan.
Is your index missing statistics ?
The SQL Server query optimizer is a cost-based optimizer. Two factors determine the estimated cost:
1) The query plan has multiple levels processing rows. The total number of  rows is the plan cardinality
2) Operators used in the query defining the cost model of the algorithm
The cardinality is an input parameter for the cost model. Maintaining cardinality information – will
create improved estimates of the cost – which should lead to quicker query plans.
The Optimizer considers multiple execution plans – each execution plan has an associated cost. The
Optimizer analyses the plans and decides which one to use. In a complex SQL Statements – which may
have hundreds of plans – the Optimizer defines a minimum possible cost – and chooses a plan close to
the criteria.
Statistics are stored for tables in a statistics object. The object is either on a column list or an index. A
statistics object is comprised of:
1) Metadata header
2) Histogram with a value distribution in the first key column
3) Density vector for cross-column correlation
 The Engine computes cardinality with the data represented in the statistics object
 DBCC SHOW_STATISTICS displays current query optimization statistics. A example output:

 
 
sys.dm_exec_query_optimizer_info – displays information about the SQL Server query optimizer.
Use it for workload tuning process
   
It is possible to force the Optimizer to use a plan. Use USE PLAN to pass a query plan to the
Optimizer. USE PLAN will force the plan to use and override the Optimizer. Typically used in an
upgrade , where the later version of SQL Server optimizer does not return query plans as expected.
 Maintaining statistics is critical to accurate execution plans. If the data in the statistics objects is
inaccurate the Optimizer will use an inefficient execution plan. DBCC SHOW_STATISTICS,
sys.dm_exec_query_optimizer_info, and USE PLAN are three ways of exploring and maximizing the
Optimizer

You might also like