we don't need roads: a developers look into sql server indexes
TRANSCRIPT
WE DON'T NEED ROADS: A
DEVELOPER'S LOOK INTO SQL
SERVER INDEXES
RICHIE RUMP
JORRISS LLC
@JORRISS
HTTP://JORRISS.NET
WHO IS THIS GUY?
WHAT WE’RE GOING
TO TALK ABOUT
• Why indexes?
• How indexes are used
• DB Storage Basics
• Index Structure
• Clustered indexes vs non-clustered
• Includes, Fill factor and covering
A TRAGEDY
Great Scott!
WHY ARE INDEXES
IMPORTANT?
“An index makes the
query fast.”
Reference: http://use-the-index-luke.com/sql/anatomy
WHY ARE INDEXES
IMPORTANT?
“An index makes your
query fast.”
Reference: http://use-the-index-luke.com/sql/anatomy
HOW ARE INDEXES USED
BY SQL SERVER?
SQL Server creates a plan to
determine what’s the best way to
execute the query.
(Unlike some developers)
EXECUTION PLANS
Demo
TYPES OF INDEXES
• Clustered
• Nonclustered
• Heaps
• Column Store
• XML
• Spatial
• Full-Text
• Clustered
• Nonclustered
• Heaps
• Column Store
• XML
• Spatial
• Full-Text
HOW DO INDEXES
WORK?
In order to understand the magic of
indexes we need to understand how
SQL Server stores data.
HOW SQL SERVER
STORES INFORMATION
Pages
• Page Size: 8K
• Used to store just about
everything.
• Page Header: Metadata
about the page. (Page
Number, Type, etc.)
• Offset Array: Pointers to
where each row begins.
Page Header
Records
Offset Array
HOW SQL SERVER
STORES INFORMATION
Page Header
Row 1
1
Page Header
Row 1
1
Row 2
2
Page Header
Row 1
13
Row 2
Row 3
2
HOW SQL SERVER
STORES INFORMATION
Page Header
Row 1
1
Row 2
Row 3
Row 4
Row 5
2345
Page Header
Row 6
6
Row 7
Row 8
Row 9
Row 10
78910
Page Header
Row 11
11
Row 12
Row 13
Row 14
Row 15
12131415
HOW INDEXES ARE
STORED
• B-Tree or Balanced Tree
• The tree is made up of Pages
Leaf Level
Intermediate Level
Root Level
HOW ARE INDEXES
STORED
1 -
400
201 -
400
1 -
200
151 -
200
101 -
150
251 -
300
201-
250
101 -
200
201 -
300
301 -
400
1 -
100
301 -
350
51 -
100
1 -
50
351 -
400
HOW ARE INDEXES
STORED
1 -
400
201 -
400
1 -
200
151 -
200
101 -
150
251 -
300
201-
250
101 -
200
201 -
300
301 -
400
1 -
100
301 -
350
51 -
100
1 -
50
351 -
400
SELECT UserId, TitleFROM PostsWHERE UserId = 160
CREATE NONCLUSTERED INDEX IX_Posts_OwnerUserIdON Posts (OwnerUserId)
CLUSTERED INDEXES
• What is a table, Alex?
• Only one per table!
• Can the key be more than one column.
• Must be unique (SQL Server will help if it isn’t.
• Can’t be more than 900 bytes combined
Leaf Level – THIS IS SPARTA THE DATA!
Intermediate Level
Root Level
CLUSTERED INDEXES
1 -
400
201 -
400
1 -
200
151 -
200
101 -
150
251 -
300
201-
250
101 -
200
201 -
300
301 -
400
1 -
100
301 -
350
51 -
100
1 -
50
351 -
400
NONCLUSTERED
INDEXES
• Subset of data optimized for
searching.
• Key can be more than one column
• Can’t be more than 900 bytes
combined
• Doesn’t have to be unique.
• Max of 999 per table.
(If I find 999 indexes on your table I will find you…)
Leaf Level – Key Data and Included columns.
Intermediate Level
Root Level
NONCLUSTERED
INDEXES
1 -
400
201 -
400
1 -
200
151 -
200
101 -
150
251 -
300
201-
250
101 -
200
201 -
300
301 -
400
1 -
100
301 -
350
51 -
100
1 -
50
351 -
400
CLUSTERED AND
NONCLUSTERED INDEXES
Demo
DEMO REVIEW
• Key lookups are slow. Use Includes
to combat.
• Use columns in the index key if you
intend on searching them.
• Index order matters!
• Suggested indexes may not be the
best way to go.
SORRY, YOUR PAGE IS
IN ANOTHER CASTLE
Page HeaderPage Header
Id: 2
1
Id: 3
Id: 7
Id: 12
Id: 14
2345
Id: 6
123
FILL FACTOR
• Minimizes page splits by leaving empty
space in the page.
• Is a number 0 to 100.
• A value of 80 means that 80% of the
page will be filled with data.
• 0 and 100 both mean that 100% of the
page will be filled with data.
• Only applies to leaf pages
DISADVANTAGES OF
INDEXES
• Increases storage.
• Creating duplicate copies of data.
• Insert, Updates and Deletes are slower.
• The DB has to delete data from the table
and all of the related indexes.
• Additional maintenance
BONUS! ENTITY
FRAMEWORK!
Demo
REFERENCES
THANK YOU
Richie Rump
@Jorriss
http://jorriss.net
http://dotnetmiami.com
http://statisticsioparser.com