77
Cardinality Estimates in SQL Server 2014 Thomas LaRock Head Geek Solarwinds © 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Why SQL Server 2014 Cardinality Estimator is *the* killer feature

Embed Size (px)

Citation preview

Page 1: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

Cardinality Estimates in SQL Server 2014

Thomas LaRock

Head Geek

Solarwinds

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 2: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

Why Are You Here?

» You are thinking about migrating/upgrading to SQL 2014 at some point

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 3: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 4: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 5: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

A Little About Me…

@sqlrockstar

sqlrockstar@

thomaslarock.com

linkedin.com/in/sqlrockstar

Page 6: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

solarwinds.com/products

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

A Little About Solarwinds…

@headgeeks

lab.solarwinds.com

confio.com/events

Page 7: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

» Why cardinality matters

» What’s new in SQL 2014

» See the new CE in action

» Questions

Agenda

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 8: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Cardinality: Why It Matters

Page 9: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

How most people see SQL Server

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 10: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

How most people see SQL Server

QUERY

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 11: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

How most people see SQL Server

QUERY DATA

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 12: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

What’s inside the box?

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 13: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

What Happens When a Connection Is Made to SQL Server

Connection

• Session ID

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 14: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

What Happens When a Connection Is Made to SQL Server

Connection

• Session ID

Request

• Query sent

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 15: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 16: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 17: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 18: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 19: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 20: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 21: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

What Happens When a Connection Is Made to SQL Server

Connection

• Session ID

Request

• Query sent

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 22: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 23: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

» Primary goal: To find plan with “least cost”

What Happens During Query Optimization in SQL Server

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 24: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

» 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.

Page 25: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

» 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.

Page 26: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

» 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.

Page 27: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

» 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.

Page 28: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

Query Optimization Model

» Contents are a mystery

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 29: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

Query Optimization Model

» Contents are a mystery (except to Conor and his team)

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 30: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 31: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 32: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 33: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

1. Does plan already exist? In not, continue to next step.

The bigger picture

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 34: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 35: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 36: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 37: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 38: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 39: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

» Explore basic rules

Cost-based optimization

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 40: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

» Explore basic rules

» Does plan have a cost less than 0.2?

Cost-based optimization

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 41: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

» 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.

Page 42: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

» 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.

Page 43: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

Why This Matters

» Query optimizer contains the Cardinality Estimator (CE)

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 44: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 45: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 46: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

What’s inside the box?

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 47: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 48: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

What’s New In SQL Server 2014

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 49: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

» 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.

Page 50: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

» 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.

Page 51: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

» 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.

Page 52: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

Legacy CE assumptions

Uniformity Independence

Containment Inclusion

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 53: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

Legacy CE assumptions

Uniformity Independence

Containment Inclusion

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 54: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

Legacy CE assumptions

Uniformity Independence

Containment Inclusion

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 55: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

Legacy CE assumptions

Uniformity Independence

Containment Inclusion

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 56: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

See the New CE in Action

DEMO

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 57: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

Likely common scenarios

ASC/DESC Key Estimates

Multiple Column Dependency

Join Estimates

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 58: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

Likely common scenarios

ASC/DESC Key Estimates

Multiple Column Dependency

Join Estimates

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 59: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

Likely common scenarios

ASC/DESC Key Estimates

Multiple Column Dependency

Join Estimates

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 60: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 61: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 62: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 63: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

New CE Changes That Require Action

No changes to estimates but

the query execution

plan changed…

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 64: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 65: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 66: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 67: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 68: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

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.

Page 69: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

Tips & Tricks

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 70: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

Tips and Tricks

Auto Create/Update stats

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 71: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

Tips and Tricks

Auto Create/Update stats

Database compatibility mode

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 72: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

Tips and Tricks

Auto Create/Update stats

Database compatibility mode

Query trace flags

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 73: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

Tips and Tricks

Auto Create/Update stats

Database compatibility mode

Query trace flags

XML showplan, xEvents

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 74: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

» Why cardinality matters

» What’s new in SQL 2014

» See the new CE in action

» Questions

Agenda

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 75: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

© 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.

Page 76: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

Questions?

© 2014 SOLARWINDS WORLDWIDE, LLC. ALL RIGHTS RESERVED.

Page 77: Why SQL Server 2014 Cardinality Estimator is *the* killer feature

» 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.