Understanding SQL Server Indexes - 2013-11-19

30
Understanding Indices Richard Douglas

description

Indexing presents daunting challenges, as it offers countless options to choose from. Even organizing indexes in an optimal fashion can be overwhelming. But with a little help from SQL Server domain expert Richard Douglas, you’ll see how to simplify indexing and improve the overall performance of your SQL Server applications. Richard will provide step-by-step guidance to help you: Find missing and unused indexes Resolve fragmentation issues Follow the golden rules of indexing And much more Read more: Presentations - 2013 | Richard Douglas - SQL Server Professional http://sql.richarddouglas.co.uk/presentations/presentations-2013#ixzz2l61TSSrj Follow us: @SQLRich on Twitter

Transcript of Understanding SQL Server Indexes - 2013-11-19

Page 1: Understanding SQL Server Indexes - 2013-11-19

Understanding IndicesRichard Douglas

Page 2: Understanding SQL Server Indexes - 2013-11-19

2 Dell SoftwareUnderstanding Indices

“Can’t you just put an index on it?”

(Name withheld to protect the guilty)

Page 3: Understanding SQL Server Indexes - 2013-11-19

3 Dell SoftwareUnderstanding Indices

Fixing the problem

Page 4: Understanding SQL Server Indexes - 2013-11-19

4 Dell SoftwareUnderstanding Indices

What about Europe?

Page 5: Understanding SQL Server Indexes - 2013-11-19

5 Dell SoftwareUnderstanding Indices

You need the big picture

Page 6: Understanding SQL Server Indexes - 2013-11-19

6 Dell SoftwareUnderstanding Indices

It’s all about DWI knowledge:Indexing strategiesD

W

I

ata

orkload

nternals

Page 7: Understanding SQL Server Indexes - 2013-11-19

7 Dell SoftwareUnderstanding Indices

Your host

• Richard Douglas

• Systems Consultant

• Editor in Chief - ToadWorld.com

• SQL Server MCITPro

• PASS Chapter Leader

• Blog: http://SQL.RichardDouglas.co.uk

• Twitter: @SQLRich

• Email: [email protected]

Page 8: Understanding SQL Server Indexes - 2013-11-19

8 Dell SoftwareUnderstanding Indices

Agenda

• Pre requisites

• Understanding indexes and their architecture

• Utilising SQL Server DMO’s

• Index golden rules

• The importance of maintenance

Page 9: Understanding SQL Server Indexes - 2013-11-19

9 Dell SoftwareUnderstanding Indices

Understanding the SQL Server Data PagePre 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 10: Understanding SQL Server Indexes - 2013-11-19

10 Dell Software Group

Index Architectures

Page 11: Understanding SQL Server Indexes - 2013-11-19

11 Dell SoftwareUnderstanding Indices

Index Architecture

Heaps

Clustered Indexes

Nonclustered Indexes

Nonclustered Columnstore Indexes

Page 12: Understanding SQL Server Indexes - 2013-11-19

12 Dell SoftwareUnderstanding Indices

Heap Structure

Page 13: Understanding SQL Server Indexes - 2013-11-19

13 Dell SoftwareUnderstanding Indices

Clustered Index Structure

Page 14: Understanding SQL Server Indexes - 2013-11-19

14 Dell SoftwareUnderstanding Indices

Nonclustered Index Structure

Page 15: Understanding SQL Server Indexes - 2013-11-19

15 Dell SoftwareUnderstanding Indices

Columnstore pages

Page 16: Understanding SQL Server Indexes - 2013-11-19

16 Dell SoftwareUnderstanding Indices

Index options

Fill factor

Pad Index

Filtered index (WHERE)

Include

Data Compression

Online

Page 17: Understanding SQL Server Indexes - 2013-11-19

17 Dell SoftwareUnderstanding Indices

Hidden dangers

Versioning

Uniqifiers

VARCHAR

NULLS

Page 18: Understanding SQL Server Indexes - 2013-11-19

18 Dell SoftwareUnderstanding Indices

Introducing Index DMO’s

• Admin– Sys.dm_db_index_physical_stats

• Usage– Sys.dm_db_index_usage_stats

– Sys.dm_db_index_operational_stats

• Missing– Sys.dm_db_missing_index_details

– Sys.dm_db_missing_index_columns

– Sys.dm_db_missing_index_groups

– Sys.dm_db_missing_index_group_stats

Page 19: Understanding SQL Server Indexes - 2013-11-19

19 Dell SoftwareUnderstanding 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 20: Understanding SQL Server Indexes - 2013-11-19

20 Dell SoftwareUnderstanding Indices

Bad example; Surname, Firstname, Middle InitialGolden Rules for Clustered Indexes

• Narrow

• Static

• Progressive

• Unique

• Fixed width

• Not Null

Impacts nonclustered indexes

Fragmentation

Space impact

This example will 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 21: Understanding SQL Server Indexes - 2013-11-19

21 Dell SoftwareUnderstanding Indices

Golden Rules for NonClustered Indexes

• Have an optimal clustered key

• Not narrow

• Reduce unnecessary overhead– Fixed Width

– Not null

• Consolidate

• Index foreign keys

Page 22: Understanding SQL Server Indexes - 2013-11-19

22 Dell SoftwareUnderstanding Indices

Page split scenarioThe importance of maintenance

Page header

Data 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 23: Understanding SQL Server Indexes - 2013-11-19

23 Dell SoftwareUnderstanding Indices

Two main typesFragmentation

• Logical / External

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

performance

– Can cause extra IO

• Physical / Internal

“When page density is sub optimal”

– Increased storage space

– More room required in the buffer pool

– More IO’s to read in the data

– More logging, bigger backups

Page 24: Understanding SQL Server Indexes - 2013-11-19

24 Dell SoftwareUnderstanding Indices

Fixing fragmentation

• Rebuild Indexes

• Reorganize Indexes

• Options– Maintenance plan

– Custom/community scripts

– Third party tools

• Guideline (sys.dm_db_Index_Physical_Stats provides these figures)

– 0 – 5% No action

– 5 – 30% Reorganize

– 30% + Rebuild

Page 25: Understanding SQL Server Indexes - 2013-11-19

25 Dell SoftwareUnderstanding Indices

Maintenance best practices

• Auto Close

• Auto Shrink

• Statistics

• Recovery models

Page 26: Understanding SQL Server Indexes - 2013-11-19

26 Dell SoftwareUnderstanding Indices

Summary

Architecture

Options

Querying

Design

Fragmentation

Maintenance

Page 27: Understanding SQL Server Indexes - 2013-11-19

Pg. 27© 2012 Quest Software Inc. All rights reserved.

Solution Area Product DescriptionFast, flexible backup and recovery with industry-leading compression technology

Discover and resolve performance issues in production before they impact end users and service levels

Deepest possible understanding of database performance and norms

Plan and develop applications that deliver both functionality and

optimal performance

Project Lucy

Backup and Recovery

Performance & Operations

Performance Tuning

Development

Comprehensive schema, object, security and change management

Administration

Community crowdsourcing for SQL Server tracing and performance information!

Community, Knowledge, Training

Page 28: Understanding SQL Server Indexes - 2013-11-19

SQLRelay Dates 11/11/13 – Reading

12/11/13 – Southampton

13/11/13 – Cardiff 14/11/13 – Birmingham 15/11/13 – Hemel

Hempstead

25/11/13 – Newcastle 26/11/13 – Manchester 27/11/13 – Norwich 28/11/13 – Bristol 29/11/13 – London

Register at

sqlrelay.co.uk

@SQLRelay2013

Page 29: Understanding SQL Server Indexes - 2013-11-19

29 Dell SoftwareUnderstanding Indices

Any questions?

Page 30: Understanding SQL Server Indexes - 2013-11-19

Thank you

[email protected]@SQLRich