Indexes: The Basics Kathi Kellenberger
Good things come in small packages…
Heaps B-Trees Two Types of Tables Heaps – not logically organized. Table without a clustered index B-Trees – used for clustered indexes and non-clustered indexes Data is organized by the key Heaps B-Trees
Heap M S D X A T F When looking for a value within a heap, have to scan the data to find it. If I am looking for A, I have to look at M, S, D, X to get to A No organization Good in some circumstances like loading staging tables
B-Tree M T D A F S X Balanced tree Hierarchy When I want to find a value, just have travel down the levels To find A, I start at M, is A less than M? Go to D. Is A Less than D? Go to A. When you add a clustered index to a table, it is organized as a B Tree A F S X
Find the Number Game How long to find a number when the numbers are random? How long when they are organized in a b-tree structure?
Clustered Index Structure M D T Non-Leaf Levels A F S X A D F Leaf Level M S T X Non-leaf levels contain the cluster key Leaf levels contain the data pages: cluster key + non-key columns
The best cluster key is ever-increasing M S D X A T F You can see lots of opinions about clustered index key. The best key is ever increasing. That means a date or ID column
The best cluster key is narrow and unique
The Best Clustered Key is Static CHANGES
Non-Clustered Indexes
Non-Clustered Indexes
Non-clustered Index Structure M D T Non-Leaf Levels A F S X A D F Leaf Level M S T X Non-leaf levels contain the Index Key Leaf levels contain the Index Key, Pointer to table row and Included Columns
Column Order Is Important! LastName FirstName City
Key Lookups When the index just isn’t enough
Covering Indexes
Best Practices Index foreign keys Index keys used in searches Avoid duplicate indexes Avoid indexes that are rarely used – unless there is a business reason DO NOT BLINDLY FOLLOW RECOMENDATIONS
Index Maintenance Maintenance Wizard Scripts Reorganize Rebuild Rebuild at 30% fragmentation Reorg > 10% and < 30% Ignore small tables Record what is done
Resources Glenn Berry Blog: http://sqlserverperformance.wordpress.com/ Expert Performance Indexing for SQL Server 2012 by Jason Strate (Apress) Ola Hallengren maintenance solution: https://ola.hallengren.com/ Duplicate index script: http://www.sql-server-performance.com/2013/find- duplicate-indexes-on-sql-server/