0% found this document useful (0 votes)
7 views7 pages

SQL - Improvement Guide

SQL - Improvement Guide
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

SQL - Improvement Guide

SQL - Improvement Guide
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Improvement Guide

Improve Stored Procedure Performance


Improvement Guide
Revision History
Author Revision Number Date
TCSASSEMBER 1.0 22/06/2014
Improvement Guide TopCoder, Inc. 2014 !"e 1 o# $
Improvement Guide
Improvement In%truction%
1. &r"!ni'!tion o# Su(mi%%ion
2. Improvement Tip%
). Re%ource Cont!ct *i%t
Improvement Guide TopCoder, Inc. 2014 !"e 2 o# $
Improvement Guide
1. Improvement Instructions
2. r!ani"ation of Submission
T+e %u(mi%%ion #o,,o-% director. %tructure provided (e,o-/
###R$AD%$###.t&t
Re!d #ir%t #i,e.
S'( ) Improvement [Link]
T+i% "uide t+!t .ou !re re!din"
S'($&ecutionP*an$&[Link]
S0* Server 2001 E2ecution ,!n e2port -+o% e%tim!te% !re (!%ed on %m!,, d!t!3#i,e -e +!d.
spSearchAna*yses+i&ed.s,*
Stored rocedure -it+ minor #i2e% unre,!ted to improvement o# per#orm!nce
-. Improvement .ips
Since t+ere i% not ! "ood te%t3d!t! c!%e to te%t m. !%%umption% 4%ince d!t! i% needed (. S0* E2ecution
,!nner to m!5e prediction% !(out +o- ,on" -i,, e2ecution o# %peci#ic t!%5% ,!%t6, +ere !re coup,e o# +int%
to t+e c,ient t+!t c!n +e,p t+em %o,ve t+eir pro(,em.
0.
T+e provided procedure +!% mi%t!5e% in it -+en !pp,ied to e2i%tin" code !nd -i,, not -or5 -it+ -+!t -e
-ere provided. I7ve m!de %ome minor correction% to #i2 t+i% !nd t+e %cript i% ,oc!ted/
[Link]
1.
Inde2e% !re ver. import!nt 3 t+ere i% po%%i(i,it. t+!t %omet+in" i% not inde2ed !nd c!u%e% t+e e2ecution to
(e %,o-.
2.
T+i% ,oo5% %,o-/
AND (@invoice IS NU !" #$IS%S(S##&% ('( F"!) Invoice *+#"#
,Invoice-.,.ro/ect- 0 ,.ro/ect-.,.ro/ect- AND
,Invoice-.,&lient- 0 ,.ro/ect-.,&lient- AND
,Invoice-.,a1ID- 0 ,.ro/ect-.,a1ID- AND
,Invoice-.,Invoice- I2# 343 5 @invoice 5 343((
AND (@method IS NU !" @method 0 ,"#.SA).#ANA6SIS'-.,Speci7ic)ethod-(
3 It pro(!(,. -on7t u%e !n. inde2 (ec!u%e o# t+e &R !nd m!.(e t+e d.n!mic 8invoice !nd 8met+od !nd
it m!. (e runnin" t+e SE*ECT %t!tement repe!ted,. intern!,,. -it+ t+e 9oin. It i% !t ,e!%t comp,ic!ted !nd
,i5e,. to con#u%e .our ,o"ic no- or in #uture modi#ic!tion%. T+!t i%, (ot+ .our o-n de%i"n, !nd t+e ,o"ic o#
t+e :uer. p,!nner.
3 ;ou !re puttin" t+i% ,o"ic o# @varia1le IS NU into t+e :uer. it%e,#. I -ou,d (ui,d t+e :uer. d.n!mic!,,.,
c+ec5in" -+et+er @varia1le IS NU out%ide o# t+e :uer..
Improvement Guide TopCoder, Inc. 2014 !"e ) o# $
Improvement Guide
).
A,, ,i5e% -it+ pre#i2ed < !re %,o-.
4.
=+en e2ecutin" t+e procedure/
exec spSearchAnalyses
@pageNumber=1,@pageSize=10,@sortColumn=N'SampleDate',@sortAscening=N'N',@user
name=N'as!as!@test"com',@is#ecent=N'$',@%ey&or=N'((,@sampleDescription=N'((
,@sample)=N'((,@&or%*rer=N'((,@in+oice=N'((,@location)=N'((,@metho=N'((,@s
ampleDateStart=N'((,@sampleDate,n=N'((
-*
;ou c!n ri"+t c,ic5 in%ide t+e :uer. -indo- o# -+ere .ou !re e2ecutin" t+e procedure !nd c+o%e
Inc,ude Actu!, E2ecution ,!n !nd t+!n >i%p,!. E%tim!ted E2ecution ,!n/
Improvement Guide TopCoder, Inc. 2014 !"e 4 o# $
Improvement Guide
=+en .ou "et t+e e2ecution p,!n, .ou c!n +over over individu!, o(9ect%. !. !ttention to ?E%tim!ted
Su(tree Co%t@
A.
;ou c!n u%e t+i% :uer. to e2ecute t+e procedure !nd to "et co%t o# e2ecution p,!n in t!(,e #orm/
S,. S/*01(AN2A(( *N
-*
33 45.*N($ &ill not exec store proc
S,. 45.*N($ *N
-*
exec spSearchAnalyses
@pageNumber=1,@pageSize=10,@sortColumn=N'SampleDate',@sortAscening=N'N',@user
name=N'as!as!@test"com',@is#ecent=N'$',@%ey&or=N'((,@sampleDescription=N'((
,@sample)=N'((,@&or%*rer=N'((,@in+oice=N'((,@location)=N'((,@metho=N'((,@s
ampleDateStart=N'((,@sampleDate,n=N'((
-*
S,. 45.*N($ *44
-*
S,. S/*01(AN2A(( *44
-*
It -i,, ,oo5 ,i5e t+i% once e2ecuted 4p!. !ttention to t+e co,umn Tot!,Su(treCo%t6/
Improvement Guide TopCoder, Inc. 2014 !"e A o# $
Improvement Guide
And, !"!in, ! 5e. t+in" to c!,, out +ere i% t+!t t+e%e co%t% 4e%tim!ted or ot+er-i%e6 !re (!%ed on S0*
ServerB% 5no-,ed"e o# t+e %i'e o# .our t!(,e% !% -e,, !% t+e c!rdin!,it. !nd di%tri(ution o# .our d!t!. &r, in
ot+er -ord%, t+e%e co%t% !re (!%ed upon %t!ti%tic% !(out .our d!t!. T+[Link] not, t+ere#ore, %omet+in"
Ct!n"i(,eB ,i5e t+e num(er o# mi,,i%econd% !%%oci!ted -it+ !n oper!tion. A% %uc+, t+e (e%t -!. to t+in5 o#
t+em i% t+!t ,o-er num(er% !re (etter D un,e%% .ou -!nt to tr. !nd "et into %ome o# t+e nitt.3"ritt. det!i,%
!(out +o- t+e%e co%t% !re c!,cu,!ted 4-+ic+, !"!in, i% propriet!r. in#orm!tion or p!rt o# S0* ServerB%
C%ecret %!uceB6.
=it+ t+!t %!id, t+ereB% %ti,, ! -!. to C#r!meB t+e%e co%t% D to provide !n ide! o# -+!t co%t% rou"+,. me!n in
t+e Cre!,B -or,d.
.//-. Co%t% o# .00) !re !(out !% optimi'ed !% .ouBre "oin" to "et -+en inter!ctin" -it+ t+e
%tor!"e en"ine 4e2ecutin" %ome #unction% or oper!tion% c!n/-i,, come in !t c+e!per co%t%, (ut IBm
t!,5in" +ere !(out #u,,3(,o-n d!t!3retriev!, oper!tion%6.
./-. &(viou%,., co%t% o# .0) !re ! #u,, order o# m!"nitude "re!ter t+!n %omet+in" -it+ ! co%t o# .
00) D (ut even t+e%e :uerie% !re [Link]!,,. "oin" to (e EER; e##icient !nd :uic5 D e2ecutin" in
,e%% t+!n ! %econd in t+e v!%t m!9orit. o# c!%e%.
0uerie% -it+ ! co%t o# 1 !renBt e2!ct,. u",. or p!in#u,, 4nece%%!ri,.6 !nd -i,, [Link]!,,. t!5e !
%econd or ,e%% to e2ecute. T+[Link] not (urnin" up ,ot% o# re%ource%, (ut t+[Link] !,%o [Link]!,,. not
!% optimi'ed !% t+e. cou,d (e 4or t+e. !re optimi'ed D (ut t+[Link] pu,,in" (!c5 +u"e !mount% o#
d!t! or #i,terin" !"!in%t ver. ,!r"e t!(,e%6.
0. 0uerie% -it+ ! co%t "re!ter t+!n A, (. de#!u,t, -i,, (e e2ecuted -it+ ! p!r!,,e, p,!n D me!nin"
t+!t S0* Server %ee% t+e%e :uerie% !% (ein" ,!r"e enou"+ to t+ro- mu,tip,e
proce%%or%/core%/t+e!d%3o#3e2ecution !t D in order to %peed up e2ecution. And, i# .ouBve "ot !
Improvement Guide TopCoder, Inc. 2014 !"e 6 o# $
Improvement Guide
-e( %ite t+!tB% #irin" o## ! :uer. -it+ ! co%t o# A or more per ever. p!"e ,o!d, #or e2!mp,e, .ouB,,
pro(!(,. notice t+!t t+e p!"e C#ee,%B ! (it %,u""i%+ ,o!din" D m!.(e (. ! %econd or t-o D !%
comp!red to ! p!"e t+!t -ou,d C%prin" upB i# it -!% runnin" ! :uer. -it+ ! co%t o#, %!., .2 or ,o-er.
So, in ot+er -ord%, :uerie% up in t+i% r!n"e %t!rt +!vin" ! notice!(,e or !ppreci!(,e Cco%tB.
2/. 0uerie% in t+i% r!n"e !re T;ICA**; "oin" to (e %omet+in" .ou c!n notice t!5in" ! %econd
or %o. 4T+ou"+, on decent +!rd-!re, t+e. c!n %ti,, end up (ein" in%t!nt!neou% !% -e,, D %o even
!t t+i% point, t+in"% %ti,, depend on ! ,ot o# #!ctor%6.
2//. 0uerie% -it+ t+i% 5ind o# co%t %+ou,d re!,,. on,. (e #or ,!r"er report% !nd in#re:uent,.
e2ecuted oper!tion%. &r, t+e. mi"+t (e %eriou% c!ndid!te% #or t+e u%e o# !ddition!, tunin" !nd
t-e!5in" 4in term% o# code !nd/or inde2e%6.
1///. 0uerie% up in t+i% r!n"e !re -+!t >BA% %t!rt to ,ovin",. c!,, C:uerie% #rom +e,,B D t+ou"+ itB%
po%%i(,e to (ump into :uerie% -it+ co%t% in t+e 10% o# t+ou%!nd% or even more D dependin" upon
t+e oper!tion% (ein" e2ecuted !nd t+e !mount o# d!t! (ein" poured over.
1. Resource 2ontact (ist
Name Resource $mai*
TCSASSEMBER
Improvement Guide TopCoder, Inc. 2014 !"e $ o# $

Common questions

Powered by AI

SQL Server evaluates the cost of a query based on statistics about the data involved, including table size, cardinality, and distribution. These statistics inform the optimizer about how much effort the query will likely incur. Queries with high costs, often exceeding values like 5 or 20, become targets for tuning because they may involve complex operations, extensive data retrieval, or inefficient logic, consuming more resources and potentially slowing down application performance .

Larger reports are linked to high-cost queries due to their inherent necessity to process and retrieve massive datasets, resulting in increased execution costs. Signs that these queries need further tuning include noticeable delays in report generation, excessive use of parallelism that strains server resources, and the potential for disrupting performance of other database operations. Such queries require a closer examination of execution plans and may benefit from indexing or breaking the report into smaller, more manageable parts .

Indexes are crucial for optimizing SQL Server stored procedures as they can significantly reduce execution times. Optimizing with appropriate indexes ensures effective data retrieval and minimizes slow execution plans caused by full table scans. In execution plans, paying attention to 'Estimated Subtree Cost' helps identify bottlenecks, as costs are estimated based on statistics about data size and distribution. Lower cost numbers generally indicate better optimization. Additionally, it's important to handle conditional logic in queries dynamically, such as moving 'IS NULL' checks outside of SQL statements to prevent performance issues .

'Estimated Subtree Cost' levels reflect the resource utilization anticipated during query execution. Lower costs are preferred because they generally correspond to more optimized queries that utilize resources efficiently, leading to faster execution times. For example, costs around .003 are highly optimized, while costs around 5 or more may indicate the need for parallel processing due to larger data operations, potentially leading to noticeable delays. The aim is to keep costs low to ensure quick and efficient data retrieval .

Keeping SQL execution costs low is crucial to ensure efficient use of database resources, help maintain fast response times, and minimize server load, directly impacting user experience and operational effectiveness. While hardware capabilities can somewhat mitigate the effects of high-cost queries by providing more processing power or memory, reliance on hardware alone is not a sustainable solution. Hardware upgrades may temporarily alleviate performance issues but do not address underlying inefficiencies in query design or execution methodology .

Using SHOWPLAN_ALL in SQL Server allows database administrators to view the estimated execution plan of stored procedures without executing them, giving insights into where potential inefficiencies might arise based on theoretical execution paths and resource usage. This tool helps in understanding the anticipated performance impacts, including evaluating the 'TotalSubtreeCost', and provides a means to adjust queries and test optimizations iteratively without affecting production workloads .

Using the 'LIKE' operator with a prefixed '%' wildcard in SQL queries leads to performance pitfalls such as table scans, as indexes cannot be used to find text starting in the middle. This results in slow query execution, especially when searching large datasets, because every row in the table must be checked for a match. Designing queries such that the '%' wildcard appears at the end can significantly improve performance by allowing index use .

Execution costs above 200 are indicative of significant resource demands, often linked to large data operations or inefficient query designs. These high costs can lead to undesirable latency in application responsiveness. To address such costs, strategies include query optimization through indexing, refining query logic, or restructuring the data. Additionally, breaking down complex queries into simpler sub-operations or scheduling them for off-peak execution times may help distribute resource demands more effectively .

Including the 'Actual Execution Plan' in SQL Server executions allows for a detailed analysis of how a query is executed in practice, highlighting inefficiencies such as missing indexes or bottlenecks in the executed operations. The plan shows the actual resource costs and the path the query optimizer chooses, shedding light on parts of the procedure that need optimization. This facilitates targeted improvements by comparing the actual performance against expected outcomes and making necessary adjustments .

Stored procedures with dynamic query components can face performance challenges because the SQL Execution Planner may not efficiently utilize indexes due to unpredictable query structures. Dynamic queries often include elements like '@invoice IS NULL OR EXISTS(...)' which prevent the effective use of indexes and can confuse the SQL logic, leading to inefficient execution plans. Moreover, the repeated internal execution of dynamic components can further degrade performance by increasing execution times unnecessarily .

You might also like