You are on page 1of 4

01/02/2012 How to troubleshoot slow-running queries on SQL Server 7.

0 or on later v
1/4 support.microsoft.com/kb/243589
ArLicle lD. 243589 - LasL Review. December 5, 2005 - Revision. 5.4
How to troubIeshoot sIow-running queries on 5QL 5erver 7.0 or on
Iater versions
1his arLicle was previously published under Q243589
1his arLicle describes how Lo handle a perormance issue LhaL applicaLions
may experience in conjuncLion wiLh MicrosoL SQL Server. slow
perormance o a speciic query or group o queries. l you are
LroubleshooLing a perormance issue, buL you have noL isolaLed Lhe
problem Lo a speciic query or small group o queries LhaL perorm slower
Lhan expecLed, see Lhe ollowing arLicle in Lhe MicrosoL Knowledge 8ase
beore you conLinue.
224587 (hLLp.//supporL.microsoL.com/kb/224587/ ) HOW 1O.
1roubleshooL ApplicaLion Perormance wiLh SQL Server
1his arLicle is based on Lhe assumpLion LhaL you have used Lhe arLicle
224587 Lo narrow down Lhe scope o Lhe problem, and LhaL you have
capLured a SQL Proiler Lrace wiLh Lhe speciic evenLs and daLa columns
LhaL are deLailed in Lhe arLicle 224587.
1uning daLabase queries can be a mulLi-aceLed endeavor. 1he ollowing
secLions discuss common iLems Lo examine when you are invesLigaLing
query perormance.
Note l you are using SQL Server 2005, use SQL Server ManagemenL
SLudio insLead o SQL Query Analyzer, and use DaLabase Fngine 1uning
Advisor insLead o Lhe lndex 1uning Wizard.
Verify the xistence of the Correct lndexes
One o Lhe irsL checks Lo perorm when you are experiencing slow query
execuLion Limes is an index analysis. l you are invesLigaLing a single query,
you can use Lhe Perform lndex AnaIysis opLion in SQL Query Analyzer, i
you have a SQL Proiler Lrace o a large workload, you can use Lhe lndex
1uning Wizard. 8oLh meLhods use Lhe SQL Server query opLimizer Lo
deLermine which indexes would be helpul or Lhe speciied queries. 1his is
a very eicienL meLhod or deLermining wheLher Lhe correcL indexes exisL in
your daLabase.
For inormaLion abouL how Lo use Lhe lndex 1uning Wizard, see Lhe "lndex
1uning Wizard" Lopic in SQL Server 7.0 8ooks Online.
l you have upgraded your applicaLion rom a previous version o SQL
Server, dierenL indexes may be more eicienL in SQL Server 7.0 because
o opLimizer and sLorage engine changes. 1he lndex 1uning Wizard helps
you Lo deLermine i a change in indexing sLraLegy would improve
perormance.
For more inormaLion abouL how Lo use DaLabase Fngine 1uning Advisor
insLead o Lhe lndex 1uning Wizard in SQL Server 2005, see Lhe ollowing
Lopics in SQL Server 2005 8ooks Online.
Dierences beLween DaLabase Fngine 1uning Advisor and lndex
1uning Wizard
DaLabase Fngine 1uning Advisor LuLorial
Remove AII Query, TabIe, and Join Hints
HinLs override query opLimizaLion and can prevenL Lhe query opLimizer
rom choosing Lhe asLesL execuLion plan. 8ecause o opLimizer changes,
hinLs LhaL improved perormance in earlier versions o SQL Server may
have no eecL or may acLually adversely aecL perormance in SQL Server
7.0. AddiLionally, join hinLs can cause perormance degradaLion based on
Lhe ollowing reasons.
SUMMAR
01/02/2012 How to troubleshoot slow-running queries on SQL Server 7.0 or on later v
2/4 support.microsoft.com/kb/243589
Lhe ollowing reasons.
Join hinLs prevenL an ad hoc query rom being eligible or auLo-
parameLerizaLion and caching o Lhe query plan.
When you use a join hinL, iL implies LhaL you wanL Lo orce Lhe join
order or all Lables in Lhe query, even i Lhose joins do noL expliciLly
use a hinL.
l Lhe query LhaL you are analyzing includes any hinLs, remove Lhem, and
Lhen re-evaluaLe Lhe perormance.
Eamine the Eecution Plan
ALer you conirm LhaL Lhe correcL indexes exisL, and LhaL no hinLs are
resLricLing Lhe opLimizer's abiliLy Lo generaLe an eicienL plan, you can
examine Lhe query execuLion plan. ou can use any o Lhe ollowing
meLhods Lo view Lhe execuLion plan or a query.
SQL Profiler
l you capLured Lhe MISC:Eecution Plan evenL in SQL Proiler, iL will
occur immediaLely beore Lhe StmtCompleted evenL or Lhe query
or Lhe parLicular sysLem process lD (SPlD).
SQL Quer Analer: Graphical Shoplan
WiLh Lhe query selecLed in Lhe query window, click Lhe Quer menu,
and Lhen click Displa Estimated Eecution Plan.
NOTE. l Lhe sLored procedure or baLch creaLes and reerences
Lemporary Lables, you musL use a SF1 S1A1lS1lCS PROFlLF ON
sLaLemenL or expliciLly creaLe Lhe Lemporary Lables beore you
display Lhe execuLion plan.
SHOWPLAN_ALL and SHOWPLAN_TEXT
1o receive a LexL version o Lhe esLimaLed execuLion plan, you can
use Lhe SF1 SHOWPLAN_ALL and SF1 SHOWPLAN_1FX1 opLions.
See Lhe "SF1 SHOWPLAN_ALL (1-SQL)" and "SF1 SHOWPLAN_1FX1
(1-SQL)" Lopics in SQL Server 7.0 8ooks Online or more deLails.
NOTE. l Lhe sLored procedure or baLch creaLes and reerences
Lemporary Lables, you musL use Lhe SF1 S1A1lS1lCS PROFlLF ON
opLion or expliciLly creaLe Lhe Lemporary Lables beore displaying
Lhe execuLion plan.
STATISTICS PROFILE
When you are displaying Lhe esLimaLed execuLion plan, eiLher
graphically or by using SHOWPLAN, Lhe query is noL acLually
execuLed. 1hereore, i you creaLe Lemporary Lables in a baLch or a
sLored procedure, you cannoL display Lhe esLimaLed execuLion plans
because Lhe Lemporary Lables will noL exisL. S1A1lS1lCS PROFlLF
execuLes Lhe query irsL, and Lhen displays Lhe acLual execuLion plan.
See Lhe "SF1 S1A1lS1lCS PROFlLF (1-SQL)" Lopic in SQL Server 7.0
8ooks Online or more deLails. When iL is running in SQL Query
Analyzer, Lhis appears in graphical ormaL on Lhe Eecution Plan
Lab in Lhe resulLs pane.
For more inormaLion abouL how Lo display Lhe esLimaLed execuLion plan in
SQL Server 2005, see Lhe "How Lo display Lhe esLimaLed execuLion plan"
Lopic in SQL Server 2005 8ooks Online.
Eamine the Shoplan Output
Showplan ouLpuL provides a loL o inormaLion abouL Lhe execuLion plan
LhaL SQL Server is using or a parLicular query. 1he deLails o Lhe
inormaLion and evenLs LhaL are generaLed are discussed in deLail in Lhe
"OpLimizing DaLabase Perormance" chapLer o SQL Server 7.0 8ooks
Online. 1he ollowing are some basic aspecLs o Lhe execuLion plan LhaL
you can view Lo deLermine wheLher you are using Lhe besL plan.
01/02/2012 How to troubleshoot slow-running queries on SQL Server 7.0 or on later v
3/4 support.microsoft.com/kb/243589
CorrecL lndex Usage
1he showplan ouLpuL displays each Lable LhaL is involved in Lhe
query and Lhe access paLh LhaL is used Lo obLain daLa rom iL. WiLh
graphical showplan, move Lhe poinLer over a Lable Lo see Lhe deLails
or each Lable. l an index is in use, you see "lndex Seek", i an index
is noL in use, you see eiLher "1able Scan" or a heap or "ClusLered
lndex Scan" or a Lable LhaL has a clusLered index. "ClusLered lndex
Scan" indicaLes LhaL Lhe Lable is being scanned Lhrough Lhe
clusLered index, no LhaL Lhe clusLered index is being used Lo direcLly
access individual rows.
l you deLermine LhaL a useul index exisLs and iL is noL being used
or Lhe query, you can Lry orcing Lhe index by using an index hinL.
See Lhe "FROM (1-SQL)" Lopic in SQL Server 8ooks Online or more
deLails abouL index hinLs.
CorrecL Join Order
1he showplan ouLpuL indicaLes in whaL order Lables LhaL are
involved in a query are being joined. For nesLed loop joins, Lhe
upper Lable LhaL is lisLed is Lhe ouLer Lable and iL should be Lhe
smaller o Lhe Lwo Lables. For hash joins, Lhe upper Lable becomes
Lhe build inpuL and should also be Lhe smaller o Lhe Lwo Lables.
However, noLe LhaL Lhe order is less criLical because Lhe query
processor can reverse build and probe inpuLs aL run Lime i iL inds
LhaL Lhe opLimizer made a wrong decision. ou can deLermine which
Lable reLurns ewer rows by checking Lhe Row CounL esLimaLes in Lhe
showplan ouLpuL.
l you deLermine LhaL Lhe query may beneiL rom a dierenL join
order, you can Lry orcing Lhe join order wiLh a join hinL. See Lhe
"FROM (1-SQL)" Lopic in SQL Server 7.0 8ooks Online or more
deLails abouL join hinLs.
NOT. Using a join hinL in a large query impliciLly orces Lhe join
order or Lhe oLher Lables in Lhe query as i FORCFPLAN was seL.
CorrecL Join 1ype
SQL Server uses nesLed loop, hash, and merge joins. l a slow-
perorming query is using one join Lechnique over anoLher, you can
Lry orcing a dierenL join Lype. For example, i a query is using a
hash join, you can orce a nesLed loops join by using Lhe LOOP join
hinL. See Lhe "FROM (1-SQL)" Lopic in SQL Server 7.0 8ooks Online
or more deLails on join hinLs.
NOT. Using a join hinL in a large query impliciLly orces Lhe join
Lype or Lhe oLher Lables in Lhe query as i FORCFPLAN was seL.
Parallel FxecuLion
l you are using a mulLiprocessor compuLer, you can also invesLigaLe
wheLher a parallel plan is in use. l parallelism is in use, you see a
PARALLLl5M (CaLher SLreams) evenL. l a parLicular query is slow
when iL is using a parallel plan, you can Lry orcing a non-parallel
plan by using Lhe OP1lON (MAXDOP ) hinL. See Lhe "SFLFC1 (1-
SQL)" Lopic in SQL Server 7.0 8ooks Online or more deLails.
For more inormaLion abouL how Lo use Showplan execuLion plan ouLpuL in
SQL Server 2005, see Lhe ollowing Lopics in SQL Server 2005 8ooks Online.
How Lo save an execuLion plan in XML ormaL
XML Showplans
Showplan securiLy
CAUTlON. 8ecause Lhe query opLimizer Lypically selecLs Lhe besL execuLion
plan or a query, MicrosoL recommends LhaL you use join hinLs, query
hinLs, and Lable hinLs only as a lasL resorL, and only i you are an
experienced daLabase adminisLraLors.
01/02/2012 How to troubleshoot slow-running queries on SQL Server 7.0 or on later v
4/4 support.microsoft.com/kb/243589
experienced daLabase adminisLraLors.
1he ollowing Lopics in SQL Server 7.0 8ooks Online provide inormaLion
abouL opLimizing queries.
"OpLimizing ApplicaLion Perormance Using FicienL DaLa ReLrieval"
"Query 1uning"
"Query 1uning RecommendaLions"
"1ransacL-SQL 1ips"
APPLIES TO
Kewords: kbhowLomasLer K8243589

RFFFRFNCFS

You might also like