introduction to clustered indexes and heaps

50
Introduction to Clustered Indexes and Heaps

Upload: jason-strate

Post on 25-Dec-2014

525 views

Category:

Technology


2 download

DESCRIPTION

It's the age old question, do I put a clustered index on the table or leave it as a heap? While a simple question, this can have serious impacts on the performance of the table in queries. In this session, we'll review the basics of both clustered indexes and heaps; identifying key differences and cases where you might choose one over another.

TRANSCRIPT

Page 1: Introduction to Clustered Indexes and Heaps

Introduction to Clustered Indexes and Heaps

Page 2: Introduction to Clustered Indexes and Heaps

Jason Strate

e: [email protected]

e: [email protected]

b: www.jasonstrate.com

t: StrateSQL

Resources jasonstrate.com/go/indexing

Introduction

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 3: Introduction to Clustered Indexes and Heaps

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

• Industry leaders in Microsoft BI and SQL Server Platform

• SQL Server Professionals - PASS Board of Directors, Speakers, Authors and MVP’s

• National Sales Team Divided by Microsoft Territories

• National System Integrator (NSI)• Gold Certified in Business Intelligence and Data

Platform• Platform Modernization/Safe Passage• Premier Partner for PDW SI Partner Program

MS PDW Partner of Year FY13Frontline Partnership Partner of the Year for Big DataExecutive sponsor - Andy Mouacdie, WW sales director PDW

• Over 7,200 customers worldwide• Over 186,000 people in PW database for demand

generation

About Pragmatic Works

Page 4: Introduction to Clustered Indexes and Heaps

YourData IsTreasure

Page 5: Introduction to Clustered Indexes and Heaps

Store that

treasure

Page 6: Introduction to Clustered Indexes and Heaps

Navigate to your data

Page 7: Introduction to Clustered Indexes and Heaps

Heaps

Clustered

Page 8: Introduction to Clustered Indexes and Heaps

Agenda

Introduction The Heap Clustered Index

Index Patterns Summary

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 9: Introduction to Clustered Indexes and Heaps

Session Goals

• Define differences between heaps and clustered indexes

• Explain design considerations

• Demonstrate differences between key column choices

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 10: Introduction to Clustered Indexes and Heaps

THE HEAP

Introduction The HeapClustered

IndexIndex

PatternsSummary

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 11: Introduction to Clustered Indexes and Heaps

Heap Structure

• Pile of data

– Unordered

– First in, first out

– Scan “happy”

• Table Scan Access

• Forwarded Records

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 12: Introduction to Clustered Indexes and Heaps

Heap

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 13: Introduction to Clustered Indexes and Heaps

Heap

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 14: Introduction to Clustered Indexes and Heaps

THE HEAP

Demo

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 15: Introduction to Clustered Indexes and Heaps

CLUSTERED INDEX

Introduction The HeapClustered

IndexIndex

PatternsSummary

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 16: Introduction to Clustered Indexes and Heaps

Clustered Index

• Mechanism for storing data

– Logically ordered

– Structured

– Accessible

– Direct path

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 17: Introduction to Clustered Indexes and Heaps

Clustered Index

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 18: Introduction to Clustered Indexes and Heaps

Clustered Index

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 19: Introduction to Clustered Indexes and Heaps

Clustered Index

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 20: Introduction to Clustered Indexes and Heaps

Clustered Index Qualities

Static Narrow

UniqueEver-

increasing

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 21: Introduction to Clustered Indexes and Heaps

Clustered Index Qualities

Static Narrow

UniqueEver-

increasing

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 22: Introduction to Clustered Indexes and Heaps

Static

Non-static key columns can reshuffle the rows in the clustered index.

Page 23: Introduction to Clustered Indexes and Heaps

Clustered Index Qualities

Static Narrow

UniqueEver-

increasing

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 24: Introduction to Clustered Indexes and Heaps

Narrow

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Key columns from clustered indexes are included in all non-clustered indexes.

Size

Rows 4 8 16 32 64

10,000 0.04 0.08 0.15 0.31 0.61

100,000 0.38 0.76 1.53 3.05 6.10

1,000,000 3.81 7.63 15.26 30.52 61.04

10,000,000 38.15 76.29 152.59 305.18 610.35

100,000,000 381.47 762.94 1,525.88 3,051.76 6,103.52

1,000,000,000 3,814.70 7,629.39 15,258.79 30,517.58 61,035.16

Page 25: Introduction to Clustered Indexes and Heaps

Clustered Index Qualities

Static Narrow

UniqueEver-

increasing

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 26: Introduction to Clustered Indexes and Heaps

Unique

Uniqueifier: Adds a 4-byte integer to rows non-unique clustered indexes to create unique rows.

Size

Rows 4 8 16 32 64

10,000 0.08 0.11 0.19 0.34 0.65

100,000 0.76 1.14 1.91 3.43 6.48

1,000,000 7.63 11.44 19.07 34.33 64.85

10,000,000 76.29 114.44 190.73 343.32 648.50

100,000,000 762.94 1,144.41 1,907.35 3,433.23 6,484.99

1,000,000,000 7,629.39 11,444.09 19,073.49 34,332.28 64,849.85

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 27: Introduction to Clustered Indexes and Heaps

Clustered Index Qualities

Static Narrow

UniqueEver-

increasing

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 28: Introduction to Clustered Indexes and Heaps

Ever-increasing

Ever-increasing values allows new values to be added to the end of the index structure without reorganizing rows.

100

200

300

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 29: Introduction to Clustered Indexes and Heaps

100

200

300

600

Ever-increasing

Ever-increasing values allows new values to be added to the end of the index structure without reorganizing rows.

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 30: Introduction to Clustered Indexes and Heaps

100

200

300

400

600

Ever-increasing

Ever-increasing values allows new values to be added to the end of the index structure without reorganizing rows.

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 31: Introduction to Clustered Indexes and Heaps

100

200

300

400

500

600

Ever-increasing

Ever-increasing values allows new values to be added to the end of the index structure without reorganizing rows.

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 32: Introduction to Clustered Indexes and Heaps

CLUSTERED INDEX

Demo

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 33: Introduction to Clustered Indexes and Heaps

Pro Tip: Default to Clustered Indexes, unless heaps are proven to improve performance

Page 34: Introduction to Clustered Indexes and Heaps

INDEX PATTERNS

Introduction The HeapClustered

IndexIndex

PatternsSummary

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 35: Introduction to Clustered Indexes and Heaps

Heap Patterns

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

• Staging tables

• Low use data

• Temporary tables

• Heavy scan data

Page 36: Introduction to Clustered Indexes and Heaps

Clustered Index Patterns

Identity Column

Surrogate Key

Foreign Key

Multi-Column

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 37: Introduction to Clustered Indexes and Heaps

Identity Column

• Non-related value that defines each row uniquely

• Assigned when data is inserted

• Typically uses:

– Int or bigint

– IDENTITY or SEQUENCE

– Uniqueidentifier

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 38: Introduction to Clustered Indexes and Heaps

Identity Column

Static

Narrow

Unique

Ever-increasing

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 39: Introduction to Clustered Indexes and Heaps

Surrogate Key

• Unique value is a part of the row

• Assigned in the source data

• Data type dependent on the data

• Value should be unchanging

– Employee ID

– Social Security Number

– Birthdate

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 40: Introduction to Clustered Indexes and Heaps

Surrogate Key

Static

Narrow

Unique

Ever-increasing

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 41: Introduction to Clustered Indexes and Heaps

Foreign Key

• One-to-many relationships

• Access path in child via parent

• Piggy-backs identity pattern

– Parent left of child identity value

• Common in many applications

– Order header to details

– Hotels to rooms

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 42: Introduction to Clustered Indexes and Heaps

Foreign Key

Static

Narrow

Unique

Ever-increasing

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 43: Introduction to Clustered Indexes and Heaps

Multi-Column

• Similar to foreign key pattern

• Many-to-many relationships

• Highly dependent on selectivity

• NOT a collection of dimension keys

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 44: Introduction to Clustered Indexes and Heaps

Multi-Column

Static

Narrow

Unique

Ever-increasing

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 45: Introduction to Clustered Indexes and Heaps

INDEX PATTERNS

Demo

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 46: Introduction to Clustered Indexes and Heaps

SUMMARY

Introduction The HeapClustered

IndexIndex

PatternsSummary

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 47: Introduction to Clustered Indexes and Heaps

These are not unbreakable rules!

Page 48: Introduction to Clustered Indexes and Heaps

Summary

• Clustered indexes are important

• Select clustering keys based on need

• Validate clustered indexes over time

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 49: Introduction to Clustered Indexes and Heaps

More Information

Expert Performance IndexingFor SQL Server 2012

Jason StrateTed Krueger

OverviewStatistics

MaintenanceTools

Analysis

http://amzn.com/1430237414

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 50: Introduction to Clustered Indexes and Heaps

ServicesSpeed development through training, and rapid development services from Pragmatic Works.

ProductsBI products to covert to a Microsoft BI platform and simplify development onthe platform.

FoundationHelping those who do not have themeans to get into information technologyachieve their dreams.

For more information…Name: Jason Strate

Email: [email protected]

Blog: www.jasonstrate.com

Resource: jasonstrate.com/go/indexing

Need Help? jasonstrate.com/go/vmdba