1 / 15

Lies , Damn Lies and Statistics Making the Most Out of SQL Server Statistics

Maciej Pilecki | Project Botticelli Ltd . Lies , Damn Lies and Statistics Making the Most Out of SQL Server Statistics. SELECT Bio FROM Speakers WHERE Full Name= ‘ Maciej Pilecki ’;. Microsoft Certified Trainer since 2001 SQL Server MVP since Jan 2006

svea
Download Presentation

Lies , Damn Lies and Statistics Making the Most Out of SQL Server Statistics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Maciej Pilecki | Project Botticelli Ltd. Lies, DamnLies and StatisticsMakingthe Most Out of SQL Server Statistics

  2. SELECT Bio FROM Speakers WHERE FullName=‘Maciej Pilecki’; • Microsoft Certified Trainer since 2001 • SQL Server MVP since Jan 2006 • Specializing in SQL Server database development and administration • Delivering training and consulting services around the world • Frequent speaker at many international conferences and UG meetings

  3. Agenda • Introduction to statistics • Statistics internals • Creating statistics • Maintaining statistics

  4. HowtheQuery Optimizer Works Source: SQL 2008 Books Online

  5. How theQuery Optimizer Works • SQL Server uses "cost-based" optimizer • Execution plan is chosen based on theESTIMATED cost of execution • Cost estimation based on the number of rows processed (cardinality estimation) • Cardinality estimation based on • Number of rows in a table • Data distribution statistics • Optimizer estimates selectivity of a query

  6. What are Statistics? • Statistics hold data distribution information about of values in a columnor a set of columns • Statistics contain: • Distribution for leading column • Density information for column combinations • Every index has associated statistics

  7. Statistics internals • Viewing statistics: • sys.stats • sys.stats_columns • DBCC SHOW_STATISTICS

  8. Creating statistics • Automatically: • Depends on AUTO_CREATE_STATISTICS database option • For every index • For each non-indexed column used in the query predicate • Manually: • CREATE STATISTICS • sp_createstats

  9. Maintaining statistics • Outdated stats can lead to suboptimal execution plans • Statshave to be refreshed

  10. Maintaining statistics • Automatically, depending on: • AUTO_UPDATE_STATISTICS database setting • STATISTICS_NORECOMPUTE option of the index • NORECOMPUTE option of the statistics • sp_autostats • AUTO_UPDATE_STATISTICS_ASYNC database option • Manually: • UPDATE STATISTICS • sp_updatestats • Index rebuild

  11. Auto-updates • Thresholds for auto-updates: • For temp table with less than 6 rows – after every 6 updates • For table with less than 500 rows - after 500 updates • For larger table - after 500 + 20% of rows • For table variables – NEVER • Described in KB195565 • Updatestrackedinrowmodctrssysindexes

  12. Best practices • Don't turn off: • AUTO_CREATE_STATISTICS • AUTO_UPDATE_STATISTICS • Rebuild your indexes from time to time • Performs a full-scan update of statistics • Run sp_updatestats after any major update

  13. Summary • Introduction to statistics • Statistics internals • Creating statistics • Maintaining statistics

  14. RelatedContent • Statistics Used by the Query Optimizer in Microsoft SQL Server 2008 http://msdn.microsoft.com/en-us/library/dd535534.aspx • Elisabeth Redei – Lies, Damn Lies and Statistics (in two parts): http://sqlblog.com/blogs/elisabeth_redei/archive/2009/03/01/lies-damned-lies-and-statistics-part-i.aspx • SQL Server Internals and QueryTuningclass: Mainz, 22-26 November http://entwickler-akademie.de/codecamps/workshop-maciej-pilecki

  15. Q & Amaciej@projectbotticelli.com

More Related