Download presentation
Presentation is loading. Please wait.
Published byNeil Chapman Modified over 6 years ago
1
SQL Server Statistics 101 Travis Whitley Senior Consultant, Oakwood Systems Group @whitleysql whitleysql.wordpress.com
2
Presentation Roadmap Foundation Demo: Visualizing Statistics
The Big Picture – Query Processing Statistics Primer Demo: Visualizing Statistics The Statistics Object Cardinality Estimation Problems and Solutions Best Practices Maintenance Recommendations
3
The Big Picture Statistics Primer Foundation
4
The Big Picture – Query processing
Think about data access Selectivity Which query do you think will benefit most from an index on ProductType? select * from Transactions where ProductType = ‘Fuel' where ProductType = ‘Food' where ProductType = ‘Toiletries‘ -So let’s think about data access. Gas station transactions table example.
5
The Big Picture – Query processing
6
Statistics Primer What are statistics? Statistics contain a map of the data distribution for a column or index Statistics are real database objects The statistics object is stored in the sysidxstats table in the primary filegroup and in a binary large object or statblob (internal table) They contain density information and a histogram for the first column in the statistics object (high order element of data set) -Size 30,000 stats per table in SQL 2008 R2 - Transition from this slide to the next slide
7
Statistics Primer Creating Statistics Updating Statistics
AUTO_CREATE_STATISTICS Set to ON by default Will create SINGLE column statistics based on query WHERE/JOIN Never MULTI-column Index creation Statistics always created along with new Index SINGLE or MULTI-column CREATE STATISTICS statement Manual creation for given column(s) sp_createstats stored proc Will create statistics on all columns for all user tables in the current database, except for XML columns Excludes columns with existing statistics AUTO_UPDATE_STATISTICS Set to ON by default Colmodctrs SAMPLED based on default sample rate sp_updatestats stored proc Updates all existing stats, if necessary Uses old rowmodctr, not colmodctr UPDATE STATISTICS statement Allows specific updates of distinct stats Can specify sample size or FULLSCAN Can specify all (default), columns, or index INDEX REBUILD Will update index linked statistics, equivalent of FULLSCAN Will NOT update auto generated or column statistics Statistics Primer
8
When SQL Server “takes care of itself”
Statistics Primer When SQL Server “takes care of itself” The default sample rate gets relatively lower as tables grow Autoupdate is only triggered if the colmodctrs ‘stale’ threshold is reached AND the relative index/column is queried Manual or automatic updating invalidates any cached plan utilizing those statistics Index Reorganization/DEFRAG will NOT update index linked statistics Autocreate will not work on Read Only databases Autoupdate is generally beneficial, but in certain scenarios it can be problematic
9
DEMO: Visualizing statistics
The Statistics Object Cardinality Estimation DEMO: Visualizing statistics
10
Problems and solutions
Best Practices Maintenance Recommendations Problems and solutions
11
Keep AUTO_CREATE_STATISTICS ON by default
Best Practices Keep AUTO_CREATE_STATISTICS ON by default Keep AUTO_UPDATE_STATISTICS ON by default Try AUTO_UPDATE_STATISTICS_ASYNC for large OLTP tables if auto updating is an issue Recommend stored procs with parameters rather than local variables in queries Create filtered statistics when 200 histogram steps won’t suffice Eliminate any duplicate statistics If you notice estimate vs actual are off while troubleshooting, try updating the statistics
12
Maintenance Recommendations
Schedule an Update statistics job with large sample or FULLSCAN if feasible Nightly for larger/OLTP tables Weekly for smaller, less updated tables Update COLUMN statistics only on INDEX REBUILD days Stats on tables with ascending key columns may need updated more frequently than other tables Automate script that will pin point out of date or auto updated stats
13
Holger Schmeling: SQL Server Statistics SQL Server Internals 2008
References Books Holger Schmeling: SQL Server Statistics SQL Server Internals 2008 Online BOL: SQL Server Statistics Fabiano Amorim Blog Glenn A Berry Blog
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.