Understanding indices

29
UNDERSTANDING INDICES Richard Douglas Sales Engineer, SQL Sentry Social Media: @ SQLRich , LinkedIn , Blog Info: http://blogs.sqlsentry.com/author/RichardDouglas/ ; http://SQL.RichardDouglas.co.uk

Transcript of Understanding indices

Page 2: Understanding indices

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

E-mail [email protected] for free copies of our $10 e-books:

Page 3: Understanding indices

“CAN’T YOU JUST PUT

AN INDEX ON IT?”

Page 4: Understanding indices

FIXING THE PROBLEM

Page 5: Understanding indices

WHAT ABOUT EUROPE?

Page 6: Understanding indices

YOU NEED THE BIG PICTURE

Page 7: Understanding indices

It’s all about DWI knowledge:

INDEXING STRATEGIESDWI

ata

orkload

nternals

Page 8: Understanding indices

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

http://SQL.RichardDouglas.co.uk• Twitter: @SQLRich• Email: [email protected]• Slides:

http://www.Slideshare.net/SQLRich

Page 9: Understanding indices

AGENDA• Pre requisites

• Understanding indexes and their architecture

• Utilising SQL Server DMO’s

• Index golden rules

• The importance of maintenance

Page 10: Understanding indices

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

Page 11: Understanding indices

INDEX ARCHITECTURE

Heaps

Clustered Indexes

Nonclustered Indexes

Nonclustered Columnstore Indexes

Page 12: Understanding indices

HEAP STRUCTURE

Page 13: Understanding indices

CLUSTERED INDEX STRUCTURE

Page 14: Understanding indices

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

Page 15: Understanding indices

NONCLUSTERED INDEX STRUCTURE

Page 16: Understanding indices

COLUMNSTORE PAGES

Page 17: Understanding indices

INDEX OPTIONSFill factor

Pad Index

Filtered index (WHERE)

Include

Data Compression

Online

Page 18: Understanding indices

HIDDEN DANGERSVersioning

Uniqifiers

VARCHAR

NULLS

Page 19: Understanding indices

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

Page 20: Understanding indices

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.

Page 21: Understanding indices

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

Page 22: Understanding indices

GOLDEN RULES FOR NONCLUSTERED INDEXES

• Have an optimal clustered key

• Not narrow

• Reduce unnecessary overheado Fixed Widtho Not null

• Consolidate

• Index foreign keys

Page 23: Understanding indices

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

Page 24: Understanding indices

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

Page 25: Understanding indices

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

Page 26: Understanding indices

MAINTENANCE BEST PRACTICES• Auto Close

• Auto Shrink

• Statistics

• Recovery models

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

Page 27: Understanding indices

SUMMARYArchitecture

Options

Querying

Design

Fragmentation

Maintenance

Page 28: Understanding indices
Page 29: Understanding indices

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

o SQLSkills, et al

• E-mail [email protected] for free copies of our e-books:o Just tell them where you met me

• My contact info for other questions:o [email protected] Twitter: @SQLRicho Blogs: http://SQL.RichardDouglas.co.uk / http://http://

blogs.sqlsentry.com/author/RichardDouglas