0% found this document useful (0 votes)
51 views9 pages

Query Optimization Document

The document discusses various tips and techniques for optimizing SQL queries and stored procedures. Some of the key points covered include: 1. Using temporary tables or table variables to store processed data before further operations to improve query speed. 2. Using views and stored procedures instead of complex queries to reduce network traffic. 3. Avoiding SQL Server cursors when possible as they can degrade performance compared to SELECT statements. 4. Properly ordering filters in WHERE clauses to reduce records early for better performance.

Uploaded by

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

Query Optimization Document

The document discusses various tips and techniques for optimizing SQL queries and stored procedures. Some of the key points covered include: 1. Using temporary tables or table variables to store processed data before further operations to improve query speed. 2. Using views and stored procedures instead of complex queries to reduce network traffic. 3. Avoiding SQL Server cursors when possible as they can degrade performance compared to SELECT statements. 4. Properly ordering filters in WHERE clauses to reduce records early for better performance.

Uploaded by

sunilreddy123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Sr.

No

Topic

Between

2 3

TempTable Use Storeprocedure

Cursors

Use of Count( !unction

" %

Sub #uer$ !ilter

& ( 1* 11 12 13

'lias Table )oin Null +alue Not 12ist in place of Not in ,C.an/e Cursor to 0oop

Comments if two condition in use like lessthan and clause in the query for !pti"i#ation reaterthan then use between

if more data will be process t.an user Temporar$ table for /et data in Temporar$ table and use t.at Temp Table for below 4rocessin/ t.is is most useful tips for #uer$ speed up Use views and stored procedures instead of heavy-duty queries. T.is can reduce networ5 trafficB because $our client will send to ser+er onl$ stored procedure or +iew name (per.aps wit. some parameters instead of lar/e .ea+$C dut$ Dueries te2t. Try to avoid using SQL Server cursors, whenever possible. S#0 Ser+er cursors can result in some performance de/radation in comparison wit. select statements. Tr$ to use correlated subDuer$ or deri+ed tablesB if $ou need to perform rowCb$Crow operations. Use Table3s 4rimar$ 5e$ in Count(p56id in place of Count(7 below #uer$ will /i+e same output as count rows for table S101CT rows !-,8 s$sinde2es 9:1-1 id ; ,B)1CT6<=(3TableName3 'N= indid > 2 if onl$ one field is needed t.en use Sub#uer$ not use ?oin for one field Use proper SeDuence of t.e !ilter in t.e w.ere condition it is +er$ important for select #uer$ e2ample@9rite bi/filter first w.ic. reduce t.e -eocrds for ot.er !ilters Use Table 'lias for t.e use of more t.an one table t.at will affect t.e speed of #uer$ if $ou ?oin Two table t.an 9rite more rows table first in t.e ?oinin/ t.at will wor5 fast in t.e #uer$ =onAt Compare Null +alues in t.e w.ere condition it will affect t.e speed of #uer$ Use Not e2itst in place of not in it +er$ beneficial for #uer$. in w.ere condition replace not e2itst in place of not in '+oid ,- Conditional operator use <N or UN<,N inplace of or S.ow Below 12ample

if cursors will ta5e time t.en use t.at lo/ic for t.e $our Store procedurs. CCCbelow is cursor declare EDuer$ +arc.ar(1** B Edbname s$sname declare BadCursor Cursor for select name from s$s.databases w.ere name not in (3master3B 3model3B 3msdb3B 3tempdb3 open BadCursor fetc. ne2t from BadCursor into Edbname w.ile EEfetc.6status ; * be/in select EDuer$ ; 3dbcc c.ec5db(3 F Duotename(Edbname F 3 3 e2ec(EDuer$ fetc. ne2t from BadCursor into Edbname end close BadCursor deallocate BadCursor /o CC .ow to c.an/e to loop declare EDuer$ +arc.ar(1** B Edbname s$sname declare Edblist table (dbname s$sname insert into Edblist(dbname select name from s$s.databases w.ere name not in (3master3B 3model3B 3msdb3B 3tempdb3 w.ile (select count(7 from Edblist G * be/in select top 1 Edbname ; dbname from Edblist select EDuer$ ; 3dbcc c.ec5db(3 F Duotename(Edbname F 3 3 e2ec(EDuer$ delete from Edblist w.ere dbname ; Edbname end /o ?ust /et $our data in t.e 'n$ Temp table or table +ariable after t.at delete rows from t.at at end of loop.

Sr.No 111 127 1!" 1() 1*1 2(" 2)) 2)" 271 27* 2"7 2*2 !(1 !11 !*2

Name of 4rocedure NHS_SP_GET_MedicationDataBetWeenTime NHS_SP_GETACTIVERORDERLISTo d NHS_SP_Get_O#de#Reco#d$%o#P&'$ician NHS_SP_GETADDMISSIONORDERRECORDS NHS_GET_Re$identP#o+ide#Di$c&a#,eS-mma#'Data NHS_RPT_Di$c&a#,e_CMS".2Re/o#t NHS_SP_RPT_GETACTIVERORDERLIST_ALL NHS_RPT_0nitMont 'T#eatMentS&eetRe/o#t NHS_RPT_SP_GETORDERHISTOR2%o#Batc&P#int NHS_SP_LAST2(Ho-#Reac/Re$identLi$t NHS_SP_Get_N-#$eC&emica A$$i,nedMedication$ NHS_SP_Get_N-#$eRe$identBo3e Medication$ NHS_SP_RPT_GETACTIVERORDERLIST NHS_SP_GetMedication0nitIn4o NHS_SP_GETORDERHISTOR2

Status In50$e In50$e In50$e In50$e In50$e In50$e In50$e In50$e In50$e In50$e In50$e In50$e In50$e In50$e In50$e

,ptimiHation 'ppl$ DONE DONE DONE DONE DONE DONE DONE DONE DONE DONE DONE DONE Done Done DONE

C.an/e 2e$ 2e$ 2e$ 2e$ 2e$ 2ES 2ES 2ES 2ES 2ES 2ES 2ES 2ES 2ES 2ES

=etail

Sec Before

Sec 'fter

$e ect5654#om5in4o#mation_Sc&ema7#o-tine$ 3&e#e 5$/eci4ic_name5not5 i8e59:In$e#t_t; :9 and 5$/eci4ic_name5not5 i8e59:-/date_t; :9 and5 5$/eci4ic_name5not5 i8e59:Reada _t; :9 and5 $/eci4ic_name5not5 i8e59:De ete_t; :9 and5 RO0TINE_t'/e5<9PROCED0RE9 and5$/eci4ic_name5not5 i8e59:dt_:9 and5$/eci4ic_name5 i8e59NHS:9 and5RO0TINE_DE%INITION5 i8e59:NHS_SP_GetEm/ o'ee0$e#ID:9

P_GetEm/ o'ee0$e#ID:9

You might also like