Sql Traning

Embed Size (px)

Citation preview

  • 8/9/2019 Sql Traning

    1/28

    Basic SQL TrainingBasic SQL Training

  • 8/9/2019 Sql Traning

    2/28

  • 8/9/2019 Sql Traning

    3/28

  • 8/9/2019 Sql Traning

    4/28

    Select QuerySelect Query

    The SELECT statement is used to select data from a database.

    The result is stored in a result table, called the result-set.

    SQL SELEC Syntax:

  • 8/9/2019 Sql Traning

    5/28

    Sql Where ClauseSql Where Clause

    The W E!E clause is used to extract only those records that fulfill as"ecified criterion.

    SQL W E!E Syntax :

    Exam"le:

    SELECT # $!%& CustomersW E!E Country'(&exico()

  • 8/9/2019 Sql Traning

    6/28

    The SQL I&S%'T I&T" Statement The SQL I&S%'T I&T" Statement The *+SE!T *+T% statement is used to insert ne records in a table.

    SQL *+SE! *+ % Syntax: *t is "ossible to rite the *+SE!T *+T% statement in t o forms.

    The first form does not s"ecify the column names here the data ill be inserted,only their alues:

    The second form s"ecifies both the column names and the alues to be inserted:

    Exam"le:

    *+SE!T *+T% Customers Customer+ame, Contact+ame, /ddress, City, 0ostalCode,Country1 2/L3ES (Cardinal(,(Tom 4. Erichsen(,(S5a6en 78(,(Sta-an6er(,(9::;(,(+or ay(1)

  • 8/9/2019 Sql Traning

    7/28

  • 8/9/2019 Sql Traning

    8/28

    SQL J"I&SQL J"I& /n SQL =%*+ clause is used to combine ro s from t o or more tables,

    based on a common field bet een them.

    The most common ty"e of >oin is: SQL *++E! =%*+ sim"le >oin1. /n SQL*++E! =%*+ return all ro s from multi"le tables here the >oin conditionis met.

  • 8/9/2019 Sql Traning

    9/28

    SQL I&&%' J"I&SQL I&&%' J"I&) The INNER JOIN keyword selects all rows from both tables as long as there is a match

    between the columns in both tables.

    ) INNER JOIN is the same as JOIN.Syntax:

  • 8/9/2019 Sql Traning

    10/28

    SQL LE$T =%*+SQL LE$T =%*+

    The LE$T =%*+ 5ey ord returns all ro s from the left table table81, ith thematchin6 ro s in the ri6ht table table71. The result is +3LL in the ri6ht side

    hen there is no match. SQL LE$T =%*+ Syntax:

  • 8/9/2019 Sql Traning

    11/28

    SQL 'I*+T J"I& ,ey-ordSQL 'I*+T J"I& ,ey-ord

    The !*@ T =%*+ 5ey ord returns all ro s from the ri6ht table table71,ith the matchin6 ro s in the left table table81. The result is +3LL in the

    left side hen there is no match.

  • 8/9/2019 Sql Traning

    12/28

    SQL .ULL "UT%' J"I&SQL .ULL "UT%' J"I&

    ) The .ULL "UT%' J"I& /ey-ord returns all ro-s 0rom the le0t ta1le 2ta1le34and 0rom the right ta1le 2ta1le546

    ) The .ULL "UT%' J"I& /ey-ord com1ines the result o0 1oth L%.T and 'I*+T 7oins6

    ) SQL .ULL "UT%' J"I& Synta$8

  • 8/9/2019 Sql Traning

    13/28

    Analyzing SQ Pro!iler output Analyzing SQ Pro!iler output

    "icroso!t SQ Ser#er Pro!iler is a graphical user inter!ace to SQ Trace !ormonitoring T$SQ Statements o! Database Engine. %e can sa#e and reusethe state at a later point o! time.

    We can do the following using SQL Se !e P ofile :

    &reate a trace%atch the trace results as the trace runsStore the trace results in a tableStart' stop' pause' and modi!y the trace results as necessary(eplay the trace results

    ) Menu Path: Start | All Programs | Microsoft SQL Server 2008 |Menu Path: Start | All Programs | Microsoft SQL Server 2008 |Performance Tools | SQL Server Pro lerPerformance Tools | SQL Server Pro ler

    !ont !ont

  • 8/9/2019 Sql Traning

    14/28

    Step ) The 0ollo-ing screen -ill come8

    This is 1asically a login screen o0 sql ser9er pro!ler -here -e ha9e toenter credentials to login

  • 8/9/2019 Sql Traning

    15/28

    Step 2:Step 2: /fter Lo6in this indo ill a""er,this is basically trace "ro"erties indo .

    ere e ill select arious e ent aacordin6 to our need by sim"ly chec5in6 thechec5boxes.

    @enerally e chec5 only follo in6 e ents:!0C Com"letedSAl 4atch Com"letedSAl 4atch Startin6

  • 8/9/2019 Sql Traning

    16/28

    Ste" ?Ste" ?

    /fter clic5in6 on column filter ne indo ill a""erThis Section is somethin6 li5e

    customiBation. =ust clic5 on the Column $ilter 4utton . *n this section, you cans"ecify some condition li5e or +ot li5e1. ere 6enrally e enter database id forbetter trac5in6 , hich is ta5en from sys.databases table .

  • 8/9/2019 Sql Traning

    17/28

    Step :8Step :8

    +o Clic5 on the !un 4utton, then Trace indo ill come:

    3sin6 these indo s, you ill 6et the detailed time duration of a Auery and all othere ents information that you ha e selected.

    Dou can sa e this result and use it in future. %r you can extract a "articular Aueryfrom the trace, >ust ri6ht clic5 and clic5 on Extract E ent

  • 8/9/2019 Sql Traning

    18/28

    Important oints8Important oints8

    Profiler is basically used to analysis performanceissue or production issue.In case of performance issue we can identify queryby identify query having higher duration & by starttime or end time of individual query.In case of production issue where there is improperfunction,we can identify query by searching query byparticular column name or its value.After this we have to paste query in query editor &check its execution plan & analy e query & try to findout ,is there any scope of improvement.

  • 8/9/2019 Sql Traning

    19/28

    "issing *ndex Query Analysis

    er0ormance Tuning is quite interesting and Inde$ plays a9ital role in it6 A proper inde$ can impro9e the per0ormanceand a 1ad inde$ can hamper the per0ormance6

    lease note; i0 you should not create all the missing inde$esthis script suggest6 This is 7ust 0or guidance6 3? inde$es per ta1le6 Additionally; thisscript sometime does not gi9e accurate in0ormation so useyour common sense6

  • 8/9/2019 Sql Traning

    20/28

    Query to !ind "issing index in SQ Ser#er+

    S%L%CT mig6inde$@group@handle;mid6inde$@handle; migs6a9g@total@user@cost

    as A9gTotalUserCostThatCould1e'educed; migs6a9g@user@impact asA9g ercentageBene!t;migs6user@see/s as &um1er"0See/Queries&eedIt; migs6user@scans as&um1er"0ScanQueries&eedIt;

    C'%AT% I&(% missing@inde$@ C"&D%'T 29archar;mig6inde$@group@handle4 @ C"&D%'T 29archar; mid6inde$@handle4 "& mid6statement 2 IS&ULL 2mid6equality@columns; 4

    CAS% W+%& mid6equality@columns IS &"T &ULL A&(mid6inequality@columns IS &"T &ULL T+%& ; %LS% %&(

    IS&ULL 2mid6inequality@columns; 4 4 IS&ULL 2 I&CLU(% 2 mid6included@columns 4 ; 4 AScreate@inde$@statement

    .'"# sys6dm@d1@missing@inde$@groups mig

    I&&%' J"I& sys6dm@d1@missing@inde$@group@stats migs "& migs6group@handleE mig6inde$@group@handleI&&%' J"I& sys6dm@d1@missing@inde$@details mid "& mig6inde$@handle Emid6inde$@handle>>order 1y migs6a9g@total@user@costFuser@see/s descorder 1y migs6user@see/s desc

  • 8/9/2019 Sql Traning

    21/28

    Inde$ Synta$Inde$ Synta$) SQL C'%AT% I&(% Synta$8

    C'%AT% I&(% inde$@name "& ta1le@name 2column@name4

    ) ('" I&(% Synta$ 8('" I&(% inde$@name "& ta1le@name

    ) (ISABL% I&(% Synta$8ALT%' I&(% inde$@name on ta1le@name (ISABL%G

    ) '%BUIL( I&(% Synta$8ALT%' I&(% inde$@name on ta1le@name '%BUIL(G

  • 8/9/2019 Sql Traning

    22/28

    Worst Query AnalysisWorst Query Analysis

    ) Worst query -ill return T" 5? -orst per0orming SQL Statement-ith the name o0 o17ect that contain those statements e6g6 Stored

    rocedure; Trigger and .unction6 query6 The result -ill 1e sorted 1yC U time in descending order6

    ) %lapsed time should 1e less corresponding to e$ecution count 0or

    1etter per0ormance6Loo/ the query and replace unnecessarily typecasting and e$tra line o0 codes -hich can 1e handle6

    ) We consider 9arious 0actor -hile selecting -orst query li/e total-or/er time;logical reads;e$ecution count;e$ecution plan etc6

    ) There is a lin/ in output o0 -orst query6Clic/ing this -ill sho-casecomplete graphical e$ecution plan6 -hich -ill pro9ide completein0ormation li/e total su1tree cost;9arious phyiscal operator;inde$recommnedation ;i0 any6

  • 8/9/2019 Sql Traning

    23/28

    Query to !ind Top ,- %orst Query

    S%L%CT T" 5? SUBST'I&*2qt6te$t; 2qs6statement@start@o set 54 3;

    22CAS% qs6statement@end@o set W+%& >3 T+%& (ATAL%&*T+2qt6te$t4 %LS% qs6statement@end@o set %&( > qs6statement@start@o set4 54 34;qs6e$ecution@count;qs6total@-or/er@time; qs6last@-or/er@time;qs6min@-or/er@time; qs6ma$@-or/er@time;qs6total@logical@reads; qs6last@logical@reads;qs6min@logical@reads; qs6ma$@logical@reads;qs6total@elapsed@time; qs6last@elapsed@time;qs6min@elapsed@time; qs6ma$@elapsed@time;qs6last@e$ecution@time;qp6query@plan

    .'"# sys6dm@e$ec@query@stats qsC'"SS A L< sys6dm@e$ec@sql@te$t2qs6sql@handle4 qtC'"SS A L< sys6dm@e$ec@query@plan2qs6plan@handle4 qpW+%'% qt6encryptedE?"'(%' B< qs6total@-or/er@time (%SC

  • 8/9/2019 Sql Traning

    24/28

    est Practices+

    ).This /uery 0ill pro#ide top ,- 0orst /ueries along 0ith their execution'max0or1er time'total 0or1er time an execution plan.

    ,.%hile selecting #ictim /uery 0e ha#e to consider #arious !actor li1eexecution'max 0or1er time'total 0or1er time.

    2.A!ter analyzing the /uery 0e ha#e to copy the /uery in /uery editor'!ormatit'execute it and chec1 its execution plan.

    3.&hec1 0eather is there any missing index'any implicit casting and ta1e actionaccordingly.

    4.*! not then 0e #eri!y is there is any scope o! /uery re$!actoring.

  • 8/9/2019 Sql Traning

    25/28

  • 8/9/2019 Sql Traning

    26/28

    SolutionSolution ++

    ) The graphical e$ecution plan is 7ust that6 It is a tool that presents in

    images and te$t; the process 1y -hich SQL Ser9er steps through the query;parsing; hashing; and 1uilding the results set using the in0ormation it hasa9aila1le 2statistics; inde$es; and the ra- data46

    ) There are actually t-o other Ma9ors o0 e$ecution plans that -e -ill not 1ediscussing at this time8 te$t e$ecution plan and an $ml e$ecution plan6

    ) There are t-o types o0 graphical e$ecution plans8 the estimated e$ecutionplan and the actual e$ecution plan6 They are precisely -hat the namesimply 2yes; a rarity in technology46

    ) The estimated e$ecution plan is an estimate 1ased upon the queryoptimizer on -hat it e$pects to occur -hen e$ecuting the query -hereas

    the actual e$ecution plan sho- -hat did actually occur -hen generatingthe results6

  • 8/9/2019 Sql Traning

    27/28

  • 8/9/2019 Sql Traning

    28/28

    SummariBe @ra"hical Execution 0lans:SummariBe @ra"hical Execution 0lans:

    There are a ariety of methods for dis"layin6 or tri66erin66ra"hical execution "lans

    Dou read them ri6ht-to-left and to"-to-bottom.

    The arro s denote not >ust the direction of data tra el, but alsocom"arati ely1 amount of data ro s bein6 transferred fromste"-to-ste" in the execution "rocess

    Costs are dis"layed for each ste", relati e to the total cost of the

    Auery "lan.

    The cost for the statement can be com"ared to other statementsrun in the same batch