25
INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM 5 SQL Server Indexing Myths

5 SQL Server Indexing Myths

Embed Size (px)

DESCRIPTION

There are many "best practices" around that help people decide how to index databases. Having these practices can help alleviate the time that it takes to design an indexing strategy for a database. These practices can be of great use, except when they are wrong. Join us in this session, as we discuss some common myths associated with indexes and then dive into the myths to demonstrate how they can be debunked. At the end of the session, you'll know a few more things about indexes and leave armed with scripts that can help you debunk these myths on your own. http://pragmaticworks.com/LearningCenter/FreeTrainingWebinars/PastWebinars.aspx?ResourceId=654

Citation preview

Page 1: 5 SQL Server Indexing Myths

INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM

5 SQL Server Indexing Myths

Page 2: 5 SQL Server Indexing Myths

INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM2

Introduction

Jason

Strate

e: [email protected]

e: [email protected]

b: www.jasonstrate.com

t: StrateSQL

Resources www.jasonstrate.com/go/indexing

Page 3: 5 SQL Server Indexing Myths

INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM3

Unicorns aren’t real!

Page 4: 5 SQL Server Indexing Myths

INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM4

I want to

believe!

Page 5: 5 SQL Server Indexing Myths

INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM5

But husky

rhinos are!

Page 6: 5 SQL Server Indexing Myths

INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM6

Primary Keys Are Always Clustered

Page 7: 5 SQL Server Indexing Myths

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

Page 8: 5 SQL Server Indexing Myths

INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM

Primary Keys Are Always Clustered

Demo

Page 9: 5 SQL Server Indexing Myths

INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM9

Index Column Order Doesn’t Matter

Page 10: 5 SQL Server Indexing Myths

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

Page 11: 5 SQL Server Indexing Myths

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

Page 12: 5 SQL Server Indexing Myths

INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM

Index Column Order Doesn’t Matter

Demo

Page 13: 5 SQL Server Indexing Myths

INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM13

Clustered Indexes Are Physically Ordered

Page 14: 5 SQL Server Indexing Myths

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

Page 15: 5 SQL Server Indexing Myths

INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM

Clustered Indexes Are Physically Ordered

Demo

Page 16: 5 SQL Server Indexing Myths

INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM16

Fill Factor Affects Insert Operations

Page 17: 5 SQL Server Indexing Myths

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

Page 18: 5 SQL Server Indexing Myths

INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM18

Fill Factor Affects Insert Operations

Page 19: 5 SQL Server Indexing Myths

INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM19

Fill Factor Affects Insert Operations

Page 20: 5 SQL Server Indexing Myths

INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM

Fill Factor Affects Insert Operations

Demo

Page 21: 5 SQL Server Indexing Myths

INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM21

Every Table Should Have a Heap/Clustered Index

Page 22: 5 SQL Server Indexing Myths

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?

Page 23: 5 SQL Server Indexing Myths

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

Page 24: 5 SQL Server Indexing Myths

INTELLIGENT DATA SOLUTIONS WWW.PRAGMATICWORKS.COM

5 SQL Server Indexing Myths

Questions?

Page 25: 5 SQL Server Indexing Myths

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