SQL Server Query Performance Tuning Introduction
SQL Server Query Performance Tuning Introduction
Tuning Basics
Why query tuning is necessary for
developers?
• Remember your reputation will follow you.
• Unique
• Narrow
• Static
• Ever-increasing (preferably)
• Order of data access/retrieval matters
• Guidelines are not mandatory and not
necessarily applicable for all scenarios
Useful SET options
Display the number of milliseconds required to parse, compile,
SET STATISTICS TIME and execute each statement.
• Statistics have to be created and maintained, and this requires some resources.
• As statistics are stored separately from the table or index they relate to, some
effort has to be taken to keep them in sync with the original data.
Whenever a distribution statistic no longer reflects the source data, the optimizer
may make wrong assumption about cardinalities, which in turn may lead to poor
execution plans.
Contents of statistics
DBCC SHOW_STATISTICS(<table_name>, <stats_name>)
sys.dm_db_stats_properties
Density
1/(distinct values) for the columns comprising the statistics
RANGE_ROWS Specifies how many rows are inside the range (they
are smaller than this RANGE_HI_KEY, but bigger
than the previous smaller RANGE_HI_KEY).
Manual updates
• UPDATE STATISTICS
• sp_updatestats
Internal Fragmentation When pages are less than fully used, the part of each
page that is unused constitutes a form of fragmentation, since the table’s or index’s
rows are no longer packed together as tightly as they could be.
External Fragmentation
Logical Fragmentation Logical fragmentation occurs when logical ordering of pages,
which is based on the key value, does not match the physical ordering inside the data
file.
• Parameter Sniffing
• Hints
• OPTIMIZE FOR (specific value/UNKNOWN) hint
• RECOMPILE
• Limitations of Statistics
• Filtered Statistics