INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM
5 SQL Server
Indexing Myths
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM2
Introduction
Jason
Strate
e: jstrate@pragmaticworks.com
e: jasonstrate@gmail.com
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 default
Common 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 same
Read 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 Matter
Col1 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 index
Accurately find specific values
A before B, B before C
Physical sorted at the tree level
Path data access directly to the required page
Logically sorted at the page level
Minimize 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 pages
Why do we do this?
Leave space for DML operations
Mitigate page splits during DML operations
Provides 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 situations
Or 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?
Products
Improve the quality,
productivity, and performance
of your SQL Server and BI
solutions.
Services
Speed development through
training and rapid
development services from
Pragmatic Works.
Foundation
Helping those who don’t have
the means to get into
information technology and to
achieve their dreams.
Name: Jason Strate
Email: jstrate@pragmaticworks.com
Blog: www.jasonstrate.com
Resource: www.jasonstrate.com/go/indexing
Need Help? jasonstrate.com/go/vmdba

5 SQL Server Indexing Myths

  • 1.
    INTELLIGENT DATA SOLUTIONSWWW.PRAGMATICWORKS.COM 5 SQL Server Indexing Myths
  • 2.
    INTELLIGENT DATA SOLUTIONSWWW.PRAGMATICWORKS.COM2 Introduction Jason Strate e: jstrate@pragmaticworks.com e: jasonstrate@gmail.com b: www.jasonstrate.com t: StrateSQL Resources www.jasonstrate.com/go/indexing
  • 3.
    INTELLIGENT DATA SOLUTIONSWWW.PRAGMATICWORKS.COM3 Unicorns aren’t real!
  • 4.
    INTELLIGENT DATA SOLUTIONSWWW.PRAGMATICWORKS.COM4 I want to believe!
  • 5.
    INTELLIGENT DATA SOLUTIONSWWW.PRAGMATICWORKS.COM5 But husky rhinos are!
  • 6.
    INTELLIGENT DATA SOLUTIONSWWW.PRAGMATICWORKS.COM6 Primary Keys Are Always Clustered
  • 7.
    INTELLIGENT DATA SOLUTIONSWWW.PRAGMATICWORKS.COM7 Primary Keys Are Always Clustered Primary keys are clustered by default Common point of confusion Often the desired behavior Is the primary key a good clustered index? Static and stable Ever increasing
  • 8.
    INTELLIGENT DATA SOLUTIONSWWW.PRAGMATICWORKS.COM Primary Keys Are Always Clustered Demo
  • 9.
    INTELLIGENT DATA SOLUTIONSWWW.PRAGMATICWORKS.COM9 Index Column Order Doesn’t Matter
  • 10.
    INTELLIGENT DATA SOLUTIONSWWW.PRAGMATICWORKS.COM10 Index Column Order Doesn’t Matter Using and seeking an index are not the same Read execution plans carefully Validate scans versus seeks Indexes are sorted left to right All columns are not required Scans aren’t necessarily bad
  • 11.
    INTELLIGENT DATA SOLUTIONSWWW.PRAGMATICWORKS.COM11 Index Column Order Doesn’t Matter Col1 Col2 A 1 A 2 A 3 B 1 B 2 B 3 C 1 C 2 C 3 A 1
  • 12.
    INTELLIGENT DATA SOLUTIONSWWW.PRAGMATICWORKS.COM Index Column Order Doesn’t Matter Demo
  • 13.
    INTELLIGENT DATA SOLUTIONSWWW.PRAGMATICWORKS.COM13 Clustered Indexes Are Physically Ordered
  • 14.
    INTELLIGENT DATA SOLUTIONSWWW.PRAGMATICWORKS.COM14 Clustered Indexes Are Physically Ordered Clustered indexes sort data within the index Accurately find specific values A before B, B before C Physical sorted at the tree level Path data access directly to the required page Logically sorted at the page level Minimize page rewrites on DML
  • 15.
    INTELLIGENT DATA SOLUTIONSWWW.PRAGMATICWORKS.COM Clustered Indexes Are Physically Ordered Demo
  • 16.
    INTELLIGENT DATA SOLUTIONSWWW.PRAGMATICWORKS.COM16 Fill Factor Affects Insert Operations
  • 17.
    INTELLIGENT DATA SOLUTIONSWWW.PRAGMATICWORKS.COM17 Fill Factor Affects Insert Operations Fill factor leaves space on data pages Why do we do this? Leave space for DML operations Mitigate page splits during DML operations Provides a buffer for data variability
  • 18.
    INTELLIGENT DATA SOLUTIONSWWW.PRAGMATICWORKS.COM18 Fill Factor Affects Insert Operations
  • 19.
    INTELLIGENT DATA SOLUTIONSWWW.PRAGMATICWORKS.COM19 Fill Factor Affects Insert Operations
  • 20.
    INTELLIGENT DATA SOLUTIONSWWW.PRAGMATICWORKS.COM Fill Factor Affects Insert Operations Demo
  • 21.
    INTELLIGENT DATA SOLUTIONSWWW.PRAGMATICWORKS.COM21 Every Table Should Have a Heap/Clustered Index
  • 22.
    INTELLIGENT DATA SOLUTIONSWWW.PRAGMATICWORKS.COM22 Every Table Should Have a XXX Index Blanket statements suck Doesn’t account for all situations Or even most situations Not 50/50 Not 90/10 Is there a safe answer?
  • 23.
    INTELLIGENT DATA SOLUTIONSWWW.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
  • 24.
    INTELLIGENT DATA SOLUTIONSWWW.PRAGMATICWORKS.COM 5 SQL Server Indexing Myths Questions?
  • 25.
    Products Improve the quality, productivity,and performance of your SQL Server and BI solutions. Services Speed development through training and rapid development services from Pragmatic Works. Foundation Helping those who don’t have the means to get into information technology and to achieve their dreams. Name: Jason Strate Email: jstrate@pragmaticworks.com Blog: www.jasonstrate.com Resource: www.jasonstrate.com/go/indexing Need Help? jasonstrate.com/go/vmdba