INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM
5 SQL Server Indexing Myths
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM2
Introduction
Jason
Strate
b: www.jasonstrate.com
t: StrateSQL
Resources www.jasonstrate.com/go/indexing
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM3
Unicorns aren’t real!
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM4
I want to
believe!
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM5
But husky
rhinos are!
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM6
Primary Keys Are Always Clustered
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM7
Primary Keys Are Always Clustered
Primary keys are clustered by defaultCommon point of confusion
Often the desired behavior
Is the primary key a good clustered index?Static and stable
Ever increasing
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM
Primary Keys Are Always Clustered
Demo
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM9
Index Column Order Doesn’t Matter
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM10
Index Column Order Doesn’t Matter
Using and seeking an index are not the sameRead execution plans carefully
Validate scans versus seeks
Indexes are sorted left to right
All columns are not required
Scans aren’t necessarily bad
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM11
Index Column Order Doesn’t MatterCol1 Col2
A 1
A 2
A 3
B 1
B 2
B 3
C 1
C 2
C 3
A 1
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM
Index Column Order Doesn’t Matter
Demo
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM13
Clustered Indexes Are Physically Ordered
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM14
Clustered Indexes Are Physically Ordered
Clustered indexes sort data within the indexAccurately find specific values
A before B, B before C
Physical sorted at the tree levelPath data access directly to the required page
Logically sorted at the page levelMinimize page rewrites on DML
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM
Clustered Indexes Are Physically Ordered
Demo
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM16
Fill Factor Affects Insert Operations
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM17
Fill Factor Affects Insert Operations
Fill factor leaves space on data pagesWhy do we do this?
Leave space for DML operations
Mitigate page splits during DML operationsProvides a buffer for data variability
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM18
Fill Factor Affects Insert Operations
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM19
Fill Factor Affects Insert Operations
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM
Fill Factor Affects Insert Operations
Demo
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM21
Every Table Should Have a Heap/Clustered Index
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM22
Every Table Should Have a XXX Index
Blanket statements suck
Doesn’t account for all situationsOr even most situations
Not 50/50
Not 90/10
Is there a safe answer?
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM23
Heap / Clustered Indexes
Heap
• Non-selective access
• Bulk Loaded tables
• Fragmentation prone
with high inserts
• Staging tables
Clustered Index
• Highly modified data
• Singleton access
• Range scans
• Key ordered results
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM
5 SQL Server Indexing Myths
Questions?
ProductsImprove the quality,
productivity, and performance
of your SQL Server and BI
solutions.
ServicesSpeed development through
training and rapid
development services from
Pragmatic Works.
FoundationHelping those who don’t have
the means to get into
information technology and to
achieve their dreams.
Name: Jason Strate
Email: [email protected]
Blog: www.jasonstrate.com
Resource: www.jasonstrate.com/go/indexing
Need Help? jasonstrate.com/go/vmdba