Download presentation
Presentation is loading. Please wait.
Published byEthelbert Gordon Modified over 6 years ago
1
Decoding the Cardinality Estimator to Speed Up Queries
Joe Obbish Decoding the Cardinality Estimator to Speed Up Queries
2
About Me Business Intelligence Developer for EHR company
Answer questions on dba.stackexchange.com Blog about SQL Server at Done performance tuning for thousands of queries
3
This Slide Intentionally Left Blank
4
Why are Some Queries Always Slow?
Data model or indexing problems Unreasonable questions – “It’s slow if I cross join 2 tables with a million rows each” Cardinality estimate issues
5
What is a Cardinality Estimate?
The estimated number of rows output by an operator in a query plan.
6
Why is This Important? Query plans are chosen on cost
Operator costs are mostly determined by estimates Key estimates need to be close enough Good performance with bad estimates often comes down to luck or processing power
7
Poor Estimates Often Lead to Poor Plans
Wrong table access type Wrong join type Wrong join order Spills to tempdb And much more!
8
The Root Cause of Poor Estimates
I try to find the root cause of cardinality estimate issues when I can. Goal isn’t to understand or memorize every detail, but to have a strong foundation to tackle future problems. The community can help you.
9
Basic Principles Use available statistics objects
Assume data is uniform Always produce an estimate Never estimate 0 rows Don’t require consistency
10
Statistics Use statistics instead of looking at table data
11
Data is Uniform There’s no data skew
Histogram is the truth, the whole truth, and nothing but the truth Somewhat relaxed with new CE or with some trace flags
12
Always Produce an Estimate
CE always produces an estimate, even if it has to guess Common guesses: 30% - unknown inequality 9% or 16.4% for BETWEEN (legacy and new CE) 10% for some other cases
13
Minimum Estimate You’ll never see a cardinality estimate of 0 rows
Optimizer assumes that you’re always looking for data that exists Costing plans with 0 row estimates doesn’t really make sense
14
Inconsistency Cardinality estimates do not need to be consistent between plans Filters with > and <= can both give 30%, even though they must add up to 100% (ignoring NULLs)
15
Academic Demos All testing done on the new CE in SQL Server 2017
SQL Server 2008 R2 and 2012 only have the legacy CE
16
Our Statistics
17
Example 1 SELECT * FROM T WHERE ID = 1;
18
Example 1 Solution SELECT * FROM T WHERE ID = 1;
19
Example 2 SELECT * FROM T WHERE ID = 2;
20
Example 2 Solution SELECT * FROM T WHERE ID = 2;
21
Example 3 SELECT * FROM T WHERE ID = 7;
22
Example 3 Solution SELECT * FROM T WHERE ID = 7;
23
Example 4 DECLARE @local_variable BIGINT = 1; SELECT * FROM T
WHERE ID
24
Example 4 Solution DECLARE @local_variable BIGINT = 1; SELECT * FROM T
WHERE ID
25
Example 5 SELECT * FROM T WHERE ID > 1;
26
Example 5 Solution SELECT * FROM T WHERE ID > 1;
27
Example 6 DECLARE @local_variable BIGINT = 1; SELECT * FROM T
WHERE ID
28
Example 6 Solution DECLARE @local_variable BIGINT = 1; SELECT * FROM T
WHERE ID
29
Example 7 DECLARE @local_variable BIGINT = 1; SELECT * FROM T
WHERE ID
30
Example 7 Solution DECLARE @local_variable BIGINT = 1; SELECT * FROM T
WHERE ID
31
Example 8 Solution SELECT ID FROM T GROUP BY ID;
32
Example 9 SELECT * FROM T WHERE ABS(ID) = -1;
33
Example 9 Solution SELECT * FROM T WHERE ABS(ID) = -1;
34
Filter Independence How should filters on different columns be combined? Is there some level of correlation between the columns or are they independent?
35
“Real World” Demos
36
Predicting Problems Window functions Column correlation
Other functions Local variables with no RECOMPILE hint Filtering on aggregates Undeclared relationships in data Data skew
37
Check For CE Problems Easiest way is to look at actual plans and use what you know about the data Is that estimate of 1 row reasonable?
38
Tricky Plans Nested loop joins
39
Bitmap filters
40
Columnstore aggregate pushdown
41
Review Try to figure out the root cause of a cardinality estimate issue that causes performance problems Recognize patterns Know if the problem might come back
42
Resources BOL Cardinality Estimation White Paper by Joe Sack on New CE
CE Questions on Stack Exchange Blog post on containment Blog post by Paul White on DISTINCT estimates Blog post by Dmitry Pilugin on Join Estimation Internals
43
Please Fill Out Feedback Forms
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.