Dave Ballantyne Clear Sky SQL. ›Freelance Database Developer/Designer –Specializing in SQL Server for 15+ years ›SQLLunch –Lunchtime usergroup –London.

Slides:



Advertisements
Similar presentations
SQL Server performance tuning basics
Advertisements

CC SQL Utilities.
Cardinality How many rows? Distribution How many distinct values? density How many rows for each distinct value? Used by optimizer A histogram 200 steps.
Query Optimization CS634 Lecture 12, Mar 12, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
SQL Performance 2011/12 Joe Chang, SolidQ
Understanding Parameter Sniffing Benjamin Nevarez Blog: benjaminnevarez.com 1.
Module 13: Optimizing Query Performance. Overview Introduction to the Query Optimizer Obtaining Execution Plan Information Using an Index to Cover a Query.
DM109 A Practical Guide To ASE Optimizer Statistics and Optdiag Eric Miner Development Engineer Optimizer Group
Module 12: Optimizing Query Performance. Overview Introducing the Query Optimizer Tuning Performance Using SQL Utilities Using an Index to Cover a Query.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
Database Management 9. course. Execution of queries.
Ashwani Roy Understanding Graphical Execution Plans Level 200.
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.
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Microsoft AREC TAM Internship SQL Server Performance Tuning(I) Haijun Yang AREC SQL Support Team Feb, SQL Server 2000.
Connect with life Nauzad Kapadia Quartz Systems
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
DAT410 SQL Server 2005 Optimizing Procedural Code Kimberly L. Tripp President/Founder, SQLskills.com.
1 Chapter 9 Tuning Table Access. 2 Overview Improve performance of access to single table Explain access methods – Full Table Scan – Index – Partition-level.
Meta Data Cardinality Explored CSSQLUG User Group - June 2009.
Pinal Dave Mentor | Solid Quality India |
SQL Query Analyzer. Graphical tool that allows you to:  Create queries and other SQL scripts and execute them against SQL Server databases. (Query window)
Thinking in Sets and SQL Query Logical Processing.
Dave LinkedIn
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP,MCP. SQL SERVER Database Administration.
APRIL 13 th Introduction About me Duško Mirković 7 years of experience.
Scott Fallen Sales Engineer, SQL Sentry Blog: scottfallen.blogspot.com.
Execution Plans Detail From Zero to Hero İsmail Adar.
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP SQL SERVER Database Administration.
Dynamic SQL Writing Efficient Queries on the Fly ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
Diving into Query Execution Plans ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
Improve query performance with the new SQL Server 2016 query store!! Michelle Gutzait Principal Consultant at
Session Name Pelin ATICI SQL Premier Field Engineer.
SQL Server Magic Buttons! What are Trace Flags and why should I care? Steinar Andersen, SQL Service Nordic AB Thanks to Thomas Kejser for peer-reviewing.
SQL Server Statistics and its relationship with Query Optimizer
Parameter Sniffing in SQL Server Stored Procedures
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
Query Tuning without Production Data
Reading execution plans successfully
Dynamic SQL Writing Efficient Queries on the Fly
Reading Execution Plans Successfully
Introduction to Execution Plans
Query Execution Expectation-Reality Denis Reznik
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
SQL Server 2016 Query Data Store
Statistics: What are they and How do I use them
Steve Hood SimpleSQLServer.com
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
Ascending Key Problem in SQL Server Large Tables
Introduction to Execution Plans
Parameter Sniffing on SQL Server
Introduction to Execution Plans
Query Optimization Techniques
Reading execution plans successfully
Introduction to Execution Plans
Presentation transcript:

Dave Ballantyne Clear Sky SQL

›Freelance Database Developer/Designer –Specializing in SQL Server for 15+ years ›SQLLunch –Lunchtime usergroup –London & Cardiff, 2 nd & 4 th Tuesdays ›TSQL Smells script author – › ›Twitter

›This is also me –Far to often …. ›Estimates are central ›Statistics provide estimates

›Every Journey starts with a plan ›Is this the ‘best’ way to Lyon? –Fastest –Most efficient –Shortest ›SQL Server make similar choices

›SQL Server is a cost based optimizer –Cost based = compares predicted costs ›Therefore estimations are needed ›Every choice is based on these estimations ›Has to form a plan –And the plan cannot change in execution if ‘wrong’

›Costs are not actual costs –Little or no relevance to the execution costs ›Cost is not a metric –1 <> 1 anything ›Their purpose: –Pick between different candidate plans for a query

›Included within a index ›Auto Updated and created –Optimizer decides “It would be useful if I knew..” –Only on single column –Not in read-only databases ›Can be manually updated ›Auto-Creation can operate Async

›DBCC SHOW_STATISTICS(tbl,stat) –WITH STAT_HEADER ›Display statistics header information –WITH HISTOGRAM ›Display detailed step information –WITH DENSITY_VECTORS ›Display only density of columns – Density = rows / count of distinct values – WITH STATS_STREAM ›Binary stats blob ( not supported )

Total rows in table Rows read and sampled No of steps in histogram – Max 200 Density (Rows/Distinct values) (exc boundaries) not used Avg byte len for all columns

 Each step contains data on a range of values  Range is defined by ◦ <= RANGE_HI_KEY ◦ > Previous range RANGE_HI_KEY Row 3 > 2 and <=407 Row 3 > 2 and <=407 Row 4 > 407 and <=470 Row 4 > 407 and <=470 6 rows of data = Rows of data > 407 and < Rows of data > 407 and < Distinct values > 407 and < Distinct values > 407 and < 470 Density (17 / 9)

RANGE_HI_KEY <= Predicate AND > Previous RANGE_HI_KEY RANGE_HI_KEY <= Predicate AND > Previous RANGE_HI_KEY As Predicate == RANGE_HI_KEY Estimate = EQ_ROWS As Predicate == RANGE_HI_KEY Estimate = EQ_ROWS

RANGE_HI_KEY <= Predicate And > previous RANGE_HI_KEY RANGE_HI_KEY <= Predicate And > previous RANGE_HI_KEY As Predicate < RANGE_HI_KEY Estimate = AVG_RANGE_ROWS As Predicate < RANGE_HI_KEY Estimate = AVG_RANGE_ROWS

›Greater accuracy on Range boundary values –Based upon the ‘maxdiff’ algorithm ›Relevant for leading column –Estimate for Smiths –But not Smiths called John ›Additional Columns cumulative density only –1/(Count of Distinct Values)

Density vector = 1/(Count of Distinct Values) =19,517 1 / 19,517 = ~ E-05 =19,517 1 / 19,517 = ~ E-05

211 * E-05 = ~ * E-05 = ~

›All Diazs will estimate to the same: –As will all Smiths,Jones & Ballantynes –The statistics do not contain detail on FirstName –Only how many distinct values there are –And assumes these are evenly distributed ›Not only across a single Surname ›But ALL Surnames

›So far we have only used a single statistic for estimations ›For this query: ›To provide the best estimate the optimizer ideally needs to know about LastName and FirstName

›Correlating Multiple Stats ›AND conditions ›Find the intersection LastName = ‘Sanchez’ FirstName = ‘Ken’

›And logic –Intersection Est = ( Density 1 * Density 2) ›Or logic –Row Est 1 + Row Est 2 –(Intersection Estimate) –Avoid double counting

10% * 10 % = 1% 10% * 20 % = 2% No Correlation in the data is assumed

To keep statistics fresh they get ‘aged’ 0 to > 0 <= 6 Rows (For Temp Tables) 6 Modification <= 500 Rows 500 Modifications >= 501 Rows % of table Will cause statistics to be updated on next use Will cause statements to be recompiled on next execution Temp tables in stored procedures more complexcomplex

 Trace flag Dynamically lower statistics update threshold for large tables  >25,000 rows  2008r2 (SP1) & 2012

›When density vector is not accurate enough ›Manually created statistics only ›Additional where clause can be utilised Filter Expression = Filter Unfiltered Rows = Total rows in table before filter Filter Expression = Filter Unfiltered Rows = Total rows in table before filter Rows Sampled Number of filtered rows sampled Rows Sampled Number of filtered rows sampled

Density of London * Density of Ramos Filter is matched and histogram is used

›For ‘large’ data sets a smaller sample can be used ›Here 100% of the rows have been sampled ›Here ~52% of the rows have been sampled ›Statistics will assume the same distribution of values through the entire dataset

›Also Auto/Forced Parameterization

›Remember the Density Vector ? ›19972 (Total Rows )* = › On Equality The Average Density Is Assumed

›Stored Procedures

›Enables a better plan to be built –(most of the time) –Uses specific values rather than average values ›Values can be seen in properties pane ›Erratic execution costs are often Parameter Sniffing problems

›Force a value to be used in optimization ›A literal value ›Or UNKNOWN –Falls back to density information

›OPTION(RECOMPILE) –Recompile on every execution ›Because the plans aren’t cached –No point as by definition the plan wont be reused ›Uses variables as if literals –More accurate estimates

›The Achilles heel –A plan is fixed ›But… The facts are: –More Smiths than Ballantynes –More Customers in London than Leeds LondonLeeds Smith50050 Ballantyne101

›This is known as the ‘Plan Skyline’ problem

›But wait… ›It gets worse –That was only EQ_ROWS –RANGE_ROWS ??

›Variations in plans –Shape ›Which is the ‘primary’ table ? –Physical Joins –Index (non)usage ›Bookmark lookups –Memory grants ›500 rows need more memory than 50 –Parallel plans

›Can the engine resolve this ? –No! ›We can help though –And without recompiling –Aim is to prevent ‘car-crash’ queries –Not necessarily provide a ‘perfect’ plan Demo

›“There is always a trace flag” –Paul White ›TF 9292 –Show when a statistic header is read ›TF 9204 –Show when statistics have been fully loaded ›TF 8666 –Display internal debugging info in QP

›Statistics Used by the Query Optimizer in Microsoft SQL Server 2008Statistics Used by the Query Optimizer in Microsoft SQL Server 2008 ›Plan Caching in SQL Server 2008Plan Caching in SQL Server 2008 ›SQL Server internals 2008 book (MSPress)

›My ›Twitter