Sie sind auf Seite 1von 5

SQL OPTIMIZATION TIPS

Here are some Sql optimization techniques (best practices) to write optimized query in sql
server, which are listed below:-
Don't use "SELECT*" in a SQL query: - Unnecessary columns may get fetched
that will add expense to the data retrieval time. So select only columns that
you used. Selecting unnecessary columns in a Select query adds overhead to
the actual query and thus increases the execution time.
SELECT * FROM TABLE instead use SELECT COL1 COL!"#FROM TABLE
A$oi% unne&essary ta'(es in )oin &on%itions: - Including unnecessary tables in join
conditions forces the database engine to retrieve and fetch unnecessary data
and increases the query execution time.
Try to a$oi% )oinin* 'et+een t+o ty,es o- &o(u.ns:-When joining between two
columns of different data types, one of the columns must be converted to the
type of the other. he column whose type is lower is the one that is
converted. If you are joining tables with incompatible types, one of them can
use an index, but the query optimi!er
"annot choose an index on the column that it converts. #or example joining
between to data type I$ and #loat as integer column converted to float and
it does not use index.
Try not to use CO/0T1*2 to o'tain t3e re&or% &ount in a ta'(e:- o count total
number of records in a table or dynamic query use "%U$&"olumn' instead
"%U$&(' specially those column which have index or primary )ey which has
default clustered index in it. "%U$ &(' uses full table scan and does not get
benefit of indexes. If no any condition is needed to chec) in your where
clause you can use*+
S,-," .%WS #.%/ S0SI$1,2,S W3,., I14%56,"7I1 &895-,:' 9$1
I$1I1;<.
4n&(u%e SET 0OCO/0T O0 state.ent:-With every S,-," and 1/- statement,
the S=- server returns a message that indicates the number of affected rows
by that statement. his information is mostly helpful in debugging the code,
but it useless after that. 5y setting S, $%"%U$ %$, we can disable the
SQL OPTIMIZATION TIPS
feature of returning this extra information. #or stored >rocedures that contain
several statements or contain ransact+S=- loops, setting S, $%"%U$ to
%$ can provide a significant performance boost because networ) traffic is
generally reduced.
Try to a$oi% %yna.i& SQL: - Unless really required, try to avoid the use of
dynamic S=- because* 1ynamic S=- is hard to debug and troubleshoot. If the
user provides the input to the dynamic S=-, then there is possibility of S=-
injection attac)s.
Mini.i5e use o- Te.,orary Ta'(e 16ta'(e2 Ta'(e 7aria'(e 18ta'(e2Su'queryCTE:-
here are mainly four ways by which we can hold intermediate data
temporarily.
?@TEM9ORAR: TABLE: It is also )nown as A table, it is resided in tempdb
system database. It uses indexes clustered and nonclustered both. 0ou can
create temporary table as
".,9, 95-, A,/> &"%-? 1990>,?, "%-< 1990>,<BBB..'
Use this table when we need to hold large rows data, it is faster because
index can be apply on it.
<@TABLE 7AR4ABLE: It starts with C declare as variable as
1,"-9., C95-, 95-, &"%-? 1990>,?, "%-< 1990>,<B.'
Use it when we need to hold small data. It reside in memory but don, t uses
indexes."lustured indexes can be created as it support column with primary
)ey.
D@S/BQ/ER:: It just hold complex query and can be nested syntax is as
follows.
S,-," ( #.%/ &S,-," "%-?, "%-<, +++++#.%/ 95-,' 9S 2 W3,., some
condition. It is faster than above two methods as it does not need data to
insert.
E@COMMO0 TABLE E;9RESS4O0: "ommonly )nown as ",, as it hold complex
query and it can be nested as subquery.he syntax is
WI3 ",7$ame &"%-?, "%-<' 9S &S,-," "%-?, "%-< #.%/ 95-,'
SQL OPTIMIZATION TIPS
S,-," ( #.%/ ",7$9/,
Where, ",7$9/, is name of ",.
/SE SC<EMA 0AME =4T< OB>ECT 0AME: - 9lways use object name with their
schema name. 1atabase objects are table, stored procedure, function, trigger
and views etc. If database schema name is dbo then use %'o# O')e&t instead
of object.
S,-," ( #.%/ F15%G.F95-,G I$S,91 S,-," ( #.%/ F95-,G. If we not
specified schema name it searches on all schemas which slow down query.
9lways use FdboG.Fsp7name or fn7nameG when calling from "A code.
DO0 T /SE STORED 9ROCED/RE =4T< S9?:- 9s sp7 stored procedure naming
convention used by system stored procedure. So don:t use this convention
instead use your own. Such stored procedure first search on master database
and then current session database cause extra overhead and increase
execution time.
TO C<EC@ E;4STE0CE OF RECORD: - o chec) existence of record, use S,-,"
? #.%/ 95-, or S-," "%-? #.%/ 95-, instead of S,-," ( #.%/ 95-,.
E;EC/TE D:A0AM4C Q/ER:: - o execute dynamic query we can use whether
sp7executesql stored procedure or ,2,"U, statement. #irst sp7executesql
support parameter and can be reused but second one cannot be and need to
hold on certain table to display result.
=<E0 E;TRACT40A DATA: ry to avoid looping while you have to select data
and need to display accordingly. If we have lots of record on table, and we
loop through table data for some condition to chec), it slows down query,
,xample W3I-,.
DO0T /SE SCALAR F/0CT4O0 40 SELECT STATEME0T:- If possible do not use
scalar function in select statement as it have to loop through each rows and
process that function. It slow down query performance. Instead use it to set
SQL OPTIMIZATION TIPS
value on single variable. Write inline query in select if it is simple, if not there
is no any option alternative to it.
TABLE COL/M0 S4BE:- able column si!e be as small as possible, if we have to
hold only D character example 8abc: always its si!e be less if possible
nvarchar&?H' not nvarchar&<IJ'.
SET STAT4ST4CS T4ME O0: - o chec) how much times consumed by your
stored procedure, include it in top and analy!e its time.
/SE 40DE;ES: - Indexes increases the query performance. Some type of
indexes are
?@"lustered Index
<@$on "lustered Index
D@"omposite Index
E@"overed Index
Use query execution plan to find missing index suggestion or database tuning
advisor or use your brain.
here are ,stimate execution plan and actual execution plan.
,2," Fdbo.G.Fusp7Ket$ameG ?, < ,:en+US: select this and write clic) from
context menu select 1isplay estimated execution plan or go to /enu at top
select query and estimated plan and see what happen with your query and
which section of your query is costly.

SQL OPTIMIZATION TIPS

Das könnte Ihnen auch gefallen