Sei sulla pagina 1di 31

Module 6

Statistics and Index Internals

Module Overview

Statistics Internals and Cardinality Estimation

Index Internals

Columnstore Indexes

Lesson 1: Statistics Internals and Cardinality Estimation

Cost-Based Optimization

Predicate Selectivity

Inspecting Statistics

Cardinality Estimation

Creating Statistics

Updating Statistics

Filtered Statistics

Demonstration: Analyzing Cardinality Estimation

Cost-Based Optimization

Cost-based optimization selects the query

execution plan with the lowest estimated cost for


Statistics are a critical part of cost-based


Statistics provide information about data distribution in

a column or group of columns

Columns in tables and indexes might have statistics

Predicate Selectivity


Expressions which evaluate to true or false

Found in joins, WHERE and HAVING clauses

Predicate Selectivity

The number of rows from a table that meet a predicate

High selectivity = small percentage of rows returned

Low selectivity = large percentage of rows returned

Predicate Selectivity in Query Optimization

Selectivity used to sequence join operations and choose between scan/seek

Inspecting Statistics

Statistics header

Statistics metadata

Density vector

Measure of uniqueness


Data distribution, in up to 200 steps


Cardinality Estimation

In SQL Server, cardinality estimation attempts to predict the number of rows returned by a query or query operator SQL Server 2014 and SQL Server 2016 include rewritten cardinality estimation logic Several factors can cause poor cardinality estimation:

Out-of-date or missing statistics

Functions in predicates

Table variables

Creating Statistics

Automatic creation:


Single column statistics only

Names start _WA…

Manual creation:



Updating Statistics

Automatic update:


Automatic update threshold


Manual update:



Updating statistics causes query plans to be


Filtered Statistics


Some limitations:

Limited benefit from AUTO_UPDATE_STATISTICS

Limited to simple comparison logic

Cannot be created on all data/column types

Cannot be created on indexed views

Histogram has finer resolution

Demonstration: Analyzing Cardinality Estimation

In this demonstration, you will see:

Worked examples of cardinality estimation

Lesson 2: Index Internals

Heap Internals

Index Structure

Picking the Right Index Key

Single Column and Multicolumn Indexes

Filtered Indexes

The Query Optimizer’s Choice of Indexes

Data Modification Internals

Index Fragmentation

Index Column Order

Identify and Create Missing Indexes

Demonstration: Picking the Right Index Key

Heap Internals

A table without a clustered index is a heap IAM pages contain pointers to extents containing heap data

The IAM is the only link between pages in a heap; row data is unordered

Heap rows are identified by a row identifier (RID)

Nonclustered indexes on a heap contain RID pointers

Index Structure


Self-balancing tree data structure

One root node; many non-leaf nodes; many leaf nodes

Clustered and nonclustered indexes are b-trees

Index key:

Defines the order of data in an index

Clustered index:

Root node and non-leaf nodes contain index pages; leaf nodes contain data pages

Index order = table data order

Nonclustered index:

All nodes contain index pages

XML and spatial data types have special indexes

Picking the Right Index Key

Clustered index key criteria:






Nonclustered index criteria:

Frequently used predicates

Join columns

Aggregate queries

Avoid redundant/duplicate indexes, wide keys, indexes serving only one query for systems with a lot of data change

Single Column and Multicolumn Indexes

Single column index:

Each predicate column has its own index Less performance gain, but more reusable

Multicolumn index:

All predicate columns are included in the key of the


Greatest performance gain, but limited reusability

Filtered Indexes

Nonclustered index with a filter predicate:

Filter predicate defined with a WHERE clause in the index definition

Better performance than a whole-table index:

Finer-grained statistics

Smaller size

Suitable when table data is queried in clearly identifiable subsets Some restrictions apply

The Query Optimizer’s Choice of Indexes

Data access methods for indexes

Table scan

Clustered index access

Nonclustered index seek on heap

Nonclustered index seek on non-heap

Covering nonclustered index

Predicate SARGability:

WHERE <column> <operator> <value> <column> must appear alone

Leading string wildcards prevent index seek

Data Modification Internals


Key reference removed from leaf-level page A page with no references is removed from the b-tree


New key reference added to either:

Existing free space on a page

New page


A delete followed by an insert

Index Fragmentation

Index Fragmentation

Index Column Order

SELECT performance:

Only the first column has a histogram Use the most selective column as the first column, but consider how the index will be used

INSERT performance:

Consider the effect column order will have on INSERT

performance for a clustered multicolumn index

Identify and Create Missing Indexes

Query Plans

Query Store

Database Engine Tuning Advisor

Missing index DMVs

Tools will not:

Suggest clustered indexes

Suggest modifications to existing indexes

Analyze column order in multicolumn indexes

Suggest filtered indexes

Demonstration: Picking the Right Index Key

In this demonstration, you will see:

The effects of selecting different data types as a clustered index key

Lesson 3: Columnstore Indexes

What Is a Columnstore Index?

Columnstore Index Features

Columnstore Index Internals

Demonstration: Implementing a Columnstore


What Is a Columnstore Index?


Page 1

Row-Based Index

What Is a Columnstore Index? Rows Page 1 Row-Based Index Page 2 Page 3 Column-Based Index

Page 2

Is a Columnstore Index? Rows Page 1 Row-Based Index Page 2 Page 3 Column-Based Index Pages

Page 3

Column-Based Index

Pages only contain one column from many rows

C1 C2 C3 C4

Columnstore Index Features

Clustered and nonclustered columnstore indexes

Both types can be updated

Combining row-based and columnstore indexes

Each table may only have one columnstore index

Filtered columnstore indexes

Columnstore index limitations

Restricted data types

Tables only (not view or indexed view)

No sparse columns

Columnstore Index Internals

Columnstore Index
Columnstore Index

Demonstration: Implementing a Columnstore Index

In this demonstration, you will see:

How to implement a clustered columnstore index

Columnstore index internals

Lab: Statistics and Index Internals

Exercise 1: Fixing Cardinality Estimation Errors Exercise 2: Improve Indexing Exercise 3: Using Columnstore Indexes

Logon Information

Virtual machine: 10987C-MIA-SQL Username: ADVENTUREWORKS\Student Password: Pa55w.rd

Estimated Time: 90 minutes.

Lab Review

In this lab, you gained practical experience of working with statistics objects to improve

cardinality estimates. You learned how to gather missing index recommendations and evaluate them

against an existing indexing strategy. You also worked with columnstore indexes.

Module Review and Takeaways

In this module, you have learned about how statistics are

used to estimate the cardinality of result sets returned by

SQL Server. You have learned how to create, view, and

update statistics objects. You have learned about the internal structure of heaps,

clustered indexes, and nonclustered indexes, and about how

index design can affect query performance.

You have also learned about the internal structure of columnstore indexes, how they differ from row-based

indexes, and about circumstances where a columnstore

index is an appropriate choice.