SQL Server uses a phase during query optimization called cardinality estimation (CE). This process makes estimates based on the statistics as to how many rows flow from one query plan iterator to the next. Knowing how CE generates these numbers, will enable you to write better TSQL code and in turn influence type of physical operations during a query execution. Based on that estimated rows, the query processor decides how to access an object, which physical join to use, how to sort the data. Do you know how the CE generates these numbers? If your query has only one predicate, the query optimizer will use the histogram to estimate how many rows will be qualified. What happen when you have multiple predicates, range predicates, variable values that are “NOT KNOWN” to the optimizer or you have predicate values increasing in ascending order? Do you know what will happen if your predicate is using a value that is outside of histogram range? In this session, I will show you how cardinality estimator estimates in all of these scenarios. You will walk out of this session with a clear understanding of how the CE generates its numbers and ready to tackle those nasty, hard to solve query plans.
2. • 11 years as DBA
• MCSA SQL Server 2012/2014
• 10 years in Merchant Marine
Work
• Running—One 26.2 and Many 13.1
• Shuttling 3 kids
Outside Work
• Frequent speaker local user groups & SQL Saturdays
• Answering question at #sqlhelp & dba.stackexchange
• Blog at sqlworldwide.com
Giving Back
6. Density
How often duplicate values occur in a column.
1__________________________________
[# of distinct values in a column]
Density
663 distinct customerID
1/663=0.00150830
7. Predicate Selectivity
Most commonly used to describe a predicate.
[ # rows that pass the predicate ]__________________________________
[total number of rows]
75 rows for
customerID 577
73595 total number
of rows.
75/73595=.0010
8. Cardinality
• Number of rows returned by a query operator.
For Us
• Uniqueness of data values contained in a particular
column (attribute) of a database table.
Structured Query Language
• Cardinality of a set is a measure of the “number
of elements of the set”.
Math
12. DBCC SHOW_STATISTICS
• Meta data about the statistics.
Header
• How many unique values are present within a
column or columns?
DensityVector
• Frequency of data within the first key column
of the statistics.
Histogram
22. Resource
▪ QueryTuning Fundamentals
▪ DBCC SHOW_STATISTICS (Transact-SQL)
▪ 13ThingsYou Should Know About Statistics and the Query
Optimizer
▪ Cardinality Estimation for Multiple Predicates
▪ NewTrace Flag to FixTableVariable Performance
▪ Ascending key IssueTF 2389 and 2390
▪ Optimizing Query Plans with the SQL Server 2014
Cardinality Estimator
23. Resource
▪ QueryTuning Fundamentals
▪ DBCC SHOW_STATISTICS (Transact-SQL)
▪ 13ThingsYou Should Know About Statistics and the Query
Optimizer
▪ Cardinality Estimation for Multiple Predicates
▪ NewTrace Flag to FixTableVariable Performance
▪ Ascending key IssueTF 2389 and 2390
▪ Optimizing Query Plans with the SQL Server 2014
Cardinality Estimator