Understanding IndicesRichard Douglas
2 Dell SoftwareUnderstanding Indices
“Can’t you just put an index on it?”
(Name withheld to protect the guilty)
3 Dell SoftwareUnderstanding Indices
Fixing the problem
4 Dell SoftwareUnderstanding Indices
What about Europe?
5 Dell SoftwareUnderstanding Indices
You need the big picture
6 Dell SoftwareUnderstanding Indices
It’s all about DWI knowledge:Indexing strategiesD
W
I
ata
orkload
nternals
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]
8 Dell SoftwareUnderstanding Indices
Agenda
• Pre requisites
• Understanding indexes and their architecture
• Utilising SQL Server DMO’s
• Index golden rules
• The importance of maintenance
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
10 Dell Software Group
Index Architectures
11 Dell SoftwareUnderstanding Indices
Index Architecture
Heaps
Clustered Indexes
Nonclustered Indexes
Nonclustered Columnstore Indexes
12 Dell SoftwareUnderstanding Indices
Heap Structure
13 Dell SoftwareUnderstanding Indices
Clustered Index Structure
14 Dell SoftwareUnderstanding Indices
Nonclustered Index Structure
15 Dell SoftwareUnderstanding Indices
Columnstore pages
16 Dell SoftwareUnderstanding Indices
Index options
Fill factor
Pad Index
Filtered index (WHERE)
Include
Data Compression
Online
17 Dell SoftwareUnderstanding Indices
Hidden dangers
Versioning
Uniqifiers
VARCHAR
NULLS
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
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.
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
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
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
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
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
25 Dell SoftwareUnderstanding Indices
Maintenance best practices
• Auto Close
• Auto Shrink
• Statistics
• Recovery models
26 Dell SoftwareUnderstanding Indices
Summary
Architecture
Options
Querying
Design
Fragmentation
Maintenance
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
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
29 Dell SoftwareUnderstanding Indices
Any questions?
Thank you
[email protected]@SQLRich