All about Indexes Gail Shaw
Please Thank & Support our Sponsors SQL Saturday is made possible with the generous support of these sponsors. You can support them by opting-in and visiting them in the sponsor area. Global Alliance Partner: Venue & Internet Sponsor: Silver Sponsor: Operations Support: Endless Support from: OGMA Consulting Corp. Bronze Sponsor:
Agenda Why index? Index Architecture Non-clustered Indexes Other index types
Overview
Why index? To reduce the number of rows in consideration for a query as early as possible To support operations that require a sort order for rows To enforce uniqueness upon a column, set of columns or subset of values in a column
Index Architecture
Index architecture B-tree (balanced tree) Double-linked list at all levels
Index architecture
Index pages Two types of pages found in indexes Data pages Index pages Data pages found at the leaf level of a clustered index Index pages found at the non-leaf level of a clustered index and all levels of a nonclustered index
Index operations Seek Navigation down the b-tree to find a row or the start/end of a range of rows Scan Read of (potentially) the entire index leaf-level Lookup Single-row seek against the clustered index/heap
Definitions Density – Measure of how unique a column is Selectivity – What % of a table a predicate will return Cardinality – How many rows a query operator will affect Predicate – An expression that evaluates to True or False. Seek predicate – A predicate which SQL can evaluate by traversing an index’s b-tree
Nonclustered Indexes
Architecture Nonclustered indexes are secondary structures They are separate from the table but always in-sync with it One row in the index for each row in the table Each index row has a pointer to the actual data row If the table has a clustered index, this is the clustered index key If the table is a heap, this is the RID (file ID : page number : slot index)
Architecture A, 2 K, 7 A, 2 C, 8 E, 1 E, 8 I, 6 K, 7 M, 6 N, 9 S, 1 F, 7 F, 9 H, 1 I, 6 K, 7 M,6 N,9 S,1 S, 8
Guidelines Indexes should be selective or covering (or both) Wider indexes tend to be more useful Consider covering indexes for frequently run queries Consider indexes to support order by and group by Use INCLUDE for columns that are selected, but not involved in filters/joins
Indexing for equality When all where clause predicates are an equality Order of columns in the index don’t matter for this query SQL does not seek on one, then the second, etc SQL will do a multi-column seek to the beginning or end of the range of rows Order of index columns does matter when trying to support multiple queries with one index Seek only possible on a left-based subset of the index key Don’t put most selective column first by default
Indexing for inequality Columns used for equality matches before those for inequality When dealing with two or more inequality predicates, column order implied by which predicate returns fewer rows Selectivity much less important for inequality predicates, cardinality is more relevant
Indexing for sorts/group by Indexes are logically ordered by the index key columns As such, an index can provide the order needed for an ORDER BY or a stream aggregate Usually should be considered after considering indexes to support the WHERE clause Not always possible to achieve
Indexing for joins Three join types in SQL Server Nested loop Merge join Hash join
Indexing for joins Nested loop can benefit from an index on the inner table Merge requires rows sorted in the join order, so can benefit from indexes on both tables Hash doesn’t require any indexes at all Secondary consideration after indexing for the WHERE clause
Filtered indexes Index on a subset of the table Filter condition should be simple. It cannot reference a computed column, spatial data types or Hierarchy. Very useful for enforcing uniqueness on a subset of the table To be useful for a query, the predicate must match exactly Parameterised queries may not use the filtered index
Include columns Only included at the leaf level of the index Does not count towards the 16 column/900 byte limit for index keys Only TEXT, NTEXT, IMAGE cannot be INCLUDE columns
Thank you