Upload
solarwinds
View
571
Download
2
Embed Size (px)
Citation preview
Cardinality Estimates in SQL Server 2014
Thomas LaRock
Head Geek
Solarwinds
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Why Are You Here?
» You are thinking about migrating/upgrading to SQL 2014 at some point
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Why Are You Here?
» You are thinking about migrating/upgrading to SQL 2014 at some point
» You’ve been told to migrate to SQL 2014
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Why Are You Here?
» You are thinking about migrating/upgrading to SQL 2014 at some point
» You’ve been told to migrate to SQL 2014
» You want to understand more about specific changes to cardinality estimates
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
A Little About Me…
@sqlrockstar
sqlrockstar@
thomaslarock.com
linkedin.com/in/sqlrockstar
solarwinds.com/products
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
A Little About Solarwinds…
@headgeeks
lab.solarwinds.com
confio.com/events
» Why cardinality matters
» What’s new in SQL 2014
» See the new CE in action
» Questions
Agenda
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Cardinality: Why It Matters
How most people see SQL Server
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
How most people see SQL Server
QUERY
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
How most people see SQL Server
QUERY DATA
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
What’s inside the box?
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
What Happens When a Connection Is Made to SQL Server
Connection
• Session ID
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
What Happens When a Connection Is Made to SQL Server
Connection
• Session ID
Request
• Query sent
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
What Happens When a Request Is Sent To SQL Server
Parse
Syntax check;
reserved keywords,
column and table names
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
What Happens When a Request Is Sent To SQL Server
Parse
Syntax check;
reserved keywords,
column and table names
Bind
Name reservation,
type derivation, aggregates,
grouping
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
What Happens When a Request Is Sent To SQL Server
Parse
Syntax check;
reserved keywords,
column and table names
Bind
Name reservation,
type derivation, aggregates,
grouping
Optimize
Find “good enough”
plan
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
What Happens When a Request Is Sent To SQL Server
Parse
Syntax check;
reserved keywords,
column and table names
Bind
Name reservation,
type derivation, aggregates,
grouping
Optimize
Find “good enough”
plan
Execute
Storage Engine
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
What Happens When a Request Is Sent To SQL Server
Parse
Syntax check;
reserved keywords,
column and table names
Bind
Name reservation,
type derivation, aggregates,
grouping
Optimize
Find “good enough”
plan
Execute
Storage Engine
Logical Engine
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
What Happens When a Request Is Sent To SQL Server
Parse
Syntax check;
reserved keywords,
column and table names
Bind
Name reservation,
type derivation, aggregates,
grouping
Optimize
Find “good enough”
plan
Execute
Storage Engine
Logical Engine
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
What Happens When a Connection Is Made to SQL Server
Connection
• Session ID
Request
• Query sent
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
What Happens When a Connection Is Made to SQL Server
Connection
• Session ID
Request
• Query sent
Optimize
• Optimization
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
» Primary goal: To find plan with “least cost”
What Happens During Query Optimization in SQL Server
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
» Primary goal: To find plan with “least cost”
» Will stop when “good enough” plan is found
What Happens During Query Optimization in SQL Server
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
» Primary goal: To find plan with “least cost”
» Will stop when “good enough” plan is found
» Uses statistics(!) to estimate rows needed for operators in the execution plan
What Happens During Query Optimization in SQL Server
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
» Primary goal: To find plan with “least cost”
» Will stop when “good enough” plan is found
» Uses statistics(!) to estimate rows needed for operators in the execution plan
» The cost is an abstraction, no correlation to performance (ie., CPU ticks)
What Happens During Query Optimization in SQL Server
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
» Primary goal: To find plan with “least cost”
» Will stop when “good enough” plan is found
» Uses statistics(!) to estimate rows needed for operators in the execution plan
» The cost is an abstraction, no correlation to performance (ie., CPU ticks)
What Happens During Query Optimization in SQL Server
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Query Optimization Model
» Contents are a mystery
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Query Optimization Model
» Contents are a mystery (except to Conor and his team)
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Query Optimization Model
» Contents are a mystery (except to Conor and his team)
» You don’t need the details
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Query Optimization Model
» Contents are a mystery (except to Conor and his team)
» You don’t need the details (no, you don’t)
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Query Optimization Model
» Contents are a mystery (except to Conor and his team)
» You don’t need the details (no, you don’t)
» You need the bigger picture
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
1. Does plan already exist? In not, continue to next step.
The bigger picture
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
1. Does plan already exist? In not, continue to next step.
2. Is this a trivial plan? If not, continue.
The bigger picture
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
1. Does plan already exist? In not, continue to next step.
2. Is this a trivial plan? If not, continue.
3. Apply simplification.
The bigger picture
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
1. Does plan already exist? In not, continue to next step.
2. Is this a trivial plan? If not, continue.
3. Apply simplification.
4. Is this plan cheap enough? If not, continue.
The bigger picture
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
1. Does plan already exist? In not, continue to next step.
2. Is this a trivial plan? If not, continue.
3. Apply simplification.
4. Is this plan cheap enough? If not, continue.
5. Start cost-based optimization
The bigger picture
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
1. Does plan already exist? In not, continue to next step.
2. Is this a trivial plan? If not, continue.
3. Apply simplification.
4. Is this plan cheap enough? If not, continue.
5. Start cost-based optimization
The bigger picture
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
» Explore basic rules
Cost-based optimization
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
» Explore basic rules
» Does plan have a cost less than 0.2?
Cost-based optimization
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
» Explore basic rules
» Does plan have a cost less than 0.2? Explore more rules, alternate join ordering. If the best plan is less than
1.0, use that plan. If not, but MAXDOP is > 0 and this is an SMP system and the min cost > cost for parallelism, then use a parallel plan. Compare the cost of parallel plan to best serial plan and pass the cheaper of the plans along.
Cost-based optimization
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
» Explore basic rules
» Does plan have a cost less than 0.2? Explore more rules, alternate join ordering. If the best plan is less than
1.0, use that plan. If not, but MAXDOP is > 0 and this is an SMP system and the min cost > cost for parallelism, then use a parallel plan. Compare the cost of parallel plan to best serial plan and pass the cheaper of the plans along.
» Opt for the cheapest plan after a limited number of explorations
Cost-based optimization
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Why This Matters
» Query optimizer contains the Cardinality Estimator (CE)
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Why This Matters
» Query optimizer contains the Cardinality Estimator (CE)
» The CE is dependent upon statistics being current and accurate
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Why This Matters
» Query optimizer contains the Cardinality Estimator (CE)
» The CE is dependent upon statistics being current and accurate
» If stats are missing, incorrect, or invalid then you are likely to get a sub-optimal plan
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
What’s inside the box?
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
The CE is the DNA for your queries!
» DNA is what makes you you
» DNA not easily seen or understood
» CE is similar to DNA
» Tiny but BIG role
» Neither DNA nor CE are trivial pieces of code
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
What’s New In SQL Server 2014
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
» There have been slight enhancements over the years Hotfixes, patches, trace flags
The CE has been unchanged since 7.0!
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
» There have been slight enhancements over the years Hotfixes, patches, trace flags
» Goal for SQL2014 was not to avoid regression
The CE has been unchanged since 7.0!
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
» There have been slight enhancements over the years Hotfixes, patches, trace flags
» Goal for SQL2014 was not to avoid regression
» It is a new CE Will work better for most queries
May not work well for some
Testing is vital!
The CE has been unchanged since 7.0!
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Legacy CE assumptions
Uniformity Independence
Containment Inclusion
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Legacy CE assumptions
Uniformity Independence
Containment Inclusion
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Legacy CE assumptions
Uniformity Independence
Containment Inclusion
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Legacy CE assumptions
Uniformity Independence
Containment Inclusion
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
See the New CE in Action
DEMO
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Likely common scenarios
ASC/DESC Key Estimates
Multiple Column Dependency
Join Estimates
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Likely common scenarios
ASC/DESC Key Estimates
Multiple Column Dependency
Join Estimates
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Likely common scenarios
ASC/DESC Key Estimates
Multiple Column Dependency
Join Estimates
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
New CE Changes That Require Action
If there are no changes to
estimates or the query execution
plan…
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
New CE Changes That Require Action
…but you are seeing
degraded performance
If there are no changes to
estimates or the query execution
plan…
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
New CE Changes That Require Action
…it’s not related to the new CE, but
general performance
tuning may be required
…but you are seeing
degraded performance
If there are no changes to
estimates or the query execution
plan…
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
New CE Changes That Require Action
No changes to estimates but
the query execution
plan changed…
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
New CE Changes That Require Action
…and you are seeing
degraded performance
No changes to estimates but
the query execution
plan changed…
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
New CE Changes That Require Action
…unlikely to be related to the new CE, but tuning
may be required
…and you are seeing
degraded performance
No changes to estimates but
the query execution
plan changed…
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
New CE Changes That Require Action
If there are changes to
the estimates regardless of
the query execution
plan shape…
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
New CE Changes That Require Action
…and you are seeing
degraded performance
If there are changes to
the estimates regardless of
the query execution
plan shape…
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
New CE Changes That Require Action
…action may be necessary if degradation
exceeds workload
performance SLAs
…and you are seeing
degraded performance
If there are changes to
the estimates regardless of
the query execution
plan shape…
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Tips & Tricks
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Tips and Tricks
Auto Create/Update stats
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Tips and Tricks
Auto Create/Update stats
Database compatibility mode
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Tips and Tricks
Auto Create/Update stats
Database compatibility mode
Query trace flags
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Tips and Tricks
Auto Create/Update stats
Database compatibility mode
Query trace flags
XML showplan, xEvents
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
» Why cardinality matters
» What’s new in SQL 2014
» See the new CE in action
» Questions
Agenda
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
Thank You!
The SOLARWINDS and SOLARWINDS & Design marks are the exclusive property of SolarWinds Worldwide, LLC, are registered with the U.S.
Patent and Trademark Office, and may be registered or pending registration in other countries. All other SolarWinds trademarks, service
marks, and logos may be common law marks, registered or pending registration in the United States or in other countries. All other
trademarks mentioned herein are used for identification purposes only and may be or are trademarks or registered trademarks of their respective
companies.
Questions?
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.
» http://tinyurl.com/joe-sack-cardinality
» http://tinyurl.com/sql-2014-new-CE
» http://tinyurl.com/sql-2014-new-CE-p2
» http://tinyurl.com/CE-deep-dive-PWhite
» http://tinyurl.com/MSDN-CE-2014
Additional info
© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.