Understanding indices

Post on 13-Apr-2017

379 views 0 download

Transcript of Understanding indices

Tuning blog: http://www.sqlperformance.com/

E-mail ebooks@sqlsentry.com for free copies of our $10 e-books:

“CAN’T YOU JUST PUT

AN INDEX ON IT?”

FIXING THE PROBLEM

WHAT ABOUT EUROPE?

YOU NEED THE BIG PICTURE

It’s all about DWI knowledge:

INDEXING STRATEGIESDWI

ata

orkload

nternals

YOUR HOST• Richard Douglas• Sales Engineer at SQL Sentry• Blog:

http://SQL.RichardDouglas.co.uk• Twitter: @SQLRich• Email: RDouglas@SQLSentry.com• Slides:

http://www.Slideshare.net/SQLRich

AGENDA• Pre requisites

• Understanding indexes and their architecture

• Utilising SQL Server DMO’s

• Index golden rules

• The importance of maintenance

Understanding the SQL Server Data Page

PRE REQUISITES• A data page is 8KB (8192

bytes)

• Page header is 96 bytes leaving 8096 bytes for data

• Slot array stores the logical order

Page header

1 - Cooper

1

2 - Hofstadter

2

3 - Koothrappali

3

4 - Kripke

54

5 - Wolowitz

5

INDEX ARCHITECTURE

Heaps

Clustered Indexes

Nonclustered Indexes

Nonclustered Columnstore Indexes

HEAP STRUCTURE

CLUSTERED INDEX STRUCTURE

INDEX EXAMPLE

1 Douglas2 Kline3 Bertrand4 Martin5 White6 Fallen7 Edwards

8 Ozar9 Randal10 Trip11 Cunningham12 Woody13 La Rock14 Locke

6993 Saxby6994 Fryer6995 Sabin 6996 Langford6997 Bolton6998 Fairweather6999 Whittles

1 ,1, 1008, 1, 101

YouGetTheIdea

File 1 Page 100 File 1 Page 101 File 1 Page 404 File 1 Page 911 File 1 Page 999

Nothing to see here, move along please

Please help my query performance is poor

1 ,1, 100Other records6993, 1, 999

6993, 1, 999

dbo.CustomerClustered primary key on an identity.

Lowest key

NONCLUSTERED INDEX STRUCTURE

COLUMNSTORE PAGES

INDEX OPTIONSFill factor

Pad Index

Filtered index (WHERE)

Include

Data Compression

Online

HIDDEN DANGERSVersioning

Uniqifiers

VARCHAR

NULLS

INTRODUCING INDEX DMO’S• Admin

o Sys.dm_db_index_physical_stats

• Usageo Sys.dm_db_index_usage_statso Sys.dm_db_index_operational_stats

• Missingo Sys.dm_db_missing_index_detailso Sys.dm_db_missing_index_columnso Sys.dm_db_missing_index_groupso Sys.dm_db_missing_index_group_stats

MISSING INDEX LIMITATIONS• Not intended to fine tune an indexing configuration.

• Only gathers 500 missing index groups.

• Optimal column orders for the index are not specified.

• In some scenarios it has been known to only return information on included columns.

• Returns only raw information about columns.

• Does not suggest filtered indexes.

• It can return different costs for the same missing index group that appears multiple times in XML Showplans.

• Multiple similar indexes may be returned.

• Only considers non-trivial query plans.

Bad example; Surname, Firstname, Middle Initial

GOLDEN RULES FOR CLUSTERED INDEXES

• Narrow

• Static

• Progressive

• Unique

• Fixed width

• Not Null

Impacts nonclustered indexes

Fragmentation

Space impact

This example could have 13 bytes of overhead alone;• 4 byte uniquifier• 2 byte variable offset + 6 bytes for variable length fields• 1 byte for NULL values and NULL bitmap

GOLDEN RULES FOR NONCLUSTERED INDEXES

• Have an optimal clustered key

• Not narrow

• Reduce unnecessary overheado Fixed Widtho Not null

• Consolidate

• Index foreign keys

Page split scenario

THE IMPORTANCE OF MAINTENANCE

Page headerData Row 1Data Row 2Data Row 3

Data Row 4

321

Data Row 5

5

Page header

Data Row 4Data Row 5

54

Page header

Data Row 6Data Row 7Data Row 8

Data Row 10

109876

Data Row 9

Page pointers

Two main types

FRAGMENTATION• Logical / External

“When the next logical page is not the next physical page”o Significantly reduces scan

performanceo Can cause extra IO

• Physical / Internal

“When page density is sub optimal”

o Increased storage spaceo More room required in the buffer poolo More IO’s to read in the datao More logging, bigger backups

FIXING FRAGMENTATION• Rebuild Indexes

• Reorganize Indexes

• Optionso Maintenance plano Custom/community scriptso Third party tools

• Guideline (sys.dm_db_Index_Physical_Stats provides these figures)

o 0 – 5% No actiono 5 – 30% Reorganizeo 30% + Rebuild

MAINTENANCE BEST PRACTICES• Auto Close

• Auto Shrink

• Statistics

• Recovery models

If you value your job, don’t turn the top two on.

SUMMARYArchitecture

Options

Querying

Design

Fragmentation

Maintenance

THANK YOU!• Slides will be available at http://Slideshare.net/SQLRich• More information at:

o SQLSkills, et al

• E-mail ebooks@sqlsentry.com for free copies of our e-books:o Just tell them where you met me

• My contact info for other questions:o RDouglas@SQLSentry.como Twitter: @SQLRicho Blogs: http://SQL.RichardDouglas.co.uk / http://http://

blogs.sqlsentry.com/author/RichardDouglas