Indexing for Beginners Kathi Kellenberger Kellenberger Consulting, LLC
Who am I? Teacher Author Consultant @auntkathi http://auntkathisql.com Beginning T-SQL Expert T-SQL Window Functions Beginning SSRS Consultant @auntkathi http://auntkathisql.com
The problem…. Our application is slow, and we don’t know why. We are seeing lots of timeouts. There is something wrong with our SQL Server; it used to be fast. We have indexes, but we don’t know if they are the right ones.
What they have tried Added processors Upgraded storage Added random indexes
Make SQL Server work less Table
Make SQL Server work less Index
Find My Number Game
Two types of tables Heaps Clustered Indexes
Heaps M S D X A T A
Clustered Index M D T A S X A D M S T X
Clustered Index M Non-Leaf Levels D T Contain the key A S X Leaf Level Contains the key other columns, and possibly a Uniquifier
Clustered Indexes Best Practices Narrow Unique Never changing Incrementing* Based on workload *Subject to debate
Two types of indexes Clustered Index Nonclustered Index
Nonclustered Index M Non-Leaf Levels D T Contain the key A S X Contains the key, Pointer to the table row, and included columns
Pointer to table row Clustered index – Cluster Key Heap – RID (row identifier)
Column order LastName FirstName City
Covering index
SARGability
SARGability Causes Function applied to column LIKE with leading wildcard (%) NOT LIKE No filter on leading column Implicit conversions
Nonclustered Index Best Practices Can have to 999, but try to stay at 10 or less indexes per table Foreign keys Common WHERE clause columns Use workload to tune Filter before joining columns Take advantage of included columns