statistis, row counts, execution plans and query tuning

29
STATISTICS, ROW COUNTS, EXECUTION PLANS AND QUERY TUNING Grant Fritchey | Redgate Software

Upload: grant-fritchey

Post on 28-Jan-2018

126 views

Category:

Data & Analytics


1 download

TRANSCRIPT

STATISTICS, ROW COUNTS,EXECUTION PLANS AND

QUERY TUNING

Grant Fritchey | Redgate Software

GOALS

• Understand how row counts are determined with SQL Server

• Learn how row counts affect the execution plans generated

by the Query Optimizer

LET’S TALK

scarydba.com

[email protected]

@gfritchey

Grant Fritchey

THE PLAN

• Statistics in SQL Server

• Reading Statistics

• How are Rows Estimated

• Row Estimates Affect Execution Plans

• Objects That Are Weird

• More Than Just Rows

• Recommendations

• Recap

STATISTICS

The practice or science of collecting and

analyzing numerical data in large

quantities, especially for the purpose of

inferring proportions in a whole from those

in a representative sample.

STATISTICS

The practice or science of collecting and

analyzing numerical data in large

quantities, especially for the purpose of

inferring proportions in a whole from those

in a representative sample.

SQL SERVER STATISTICS

Statistics for query optimization are

objects that contain statistical

information about the distribution of

values in one or more columns of a

table or indexed view.

SQL SERVER STATISTICS

STATISTICS: HEADER

STATISTICS: DENSITY GRAPH

1.0/ COUNT(DISTINCT Column)

STATISTICS: HISTOGRAM

EXAMPLES

SQL SERVER STATISTICS: OVERVIEW

• Distribution of the data

• Shows actual row counts and estimations

• Uses mathematics to arrive at numbers

• Histogram is the most useful numbers

• Density graph is the second most useful numbers

READING STATISTICS

RANGE HI_KEY RANGE_ROWS

1 0

5 8 1

2

2

2

2

3

3

3

4

5

READING STATISTICS

RANGE HI_KEY RANGE_ROWS EQ_ROWS DISTINCT_RANGE_ROWS

1 0 1 0

5 8 3 3

1 1

5

5

5

5

RANGE HI_KEY RANGE_ROWS EQ_ROWS DISTINCT_RANGE_ROWS

1 0 1 0

5 8 3 3

READING STATISTICS

1

2

2

2

2

3

3

3

4

5

READING STATISTICS

RANGE HI_KEY RANGE_ROWS EQ_ROWS DISTINCT_RANGE_ROWS AVG_RANGE_ROWS

1 0 1 0 1

5 8 3 3 2.67

AVG_RANGE_ROWS = RANGE ROWS

DISTINCT RANGE ROWS=

8

3

EXAMPLES

HOW ROWS ARE ESTIMATED

• Range high key

• Average range rows

• Average values

• Density

• Default values

• Magic estimates

EXAMPLES

ROW ESTIMATES AFFECT EXECUTION PLANS

• Out of Date Statistics

• Missing Statistics

• Parameter Sniffing

• Functions on Columns

• Constant Folding

• Local variables

EXAMPLES

OBJECTS THAT ARE WEIRD

• Table Variables

• Multi-statement, table-valued, user-defined, functions

• XML

• Columnstore

• Constraints

• Foreign Keys

• Joins

EXAMPLES

MORE THAN JUST ROWS

• Memory allocation

ꟷ Overestimates

ꟷ Underestimates

• Tempdb

• I/O

RECOMMENDATIONS

• Enable auto creation and auto update of statistics on the vast majority of systems

• Set database compatibility mode to the latest version

• Manually maintain statistics as necessary

• Enable Traceflag 2371

• Experiment with asynchronous statistics update

• Where necessary adjust statistics sample rate

• Use parameters and constant values

• Avoid operators and functions that limit or prevent statistics use

• Experiment with filtered statistics where applicable

• Use primary keys, foreign keys, constraints

• Ensure WITH CHECK is enabled and enforced

RESOURCES

• SQL Server 2014 Cardinality Estimator, Joe Sack, et al

• 13 Things You Should Know About Statistics, Fabiano Amorim

• Constant Folding and Expression Evaluation During Cardinality

Estimation, Microsoft

• Cardinality Estimation Place in the Optimization Process, Dimitry

Pilugin

• Cardinality Estimation Concepts, Dimitry Pilugin

• Cardinality Estimation Process, Dimitry Pilugin (just read all his stuff)

GOALS

• Understand how row counts are determined with SQL Server

• Learn how row counts affect the execution plans generated

by the Query Optimizer

LET’S TALK

scarydba.com

[email protected]

@gfritchey

Grant Fritchey