the ultimate session on indexing - victor isakov | sql ranger · 2011. 10. 14. · such as...

92
October 11-14, Seattle, WA The Ultimate Session on Indexing AD-302-HD Victor Isakov Database Architect / Trainer SQL Server Solutions www.sqlserversolutions.com.au www.victorisakov.com

Upload: others

Post on 14-Oct-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

October 11-14, Seattle, WA

The Ultimate Session on Indexing AD-302-HD

Victor Isakov Database Architect / Trainer SQL Server Solutions www.sqlserversolutions.com.au www.victorisakov.com

Page 2: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Abstract

You can have the best hardware possible: SSDs, TBs of RAM and 100s of processors. (Don't we all wish we had such budgets.) But if you don't have the correct indexing strategy in place your database can still perform poorly. With SQL Server 11 there will be even more options available. Confused about what indexing strategy to use? Then this is the session for you! In this session Victor Isakov (MCA, MCM, MCT, MVP) will cover all of the indexing options that are available in SQL Server and when best to use them. Victor will cover "practical" internals that will help you understand how indexes work and why you need to maintain them. Finally he will cover what indexing strategies to use given the most important considerations: your users, their query patterns and your data. This should prove to be a most informative and practical session that will enable you to optimise your database performance.

2 AD-302-HD | The Ultimate Session on Indexing

Page 3: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Speaker

3 AD-302-HD | The Ultimate Session on Indexing

Speaker

Victor Isakov is a Database Architect and trainer who provides consulting and training services to various organizations in the public, private, and NGO sectors globally.

He regularly speaks at international conferences such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp.

He has authored a number of books on SQL Server and worked with Microsoft to develop the SQL Server exams and certification.

In 2007, Victor was invited by Microsoft to attend the “SQL Ranger” program in Redmond, WA.

Consequently, he was one of the first IT professionals to achieve both the Microsoft Certified Master: SQL Server and Microsoft Certified Architect: SQL Server certifications globally.

Page 4: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Microsoft Certified Master

Page 5: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Caveats

• There is always an exception

• “Things always work on the whiteboard”

• Victor Isakov quoting himself

• Test in a DEV / UAT environment before

implementing in PROD

• Ensure your indexes are being used as intended

• The answer to every question will be “It

depends”

• But feel free to ask questions anyway! ☺

AD-302-HD | The Ultimate Session on Indexing 5

Page 6: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Agenda

• Heaps

• Clustered

• Nonclustered Indexes

• Composite/Compound Indexes

• Covering Indexes

• Included Columns

• Initial Guidelines

• Indexed Views

AD-302-HD | The Ultimate Session on Indexing 6

Page 7: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Agenda

• Partitioned Indexes

• Filtered Indexes

• Filtered Statistics

• Basic Indexing Strategies

• Fragmentation

• Maintaining Indexes

• Advanced Indexing Strategies

AD-302-HD | The Ultimate Session on Indexing 7

Page 8: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Why Are We Here?

• Primary concern(s)

• Improve performance

• Enforce primary keys (entity integrity)

• Secondary concern(s)

• Enforce uniqueness

• Indexes

• Unique constraints

AD-302-HD | The Ultimate Session on Indexing 8

Page 9: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Another question:

• What is the most important consideration

when designing your indexing strategy?

• Queries users are running

• Frequency of those queries

• Batch operations

• Frequency of those batch operations

• Data

• Don’t forget that you can disable indexes

are rebuild them as required!

AD-302-HD | The Ultimate Session on Indexing 9

Page 10: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Heaps

• Heaps

• Heap Structure

• Navigating a Heap

• Heap Performance

AD-302-HD | The Ultimate Session on Indexing 10

Page 11: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Heap

• A heap is a table without a clustered index

• Represents a “random collection of pages

that make up a table”

• Free space is reused

• Index Allocation Map (IAM) page used to

track allocation of pages / extents

AD-302-HD | The Ultimate Session on Indexing 11

Page 12: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Heap

• Heaps / indexes have an entry in sys.indexs

• An “index” can be partitioned

• Each partition can store 3 different “types

of pages”

• IN_ROW_DATA

• ROW_OVERFLOW_DATA

• LOB_DATA

AD-302-HD | The Ultimate Session on Indexing 12

SYS.INDEXES SYS.PARTITIONS SYS.ALLOCATION_UNITS

Page 13: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Heap Structure

• IAM Page

• Forwarding pointers

AD-302-HD | The Ultimate Session on Indexing 13

prev next

ID FName LName Other

1 Jan Jansen …

7 Wilma Boer, de …

xxxxx

16 Els Rekels …

Data page

000000100010000

000000000000000

000000000000000

000000000100000

000000000000000

000000000000000

000000000000000

IAM Page

prev next

ID FName LName Other

16 Els Rekels …

9 Klaas Jansen …

81 Annet Visser …

Data page

ID FName LName Other

3 Klaas Visser …

4 Wim Blok …

Data page

forwarding pointer

001000000000000

000010100100000

000000000000000

000100001100000

000000000000000

000000011100000

000000000000000

IAM Page

prev next

prev next prev next prev next

100000110000000

001010010000010

000000000010000

000000000000000

001100000000100

000000100100000

000000000000001

IAM Page

(If size of data file exceeds 4GB)

Page 14: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Navigating a Heap

• Table scan

AD-302-HD | The Ultimate Session on Indexing 14

ID FName LName Other

1 Jan Jansen …

7 Wilma Boer, de …

xxxxx

16 Els Rekels …

Data page

000000100010000

000000000000000

000000000000000

000000000100000

000000000000000

000000000000000

000000000000000

IAM Page

prev next

ID FName LName Other

16 Els Rekels …

9 Klaas Jansen …

81 Annet Visser …

Data page

ID FName LName Other

3 Klaas Visser …

4 Wim Blok …

Data page

forwarding pointer

prev next prev next prev next

Page 15: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Heap Performance

• Only way that SQL Server can work with a

heap is to perform a table scan

• Clustered indexed bring “order” to the

table

• Give SQL Server the option of seeking the B-Tree

to get the data

• Allow SQL Server to stop when it knows that no

more records can satisfy the query

AD-302-HD | The Ultimate Session on Indexing 15

Page 16: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Clustered Indexes

• Indexes

• SQL Server Limits

• Clustered Index

• Navigating a Clustered Index

AD-302-HD | The Ultimate Session on Indexing 16

Page 17: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Indexes

• Improve performance by optimizing data

access path

• Index seeks

• Allow SQL Server to bail out earlier

• Implemented as B-Trees in SQL Server

• Two types

• Clustered

• Nonclustered

• Can be unique

AD-302-HD | The Ultimate Session on Indexing 17

Page 18: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

SQL Server Limits (2008R2 BOL)

• 999 non clustered indexes per table

• 16 columns per index key

• 900 bytes per index key

• 30,000 statistics per non-index columns

• 1,024 columns per non-wide table

• 30,000 columns per wide table

• 1,000 partitions per partitioned table / index

• 4,096 columns per SELECT statement

AD-302-HD | The Ultimate Session on Indexing 18

Page 19: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Clustered Index

• Dictionary analogy

• Physical order == sorted order

• 1 per table

• If not created as unique, SQL Server adds 4

byte INTEGER value as required to make

non-unique key values unique

• Uniquifier

AD-302-HD | The Ultimate Session on Indexing 19

Page 20: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Clustered Index Structure

Root

Intermediate

Leaf

AD-302-HD | The Ultimate Session on Indexing 20

ID FName LName Other

1 Jan Jansen …

3 Klaas Visser …

4 Wim Blok …

7 Wilma Boer, de …

Data page

Index Page

ID FName LName Other

9 Klaas Jansen …

16 Els Rekels …

Data page

Index Page

prev next

prev next

prev next

prev next

ID FName LName Other

25 Els Rekels …

81 Annet Visser …

Data page

prev next

Index Page

prev next

ID page

NULL xxxx

17 xxxx

ID page

NULL xxxx

9 xxxx

ID page

17 xxxx

000000100010000

000000000000000

000000000000000

000000000100000

000100000000000

000000000001000

000000100000000

IAM Page

prev next

Page 21: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Index Seek

AD-302-HD | The Ultimate Session on Indexing 21

ID Fname LName Other

1 Jan Jansen …

3 Klaas Visser …

4 Wim Blok …

7 Wilma Boer, de …

Leaf page

Root level

ID FName LName Other

9 Klaas Jansen …

16 Els Rekels …

Leaf page

Intermediate

prev next

prev next

prev next

prev next

ID FName LName Other

25 Els Rekels …

81 Annet Visser …

Leaf page

prev next

Intermediate

prev next

ID page

NULL xxxx

17 xxxx

ID page

NULL xxxx

9 xxxx

ID page

17 xxxx

000000100010000

000000000000000

000000000000000

000000000100000

000100000000000

000000000001000

000000100000000

IAM Page

prev next

Page 22: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Index Scan (Unordered)

AD-302-HD | The Ultimate Session on Indexing 22

LName Fname ID

Blok Wim 4

Boer, de Wilma 7

Jansen Klaas 9

Leaf page

Root level

LName FName ID

Jansen Jan 1

Rekels Els 16

Rekels Els 25

Visser Annet 81

Leaf page

Intermediate

prev next

prev next

prev next

prev next

LName FName ID

Visser Klaas 3

Leaf page

prev next

Intermediate

prev next

LName FName page

NULL NULL xxxx

Visser Klaas xxxx

LName FName page

NULL NULL xxxx

Jansen Jan xxxx

LName Fname page

Visser Klaas xxxx

000000100010000

000000000000000

000000000000000

000000000100000

000100000000000

000000000001000

000000100000000

IAM Page

prev next

Page 23: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Index Scan (Ordered)

AD-302-HD | The Ultimate Session on Indexing 23

ID FName LName Other

1 Jan Jansen …

3 Klaas Visser …

4 Wim Blok …

7 Wilma Boer, de …

Leaf page

Root level

ID FName LName Other

9 Klaas Jansen …

16 Els Rekels …

Leaf page

Intermediate

prev next

prev next

prev next

prev next

ID FName LName Other

25 Els Rekels …

81 Annet Visser …

Leaf page

prev next

Intermediate

prev next

ID page

NULL xxxx

17 xxxx

ID page

NULL xxxx

9 xxxx

ID page

17 xxxx

000000100010000

000000000000000

000000000000000

000000000100000

000100000000000

000000000001000

000000100000000

IAM Page

prev next

Page 24: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Nonclustered Indexes

• Nonclustered Index

• Nonclustered Index Structure

• Navigating a Nonclustered Index

• Composite / Compound Indexes

• Included columns

• Covering Indexs

AD-302-HD | The Ultimate Session on Indexing 24

Page 25: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Nonclustered Index

• Index analogy

• 999 per table

• Separate B-tree structure

• On heap (physical key)

• RID (File ID, Page ID, Slot #)

• On clustered index (logical key)

AD-302-HD | The Ultimate Session on Indexing 25

Page 26: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Nonclustered Index Structure

AD-302-HD | The Ultimate Session on Indexing 26

FName LName ID

Blok Wim 4

Boer, de Wilma 7

Jansen Klaas 9

Index page

Index page

FName LName ID

Jansen Jan 1

Rekels Els 16

Rekels Els 25

Visser Annet 81

Index page

Index page

prev next

prev next

prev next

prev next

FName LName ID

Visser Klaas 3

Index page

prev next

Index page

prev next

FName LName page

NULL NULL xxxx

Visser Klaas xxxx

FName LName page

NULL NULL xxxx

Jansen Jan xxxx

FName LName page

Visser Klaas xxxx

000000100010000

000000000000000

000000000000000

000000000100000

000100000000000

000000000001000

000000100000000

IAM Page

prev next

Page 27: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Nonclustered Index Seek (Key)

AD-302-HD | The Ultimate Session on Indexing 27

ID FName LName Other

1 Jan Jansen …

3 Klaas Visser …

4 Wim Blok …

7 Wilma Boer, de …

Leaf page

Root level

ID FName LName Other

9 Klaas Jansen …

16 Els Rekels …

Leaf page

Intermediate

prev next

prev next

prev next

prev next

ID FName LName Other

25 Els Rekels …

81 Annet Visser …

Leaf page

prev next

Intermediate

prev next

ID page

NULL xxxx

17 xxxx

ID page

NULL xxxx

9 xxxx

ID page

17 xxxx

LName FName ID

Blok Wim 4

Boer, de Wilma 7

Jansen Klaas 9

Root level

LName FName ID

Jansen Jan 1

Rekels Els 16

Rekels Els 25

Visser Annet 81

Leaf page

Intermediate

prev next

prev next

prev next

prev next

LName FName ID

Visser Klaas 3

Leaf page

prev next

Intermediate

prev next

LName FName page

NULL NULL xxxx

Visser Klaas xxxx

LName FName page

NULL NULL xxxx

Jansen Jan xxxx

LName FName page

Visser Klaas xxxx

Leaf page

Page 28: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Nonclustered Index Seek (RID)

AD-302-HD | The Ultimate Session on Indexing 28

LName FName RID

Blok Wim 95,3

Boer, de Wilma 18,2

Jansen Klaas 31,3

Leaf page

Root level

LName FName RID

Jansen Jan 18,1

Rekels Els 31,2

Rekels Els 18,4

Visser Annet 31,4

Leaf page

Intermediate

prev next

prev next

prev next

prev next

LName FName RID

Visser Klaas 18,3

Leaf page

prev next

Intermediate

prev next

LName FName page

NULL NULL xxxx

Visser Klaas xxxx

LName FName page

NULL NULL xxxx

Jansen Jan xxxx

LName FName page

Visser Klaas xxxx

ID FName LName Other

1 Jan Jansen …

7 Wilma Boer, de …

xxxxx (95,2)

16 Els Rekels …

Data page 18

ID FName LName Other

16 Els Rekels …

9 Klaas Jansen …

81 Annet Visser …

Data page 31

ID FName Lname Other

3 Klaas Visser …

4 Wim Blok …

Data page 95

prev next prev next prev next

Page 29: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Initial Guidelines

• Have a good argument not to implement a clustered index

• Ideal clustered index key • Narrow

• Unique

• Static

• If it’s unique – make it unique

• Foreign key constraints do not create underlying indexes

• Try not to create multiple indexes on the same column(s) ☺

AD-302-HD | The Ultimate Session on Indexing 29

Page 30: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Clustered Index Considerations

• Where do clustered indexes work well?

• Point queries

• Range queries

• Partial scans

• Large number of duplicates in result set

• Where the data is clustered together!

• Hmm… Like the foreign key

AD-302-HD | The Ultimate Session on Indexing 30

Page 31: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Nonclustered Index Considerations

• Where do nonclustered indexes work well?

• Point queries

• Have limited value where a large number

of duplicate records / records are returned

AD-302-HD | The Ultimate Session on Indexing 31

Page 32: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Composite / Compound Indexes

• Multiple columns in the index’s definition

• Remember limits

• 16 columns

• 900 bytes

• Column order matters

• Ideally the most selective column is first

• But it really depends on the queries

• SQL Server only creates statistics on the

high-order column

AD-302-HD | The Ultimate Session on Indexing 32

Page 33: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Included Columns

• Introduced in SQL Server 2005

• Columns added to leaf level only

• All data types allowed except TEXT, NTEXT

and IMAGE

• Overcome 900 byte / 16 column limit…

AD-302-HD | The Ultimate Session on Indexing 33

Page 34: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Included Columns Impact

AD-302-HD | The Ultimate Session on Indexing 34

FName LName ID

Blok Wim 4

Boer, de Wilma 7

Jansen Klaas 9

Leaf page

Root level

FName LName ID

Jansen Jan 1

Rekels Els 16

Rekels Els 25

Visser Annet 81

Leaf page

Intermediate

prev next

prev next

prev next

prev next

FName LName ID

Visser Klaas 3

Leaf page

prev next

Intermediate

prev next

FName LName page

NULL NULL xxxx

Visser Klaas xxxx

FName LName page

NULL NULL xxxx

Jansen Jan xxxx

FName LName page

Visser Klaas xxxx

FName LName ID

Blok Wim 4

Boer, de Wilma 7

Jansen Klaas 9

FName LName ID

Jansen Jan 1

Rekels Els 16

Rekels Els 25

Visser Annet 81

FName LName ID

Visser Klaas 3

Page 35: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Included Index Impact

AD-302-HD | The Ultimate Session on Indexing 35 35

Leaf page

Root level

Leaf page

Intermediate

prev next

prev next

prev next

prev next

Leaf page

prev next

Intermediate

prev next

FName LName page

NULL NULL xxxx

Visser Klaas xxxx

FName LName page

NULL NULL xxxx

Jansen Klaas xxxx

Rekels Els xxxx

FName LName page

Visser Annet xxxx

FName LName ID Mail

Blok Wim 4 x@y

Boer, de Wilma 7 x@y

FName LName ID Mail

Jansen Klaas 9 x@y

Jansen Jan 1 x@y

FName LName ID Mail

Rekels Els 16 x@y

Rekels Els 25 x@y

Leaf page

prev next

FName LName ID Mail

Visser Annet 81 x@y

Visser Klaas 3 x@y

Page 36: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Included Columns Considerations

• Don’t make the nonclustered index too

wide

• SQL Server might not use it

• Exception

• Covering index

• Scanning the leaf level of the nonclustered index

< scanning the table

AD-302-HD | The Ultimate Session on Indexing 36

Page 37: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Covering Indexes

• The nonclustered index covers the data being requested by the query

• No lookup required • No need to read the data pages

• No locking on the data pages

• Don’t forget that SQL Server can either: • Seek through the root page

• Scan through the first page

• The SARG does not have to query the highest order column

AD-302-HD | The Ultimate Session on Indexing 37

Page 38: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Indexed Views

• What is a View?

• Indexed Views

• Indexed View Requirements

• NOEXPAND Hint

AD-302-HD | The Ultimate Session on Indexing 38

Page 39: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

What is a View?

AD-302-HD | The Ultimate Session on Indexing 39

CREATE VIEW dbo.MyView

AS SELECT FirstName, LastName

FROM Person.Person

WHERE LastName LIKE 'Isa%';

SELECT MAX(FirstName)

FROM dbo.MyView AS v;

SELECT MAX(FirstName)

FROM (SELECT FirstName, LastName

FROM Person.Person

WHERE LastName LIKE 'Isa%') AS v;

EXPAND

Page 40: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Indexed Views

• Introduced in SQL Server 2000

• Commonly referred to as materialized / persisted

views

• Ability to create indexes on a view

• Must create a unique, clustered index initially

• Benefits

• Joins and aggregations that process many rows

• Join and aggregation operations that are frequently

performed by many queries

• Decision support workloads

AD-302-HD | The Ultimate Session on Indexing 40

Page 41: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Indexed View Requirements

• “Fussy”: • The ANSI_NULLS and QUOTED_IDENTIFIER options must have been set

to ON when the CREATE VIEW statement was executed. • The ANSI_NULLS option must have been set to ON for the execution

of all CREATE TABLE statements that create tables referenced by the view

• The view must not reference any other views, only base tables

• All base tables referenced by the view must be in the same database as the view and have the same owner as the view

• The view must be created with the SCHEMABINDING option. Schema binding binds the view to the schema of the underlying base tables

• User-defined functions referenced in the view must have been created with the SCHEMABINDING option

• Tables and user-defined functions must be referenced by two-part names in the view. One-part, three-part, and four-part names are not allowed

• All functions referenced by expressions in the view must be deterministic

• More in BOL!

AD-302-HD | The Ultimate Session on Indexing 41

Page 42: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

NOEXPAND Query Hint

• Document, as might need to refactor!

AD-302-HD | The Ultimate Session on Indexing 42

Enterprise Edition Other Editions

NOEXPAND hint used Indexed view always

used

Indexed view always

used

NOEXPAND hint not

used

Indexed view may be

used

Indexed view never

used

Indexed view not

queried

Indexed view may be

used

Indexed view never

used

Page 43: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Indexed View Considerations

• Ultimately indexed views represent another

form of denormalization

• Now we have filtered indexes

• Computed, persisted columns

• “Wider” execution plans are generated

• Not used as often now as we have other

options in SQL Server

AD-302-HD | The Ultimate Session on Indexing 43

Page 44: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Partitioned Indexes

• Introduced in SQL Server 2005 Enterprise

Edition

• Two Types

• Aligned

• Unaligned

AD-302-HD | The Ultimate Session on Indexing 44

Page 45: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Unaligned Partitioned Indexes

AD-302-HD | The Ultimate Session on Indexing 45

[Sales] table

2007

2007 - 2011

2008 2011 2010 2009

Page 46: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Unaligned Partitioned Indexes

AD-302-HD | The Ultimate Session on Indexing 46

[Sales] table

2007 - 2011

2007 2008 2009 2010 2011

Page 47: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Aligned Partitioned Indexes

• Need to be aligned for efficient partition

switching

AD-302-HD | The Ultimate Session on Indexing 47

[Sales] table

2007

2007 2008 2009 2010 2011

2008 2011 2010 2009

Page 48: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Partitioned Indexes Benefits

• As with tables different partitions of the

index can be configured differently

• Row compression

• Page compression

• Lock escalation

• Can rebuild index at partition level!

• No need to rebuild partitions for older “static”

partitions

AD-302-HD | The Ultimate Session on Indexing 48

Page 49: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Filtered Indexes

• Filtered Indexes

• Filtered Index Limitations

AD-302-HD | The Ultimate Session on Indexing 49

Page 50: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Filtered Indexes

• Introduced in SQL Server 2008

• A filtered index is an optimized

nonclustered index, especially suited to

cover queries that select from a well-

defined subset of data

• Advantages

• Improved query performance and plan quality

• Reduced index maintenance costs

• Reduced index storage costs

AD-302-HD | The Ultimate Session on Indexing 50

Page 51: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Filtered Index Use Cases

• Large tables with small percentage of “useful” data / data

• 100,000s customers • Only 5% of customers have a pet

• 1,000,000s sales records going back 10 years • 99% of table is historical

• Historical data is hardly queries / queried differently

• Wide tables (many columns) where columns are partially related

• 100s of columns • [BikeColor] is only relevant to [ProductType] = ‘Bike’

AD-302-HD | The Ultimate Session on Indexing 51

Page 52: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Implementing Filtered Index

• Welcome to SQL Server’s fussiest feature ☺

• Cannot create what you want

• Can create it, but the Query Optimizer will not

use it

• Feedback: various degrees of success

• My take is that sparse columns drove

filtered indexes

• Important to test in DEV/UAT

AD-302-HD | The Ultimate Session on Indexing 52

Page 53: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Filtered Index Limitations

• Only supports very simple comparison logic

in predicate

• Cannot create a filtered index on a view

• Query optimizer can benefit from a filtered

index defined on a table that is referenced

in a view

• Cannot be created on computed fields

• Even if they are persisted

AD-302-HD | The Ultimate Session on Indexing 53

Page 54: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

From BOL

• WHERE <filter_predicate>

• <filter_predicate> ::= <conjunct> [ AND <conjunct> ]

• <conjunct> ::= <disjunct> | <comparison>

• <disjunct> ::= column_name IN (constant ,…)

• <comparison> ::= column_name <comparison_op>

constant

• comparison_op> ::= { IS | IS NOT | | <> | ! | > | > |

!> | < | < |= | != | >= | <= | !< }

• No BETWEEN, no LIKE, no subquery, no variables

• Simple and deterministic

AD-302-HD | The Ultimate Session on Indexing 54

Page 55: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Filtered Indexes Requirements

• Comparison work involved, so we must agree on various SET options

• ON • ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT,

CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER

• OFF • NUMERIC ROUNDABORT

• Otherwise: • If not set when index is created, won’t create the index

• If not set when INSERT, UPDATE, DELETE, MERGE affects the data, gives error and rolls back

• If not set when the index might be used to optimize the query, it will not be considered

AD-302-HD | The Ultimate Session on Indexing 55

Page 56: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Filtered Indexes versus Indexed Views

• Advantages over Indexed Views • Reduced index maintenance costs

• Improved plan quality

• Online index rebuilds

• Non-unique indexes

AD-302-HD | The Ultimate Session on Indexing 56

Allowed in expressions Views Filtered indexes

Computed columns Yes No

Joins Yes No

Multiple tables Yes No

Simple comparison logic in a predicate* Yes Yes

Complex logic in a predicate** Yes No

Page 57: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Statistics

• Statistics

• Filtered statistics

AD-302-HD | The Ultimate Session on Indexing 57

Page 58: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Statistics

• The query optimizer uses statistics to create

query plans that improve query

performance

• Statistical information about the distribution of

values in one or more columns of a table (or

indexed view)

• The query optimizer uses these statistics to

estimate the cardinality, or number of rows, in

the query result

• Maximum of 200 steps

AD-302-HD | The Ultimate Session on Indexing 58

Page 59: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Automatic Statistics Creation

• Automatically created

• Database Options • AUTO_CREATE_STATISTICS

• AUTO_UPDATE_STATISTICS

• AUTO_UPDATE_STATISTICS_ASYNC • Introduced in SQL Server 2005

• View • sys.stats

• DBCC SHOW_STATISTICS

AD-302-HD | The Ultimate Session on Indexing 59

Page 60: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Automatic Statistics Update

• When table row count goes from zero to not zero

• Table had less than 500 rows and there have been more than 500 changes to the leading column of the stat since the last stat update

• Table had more than 500 rows and there have been at least 500 + 20% changes to the leading column in the stat since the last update

AD-302-HD | The Ultimate Session on Indexing 60

Page 61: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Manual Statistics Creation

• Can create additional statistics

• These additional statistics can capture

statistical correlations that the query

optimizer does not account for when it

creates statistics for indexes or single

columns

AD-302-HD | The Ultimate Session on Indexing 61

Page 62: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Filtered Statistics

• Introduced in SQL Server 2008

• Filtered statistics can improve query performance for queries that select from well-defined subsets of data

• Filtered statistics use a filter predicate to select the subset of data that is included in the statistics

• Well-designed filtered statistics can improve the query execution plan compared with full-table statistics

AD-302-HD | The Ultimate Session on Indexing 62

Page 63: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Filtered Statistics Use Cases

• On skewed data to assist Query Optimizer

• Potentially on very large tables

• Create multiple statistics on same data with

different filters

• Filtered statistics will be more precise

• Partitioned tables

• Only required on current partition

• Required on historical partitions

AD-302-HD | The Ultimate Session on Indexing 63

Page 64: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Filtered Indexes Benefits

• Potentially get better execution plans for

larger volumes of data

• Better quality statistics

• Less need to update statistics

• Older data does not change

• Older data changes less frequently

AD-302-HD | The Ultimate Session on Indexing 64

Page 65: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Filtered Statistics

• One of the great advantages of the filtered

statistics is that if we switch in new partition

into the partitioned table filtered statistics

built on the older partitions does not

become invalid and you don’t need to

update it

• You only need to create new filtered statistics for

the new partition which is much faster process

comparable to if you need to update or build

statistics for entire table.

AD-302-HD | The Ultimate Session on Indexing 65

Page 66: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Filtered Statistics

CREATE STATISTICS …

ON …

WHERE …

AD-302-HD | The Ultimate Session on Indexing 66

Page 67: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Basic Indexing Strategies

• Basic Indexing Strategies

• Missing Indexes

AD-302-HD | The Ultimate Session on Indexing 67

Page 68: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Basic Indexing Strategies

• FLOAT / REAL columns

• Limited value

• BIT columns

• Encryption

• Data compression

• Improve page density

• Don’t forget you can implement compression at

the partition level

AD-302-HD | The Ultimate Session on Indexing 68

Page 69: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Basic Indexing Strategies

• DSS versus OLTP

• Too many indexes might slow down DML

operations unacceptably

• Create more indexes in a DSS environment

• Multiple indexes versus composite indexes

• Index intersection

• Use filtered indexes to create unique

indexes that support multiple NULL values

AD-302-HD | The Ultimate Session on Indexing 69

Page 70: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Fragmentation

• Fragmentation

• Analysing Fragmentation

• Page Splits

• FILLFACTOR

AD-302-HD | The Ultimate Session on Indexing 70

Page 71: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Fragmentation

• Over a period of time indexes will no longer

be optimal and become fragmented

• DML operations

• Page splits

• Forwarding records

• Fragmentation

• Internal

• External

AD-302-HD | The Ultimate Session on Indexing 71

Page 72: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Analyzing Fragmentation

• DBCC SHOWCONTIG

• Deprecated

• sys.dm_db_index_physical_stats system

function

AD-302-HD | The Ultimate Session on Indexing 72

Page 73: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Page Splits

• Clustered indexes dictate the physical

order in which data is stored

• What happens if you want to insert a new

record into a page that is full?

• What happens if you want to update a

record and there is not enough room on

the page?

• Page split

AD-302-HD | The Ultimate Session on Indexing 73

Page 74: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

FILLFACTOR

• Purpose is to “postpone” page splits

• Only uses a certain percentage of each page during index creation and rebuilds

• PAD_INDEX Option • Applies fill factor to intermediate pages

• Will not always help

• Data consumes extra disk space and EXTRA RAM!

• Fine-tuning mechanism

AD-302-HD | The Ultimate Session on Indexing 74

Page 75: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Maintaining Indexes

• Rebuilding indexes

• Reorganizing indexes

• Maintaining statistics

AD-302-HD | The Ultimate Session on Indexing 75

Page 76: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Rebuilding Indexes

• Can be done offline or online • ONLINE option

• Uses database or tempdb • SORT_IN_TEMPDB option

• Temporary space required • Sort run • Mapping index

• Version store

• Version store uses tempdb • sys.dm_db_task_space_usage

• sys.dm_db_session_space_usage • sys.dm_db_file_space_usage

AD-302-HD | The Ultimate Session on Indexing 76

Page 77: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Offline Index Rebuilds

• Only option in SQL Server Standard Edition

and below

• ALTER INDEX … REBUILD

• CREATE INDEX … DROP_EXISTING

• Alternative

• Reorganizing indexes

AD-302-HD | The Ultimate Session on Indexing 77

Page 78: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

REBUILD versus DROP_EXISTING

AD-302-HD | The Ultimate Session on Indexing 78

Functionality REBUILD DROP_

EXISTING

Index definition can be changed by adding or removing key columns, changing

column order, or changing the column sort order.*

No Yes**

Index options can be set or modified Yes Yes

More than one index can be rebuilt in a single transaction Yes No

Most index types can be rebuilt online without blocking running queries or updates Yes Yes

Partitioned index can be repartitioned No Yes

Index can be moved to another filegroup No Yes

Additional temporary disk space is required Yes Yes

Rebuilding a clustered index rebuilds associated nonclustered indexes No*** No****

Indexes enforcing PRIMARY KEY and UNIQUE constraints can be rebuilt without

dropping and re-creating the constraints

Yes Yes

Single index partition can be rebuilt Yes No

Page 79: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Online Index Rebuilds

• Introduced in SQL Server 2005 Enterprise

Edition

• SORT_IN_TEMPDB index rebuild option

• 3 Phases

• Preparation

• Build

• Finalize

AD-302-HD | The Ultimate Session on Indexing 79

Page 80: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Online Index Rebuild: Phase 1

• Locks acquired

• Metadata created

• DML plans are recompiled

AD-302-HD | The Ultimate Session on Indexing 80

Page 81: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Online Index Rebuild: Phase 2

• Populate the new index with sorted data

from the existing data source while allowing

select and DML operations to continue

• Mapping index created for clustered index

AD-302-HD | The Ultimate Session on Indexing 81

Page 82: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Online Index Rebuild: Phase 3

• Make new index “available” and “clean

up”

AD-302-HD | The Ultimate Session on Indexing 82

Page 83: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Reorganizing Indexes

• Reorganizing an index defragments the leaf level of clustered and nonclustered indexes on tables and views by physically reordering the leaf-level pages to match the logical order (left to right) of the leaf nodes

• Uses existing pages

• Additionally “compacts” pages

• Emptied pages are “available”

• Uses “minimal resources”

• Always online operation

• ALTER INDEX … REORGANIZE

AD-302-HD | The Ultimate Session on Indexing 83

Page 84: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

REBUILD versus REORGANIZE

• REORGANIZE does not UPDATE STATISTICS

• Query sys.dm_db_index_physical_stats system function to determine level of fragmentation

• avg_fragmentation_in_percent column returns the percent of logical fragmentation (out-of-order pages in the index).

• Microsoft’s recommendations:

AD-302-HD | The Ultimate Session on Indexing 84

avg_fragmentation_in_percent Statement

> 5% AND <= 30% ALTER INDEX REORGANIZE

> 30% ALTER INDEX REBUILD WITH (ONLINE = ON)

Page 85: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Victor’s Alternative for Online Index Rebuilds • Scenario:

• 24 x 7 database solution

• Want to take advantage of online index rebuilds

• Have to buy Enterprise Edition • Enterprise Edition is expensive

• Especially if you just want to buy it for this one feature

• Alternative: • Potentially can get away with REORGANIZING

clustered indexes

• Rebuild nonclustered indexes differently: • Create new nonclustered index on same columns

• Drop existing nonclustered index

• Rename new nonclustered index to match old one’s name

AD-302-HD | The Ultimate Session on Indexing 85

Page 86: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Advanced Indexing Strategies

• Hash Index

• Helper Columns

AD-302-HD | The Ultimate Session on Indexing 86

Page 87: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

“Helper” Columns

• Hash Index

• Helper Columns

AD-302-HD | The Ultimate Session on Indexing 87

Page 88: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Hash Index

• Use case: • You want to create an index on a column that is

greater than 900 bytes

• Might not want to leverage full text indexes

• Solution: • Create a computed, persisted column that

“hashes” large column • LEFT, CHECKSUM, BINARY_CHECKSUM, HashBytes, etc

• Create index on computed, persisted columns

• Change SARG to include computed

AD-302-HD | The Ultimate Session on Indexing 88

Page 89: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

“Helper” Index

• Use case: • You want to create an index on a column that is

greater than 900 bytes

• Might not want to leverage full text indexes

• Data is predominantly inserted?

• Solution: • Create a computed, persisted bit column that

searches for a pattern • PATINDEX, etc

• Create index on computed, persisted bit column

• Change SARG to include bit index

• Remember: you cannot take advantage of filtered indexes here

AD-302-HD | The Ultimate Session on Indexing 89

Page 90: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Q & A

Questions?

AD-302-HD | The Ultimate Session on Indexing 90

Email [email protected]

Blog www.victorisakov.com

Twitter @victorisakov

LinkedIn www.linkedin.com/in/victorisakov

Website www.sqlserversolutions.com.au

Certification

Page 91: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

Complete the Evaluation Form to Win! Win a Dell Mini Netbook – every day – just for handing in your completed form. Each session evaluation form represents a chance to win.

Pick up your evaluation form: • In each presentation room

• Online on the PASS Summit website

Drop off your completed form: • Near the exit of each presentation room

• At the Registration desk

• Online on the PASS Summit website

Sponsored by Dell

94 AD-302-HD | The Ultimate Session on Indexing

Page 92: The Ultimate Session on Indexing - Victor Isakov | SQL Ranger · 2011. 10. 14. · such as Microsoft Tech·Ed, SQL Connections, PASS Summit, and SQL Code Camp. He has authored a number

October 11-14, Seattle, WA

Thank you for attending this session and the

2011 PASS Summit in Seattle