Advanced TSQL Part II

21
TSQL OPTIMIZATIONS Microsoft SQL Server 2012

description

Advanced TSQL Part II

Transcript of Advanced TSQL Part II

Page 1: Advanced TSQL Part II

TSQL OPTIMIZATIONSMicrosoft SQL Server 2012

Page 2: Advanced TSQL Part II

Tips and tricks toward making queries perform betterMicrosoft SQL Server 2012

Page 3: Advanced TSQL Part II

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

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

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

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

Execution Plans and Operators

Page 8: Advanced TSQL Part II

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

WHAT DOES OPTIMIZER DO TO PLAN

Page 10: Advanced TSQL Part II

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

Query Plan QUERY PLAN

Page 12: Advanced TSQL Part II

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

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

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

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

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

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

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

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

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

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