advanced tsql part ii
DESCRIPTION
Advanced TSQL Part IITRANSCRIPT
![Page 1: Advanced TSQL Part II](https://reader030.vdocuments.net/reader030/viewer/2022032516/563db9ff550346aa9aa1c9bd/html5/thumbnails/1.jpg)
TSQL OPTIMIZATIONSMicrosoft SQL Server 2012
![Page 2: Advanced TSQL Part II](https://reader030.vdocuments.net/reader030/viewer/2022032516/563db9ff550346aa9aa1c9bd/html5/thumbnails/2.jpg)
Tips and tricks toward making queries perform betterMicrosoft SQL Server 2012
![Page 3: Advanced TSQL Part II](https://reader030.vdocuments.net/reader030/viewer/2022032516/563db9ff550346aa9aa1c9bd/html5/thumbnails/3.jpg)
OPTIMIZATION TECHNIQUES
• Avoid subqueries• Avoid LEFT JOINS • Limit the number of Joins• Filter out unwanted records• Explicitly mention columns• Create and use proper indexes• Avoid Distinct, Union, Not In operators• Use SET NO COUNT ON
![Page 4: Advanced TSQL Part II](https://reader030.vdocuments.net/reader030/viewer/2022032516/563db9ff550346aa9aa1c9bd/html5/thumbnails/4.jpg)
OPTIMIZATION TECHNIQUES CONTD.
• Use TABLE VARIABLES instead of Temp tables
• Avoid dynamic sql
• Avoid recompiles
• Use IF EXIST instead of COUNT(*)
• Use COUNT(1) instead of COUNT(*)
• Avoid functions on columns in the Where clause
![Page 5: Advanced TSQL Part II](https://reader030.vdocuments.net/reader030/viewer/2022032516/563db9ff550346aa9aa1c9bd/html5/thumbnails/5.jpg)
OPTIMIZATION TECHNIQUES CONTD.
• Avoid cursors
• Favor set-based logic over procedural or cursor logic
• Avoid query hints
• Use correlated subqueries to improve performance
• Avoid using a scalar user-defined function in the WHERE clause
• Avoid unnecessary GROUP BY columns
• Use CASE expressions to include variable logic in a query
• Divide joins into temporary tables when you query very large tables
![Page 6: Advanced TSQL Part II](https://reader030.vdocuments.net/reader030/viewer/2022032516/563db9ff550346aa9aa1c9bd/html5/thumbnails/6.jpg)
OPTIMIZATION TECHNIQUES CONTD
• Minimize transaction times
• Optimize the SPs to reduce the lock duration
• Combine updates
• Always access server objects in same order for similar operations
• Apply covering indexes when having multiple column filters
• Reduce lookups
![Page 7: Advanced TSQL Part II](https://reader030.vdocuments.net/reader030/viewer/2022032516/563db9ff550346aa9aa1c9bd/html5/thumbnails/7.jpg)
Execution Plans and Operators
![Page 8: Advanced TSQL Part II](https://reader030.vdocuments.net/reader030/viewer/2022032516/563db9ff550346aa9aa1c9bd/html5/thumbnails/8.jpg)
WHAT HAPPENS TO QUERY??• Parser: Validation for Syntactical
errors. • Example (Select * from <table
Name>, syntax error)
• Algebrizer:• Does Name resolution
• Tables, Views, Columns• Object Not found is an error from
Algebrizer• Aggregations with Group By (Below
example parser validation is OK but not algebrizer)
select LoginID, COUNT(EmployeeID) from AdventureWorksDW.HumanResources.Employee
Group by Title
• Algebrizer provides Query Processor Tree for Optimizer
• Optimizer generates query Execution plan.
• Execution Engine and Storage Engine Execute query
![Page 9: Advanced TSQL Part II](https://reader030.vdocuments.net/reader030/viewer/2022032516/563db9ff550346aa9aa1c9bd/html5/thumbnails/9.jpg)
WHAT DOES OPTIMIZER DO TO PLAN
![Page 10: Advanced TSQL Part II](https://reader030.vdocuments.net/reader030/viewer/2022032516/563db9ff550346aa9aa1c9bd/html5/thumbnails/10.jpg)
What a Query Plan Shows
A Query Plan Shows:
How data is accessed
How data is joined
How data is aggregated
Sequence of operations
Use of temporary worktables, sorts, etc.
Estimated rowcounts, iterations, and costs from each step
Actual rowcounts and iterations•When using SET STATISTICS PROFILE or SET STATISTICS XML
Use of parallelism
WHAT A QUERY PLAN SHOWS
![Page 11: Advanced TSQL Part II](https://reader030.vdocuments.net/reader030/viewer/2022032516/563db9ff550346aa9aa1c9bd/html5/thumbnails/11.jpg)
Query Plan QUERY PLAN
![Page 12: Advanced TSQL Part II](https://reader030.vdocuments.net/reader030/viewer/2022032516/563db9ff550346aa9aa1c9bd/html5/thumbnails/12.jpg)
Joins in Graphical Showplan
Flow of data is from right to left, from top to bottom
For joins, the outer table is the top one
Two crucial facts affect join performance
Order in which more than two tables are joined
Selection of outer/inner table
JOINS IN GRAPHICAL SHOWPLAN
![Page 13: Advanced TSQL Part II](https://reader030.vdocuments.net/reader030/viewer/2022032516/563db9ff550346aa9aa1c9bd/html5/thumbnails/13.jpg)
Joins
Types of Joins
The table accessed first is called “outer”; the second table is called “inner.”
Nested Loop
For each row of the outer table find all matching rows in the inner.
Merge Join
Process both tables in the order of the join columns.
Hash Join
Build a hash table (from the outer) and pass all rows of the inner through the hash table identifying the matches.
JOINS
![Page 14: Advanced TSQL Part II](https://reader030.vdocuments.net/reader030/viewer/2022032516/563db9ff550346aa9aa1c9bd/html5/thumbnails/14.jpg)
Nested Loop Join
How does it work?
For each row of the outer table, find all matches in the inner table
Best when there is a supporting index on the inner table
Low memory requirement
Smaller table should appear as the outer table
SQL Server may sort the outer table to improve the locality of the seeks
NESTED LOOP JOINNESTED LOOPJOIN
![Page 15: Advanced TSQL Part II](https://reader030.vdocuments.net/reader030/viewer/2022032516/563db9ff550346aa9aa1c9bd/html5/thumbnails/15.jpg)
Nested Loop Join
Get Row From Outer Table
Get matching row from inner table
Output composite result
Loop through inner table
When inner table exhausted, loop on outer table
NESTED LOOP JOIN
![Page 16: Advanced TSQL Part II](https://reader030.vdocuments.net/reader030/viewer/2022032516/563db9ff550346aa9aa1c9bd/html5/thumbnails/16.jpg)
Merge Join
How does it work?Both inputs must already be sorted in the order of the join key when entering the joinAn index is useful to provide the orderSmall and large tables may appear on either side of the joinLow memory requirement, unless many duplicates occur on the inner sideInner/outer selection is not usually as important in Loop and Hash joins, but if there are duplicates on the inner side, and there might be duplicates on the outer side, the inner duplicates must be stored
MERGE JOINMERGE JOIN
![Page 17: Advanced TSQL Part II](https://reader030.vdocuments.net/reader030/viewer/2022032516/563db9ff550346aa9aa1c9bd/html5/thumbnails/17.jpg)
Merge Join
Get next row from outer table
Get next row from inner table with same keyIf found, output and loop on inner tableIf not found, loop on outer table
Join Sequence
Join Sequence
Match &Merge
MERGE JOIN
![Page 18: Advanced TSQL Part II](https://reader030.vdocuments.net/reader030/viewer/2022032516/563db9ff550346aa9aa1c9bd/html5/thumbnails/18.jpg)
Hash Join
How does it work?
Outer table is used to build a hash table
Outer table is completely read before first inner row is accessed
Unique to Hash JoinSmaller table is used as outer
No order is preserved
Unique to Hash JoinThe larger the outer table, the more memory is needed
Indexes are not used
excellent for adhoc queries
HASH JOINHASH JOIN
![Page 19: Advanced TSQL Part II](https://reader030.vdocuments.net/reader030/viewer/2022032516/563db9ff550346aa9aa1c9bd/html5/thumbnails/19.jpg)
Hash Join
1 Scan Smaller (Build) Table
Hash Build Key Values; Store in Hash Table2 Scan Larger (Probe) table
Hash Probe Key Value; Look Up in Hash TableIf Found, Output Result
HASH JOIN
![Page 20: Advanced TSQL Part II](https://reader030.vdocuments.net/reader030/viewer/2022032516/563db9ff550346aa9aa1c9bd/html5/thumbnails/20.jpg)
Aggregation
GROUP BY and DISTINCT
The main task of the Aggregation Operator is to identify matching rows in a single set
Aggregation Key
Columns in the GROUP BY or DISTINCT clause
Stream Aggregation
Hash Aggregation
AGGERIGATION
![Page 21: Advanced TSQL Part II](https://reader030.vdocuments.net/reader030/viewer/2022032516/563db9ff550346aa9aa1c9bd/html5/thumbnails/21.jpg)
Suboptimal Plans
Why might the Optimizer choose a suboptimal plan?
Missing Statistics
Out-of-Date Statistics
Miscalculated Cardinalities
Missing Indexes
Incorrect Estimated Cost
SUBOPTIMAL PLANS