TSQL OPTIMIZATIONSMicrosoft SQL Server 2012
Tips and tricks toward making queries perform betterMicrosoft SQL Server 2012
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
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
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
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
Execution Plans and Operators
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
WHAT DOES OPTIMIZER DO TO PLAN
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
Query Plan QUERY PLAN
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
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
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
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
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
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
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
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
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
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