SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP SQL SERVER Database Administration.

Slides:



Advertisements
Similar presentations
Tuning: overview Rewrite SQL (Leccotech)Leccotech Create Index Redefine Main memory structures (SGA in Oracle) Change the Block Size Materialized Views,
Advertisements

Cardinality How many rows? Distribution How many distinct values? density How many rows for each distinct value? Used by optimizer A histogram 200 steps.
What Happens when a SQL statement is issued?
© IBM Corporation Informix Chat with the Labs John F. Miller III Unlocking the Mysteries Behind Update Statistics STSM.
SQL Performance 2011/12 Joe Chang, SolidQ
Comprehensive Performance with Automated Execution Plan Analysis (ExecStats) Joe Chang yahoo
Database Optimization & Maintenance Tim Richard ECM Training Conference#dbwestECM Agenda SQL Configuration OnBase DB Planning Backups Integrity.
Dave Ballantyne Clear Sky SQL. ›Freelance Database Developer/Designer –Specializing in SQL Server for 15+ years ›SQLLunch –Lunchtime usergroup –London.
Paper by: A. Balmin, T. Eliaz, J. Hornibrook, L. Lim, G. M. Lohman, D. Simmen, M. Wang, C. Zhang Slides and Presentation By: Justin Weaver.
Virtual techdays INDIA │ 9-11 February 2011 SQL 2008 Query Tuning Praveen Srivatsa │ Principal SME – StudyDesk91 │ Director, AsthraSoft Consulting │ Microsoft.
Module 7: Creating and Maintaining Indexes. Overview Creating Indexes Creating Index Options Maintaining Indexes Introduction to Statistics Querying the.
DM109 A Practical Guide To ASE Optimizer Statistics and Optdiag Eric Miner Development Engineer Optimizer Group
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
Database Management 9. course. Execution of queries.
1 Chapter 7 Optimizing the Optimizer. 2 The Oracle Optimizer is… About query optimization Is a sophisticated set of algorithms Choosing the fastest approach.
Module 5 Planning for SQL Server® 2008 R2 Indexing.
Applications hitting a wall today with SQL Server Locking/Latching Scale-up Throughput or latency SLA Applications which do not use SQL Server.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Oracle tuning: a tutorial Saikat Chakraborty. Introduction In this session we will try to learn how to write optimized SQL statements in Oracle 8i We.
Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team Feb, SQL Server 2000.
Indexes / Session 2/ 1 of 36 Session 2 Module 3: Types of Indexes Module 4: Maintaining Indexes.
Query Optimizer Execution Plan Cost Model Joe Chang
© IBM Corporation 2005 Informix User Forum 2005 John F. Miller III Explaining SQLEXPLAIN ®
Maciej Pilecki | Project Botticelli Ltd.. SELECT Bio FROM Speakers WHERE FullName=‘Maciej Pilecki’;  Microsoft Certified Trainer since 2001  SQL Server.
Meta Data Cardinality Explored CSSQLUG User Group - June 2009.
Pinal Dave Mentor | Solid Quality India |
Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to.
8 Copyright © 2005, Oracle. All rights reserved. Gathering Statistics.
SQL SERVER MAINTENANCE PLANS Kat
Dave LinkedIn
How to kill SQL Server Performance Håkan Winther.
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP,MCP. SQL SERVER Database Administration.
Scott Fallen Sales Engineer, SQL Sentry Blog: scottfallen.blogspot.com.
Module 6: Creating and Maintaining Indexes. Overview Creating Indexes Understanding Index Creation Options Maintaining Indexes Introducing Statistics.
Diving into Query Execution Plans ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
Session Name Pelin ATICI SQL Premier Field Engineer.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
SQL Server Statistics and its relationship with Query Optimizer
ASE Optdiag Features including dynamic_histogram
Query Optimization Techniques
Execution Planning for Success
SQL Server Statistics 101 Travis Whitley Senior Consultant, Oakwood Systems whitleysql.wordpress.com.
Stored Procedures – Facts and Myths
Query Tuning without Production Data
Finding more space for your tight environment
Query Tuning without Production Data
Query Tuning without Production Data
Reading execution plans successfully
Reading Execution Plans Successfully
Statistics for beginners
Statistics And New Cardinality Estimator (CE)
Statistics for beginners
Now where does THAT estimate come from?
Cardinality Estimator 2014/2016
Query Optimization Statistics: The Driving Force Behind Good Performance G. Vern Rabe -
Statistics What are the chances
Query Optimization Techniques
JULIE McLAIN-HARPER LINKEDIN: JM HARPER
Execution Plans Demystified
Statistics: What are they and How do I use them
Reading Execution Plans Successfully
Hugo Kornelis Now where does THAT estimate come from? The nuts and bolts of cardinality estimation.
SQL Server Query Plans Journeyman and Beyond
Transact SQL Performance Tips
Diving into Query Execution Plans
Introduction to Execution Plans
Query Optimization Techniques
Reading execution plans successfully
T-SQL Basics: Coding for performance
Introduction to Execution Plans
Presentation transcript:

SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP SQL SERVER Database Administration

SQL Server Statistics What are SQL Server STATISTICS? What do they look like? When\How do they get created? When\How do they get updated? When SQL Server KICKs Off AUTO UPDATES? SQL Server Statistics Best Practices. SQL Server Statistics Maintenance. AGENDA

SQL Server Statistics New Terms Statistics Header Vector Density Histogram Cardinality

SQL Server Statistics Statistics contain a map of the data distribution for a column or an Index. The query optimizer uses these statistics to estimate the cardinality. cardinality :The total number of rows processed at each level of a query plan. What are SQL Server STATISTICS?

SQL Server Statistics These cardinality estimates enable the query optimizer to create a high-quality query plan. SQL Server estimates cardinalities come primarily from histograms A histogram with distribution of values in the first column. A histogram measures the frequency of occurrence for each distinct value in a data set What are SQL Server STATISTICS?

SQL Server Statistics Statistics Stats – Header Vector Density HISTOGRAM

SQL Server Statistics Histogram

SQL Server Statistics The range includes all possible column values between boundary values, excluding the boundary values themselves. The lowest of the sorted column values is the upper boundary value for the first histogram step

SQL Server Statistics STMT Syntax check (Parsing)Algebrizer (Binding) Optimizer Plan Execution Result Estimated Row Count Hardware Configuration Query Hints Indexes Partitioning Filegroups/Files

SQL Server Statistics The Different way stats retrieved DBCC SHOW_STATISTICS (tablename.statsname) Ex: DBCC SHOW_STATISTICS (STATSIMPACT,IxSTATSIMPACT_Colx ) SP_Autostats table name Ex: SP_Autostats [STATSIMPACT] Sys.indexes Sys.stats sys.dm_db_stats_properties (object_id, stateside) How do you see them?

SQL Server Statistics The Different way stats being created The query optimizer creates statistics for indexes on tables when the index is created. Index statistics are created on the key or Lead columns of the index. The query optimizer creates statistics for single columns in query predicates. Composite indexes creates Multi column statistics. sp_createstats stored procedure Create Statistics Statement Creating Statistics

SQL Server Statistics Detailed look at the utilization of statistics. How SQL Server Thinks about Stats? How SQL Server Comes to a conclusion? DEMO -1 How SQL Server STATISTICS Impacts?

SQL Server Statistics When STASTS Updated SQL Server update stats on default threshold (20%+500) A query compiles for the first time, and a statistic used in the plan is out of date A query has an existing query plan, but a statistic in the plan is out of date Auto_update_statistics Sp_Updatestats stored procedure Update statistics Command Updating Statistics

When Stats Not Updated Index reorg will not Update index linked stats Auto create will not work on Read-only DB’s Even if a statistic becomes outdated as the result of a modification, it will not automatically update after the modification completes. Note:The statistic will automatically update the next time a query plan uses it. SQL Server Statistics Updating Statistics

SQL Server Statistics Updating Statistics Index Rebuild SQL Server update stats on default threshold (20%+500) A query compiles for the first time, and a statistic used in the plan is out of date A query has an existing query plan, but a statistic in the plan is out of date

SQL Server Statistics When AUTO Update Kicks off Demo - 2

SQL Server Statistics How Granular are AUTO Update Statistics Or NOT Updated DEMO -3

SQL Server Statistics Even Distribution is easy ( it is fairly consistent ) Un-even distribution is HARDER This is all over the place and varies over time as well The Histogram does a much better job having steps and average distribution per step but what if there are well over 200 distinct values and millions of rows with heavy skew between steps Simply put the averages just aren’t going to cut it anymore Data Distribution Matters

SQL Server Statistics Impact of Skewed estimates 1.Spills to Disk(Under estimates) 2.IndexScan Vs. Index Seek Decisions 3.Inflated Memory Grants 4.Serial vs. Parallel Operations 5.Least Selective tables Joined first 6. outer/inner choice 7.Inappropriate Join algorithm selected Data Distribution Matters

SQL Server Statistics Skewed data Distribution

SQL Server Statistics Statistics Best Practices DEMO -4

SQL Server Statistics Optimize for (Unknown) Recompile Plan guides filtered statistics Declare local Variables. Skewed data Distribution

SQL Server Statistics 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 Eliminate any duplicate statistics If you notice estimate VS actual are off while troubleshooting, try updating the statistics Make Sure Your Data is not SKEWED (Important) Statistics Best Practices

SQL Server Statistics Schedule an Update statistics job with large sample or FULLSCAN if feasible Nightly for larger/OLTP tables Weekly for smaller, less updated tables Statistics Maintenance

So the Auto Update stats will fire for every % change in table rows. Of course, we have an improved algorithm in SQL 2012 which is SQRT(1000 * Table rows) which is much better. When it fires it will use the default sampling rate and here is the algorithm how it calculates the sampling rate. 1) If the table < 8MB then it updates the statistics with a full scan. 2) If the table > 8MB, it follows an algorithm. It reduces the sampling rate as the number of rows in the table are increased to make sure we are not scanning too much data. This is not a fixed value but is under the control of optimizer. It is not a linear algorithm either. Example: if we have 1,000,000 rows it would use a sampling rate of 30% but when the number of rows increase to 8,000,000 it would reduce the sampling rate to 10%. These sampling rates are not under the DBAs control but optimizer decides it. SQL Server Statistics SQL Server 2012 algorithm

SQL Server Statistics Q&A