Introduction To Indexes
Introduction To Indexes
Introduction To Indexes
Introduction to Indexes
Module Overview
• Table Scan
• SQL Server reads all table pages
• Any query can be satisfied by a table scan
• Will result in the slowest response to a query
• A table without indexes is called a heap
• Index
• SQL Server uses index pages to find the desired rows
• Different types
• Clustered and nonclustered
• Rowstore and columnstore
The Need for Indexes
Index Structures
SQL Server B-Tree Structure
Selectivity, Density and Index Depth
• Selectivity
• A measure of how many rows are returned compared to the total
number of rows
• High selectivity means a small number of rows when related to the
total number of rows
• Density
• A measure of the lack of uniqueness of data in the table
• High density indicates a large number of duplicates
• Index Depth
• Number of levels within the index
• Common misconception that indexes are deep
Index Fragmentation
• Types of fragmentation:
• Internal – pages are not full
• External – pages are out of logical sequence
• Detecting fragmentation
• SQL Server Management Studio – Index Properties
• System function – sys.dm_db_index_physical_stats
Demonstration: Viewing Index Fragmentation
• Negatives
• Small data types will be more dense
Character Index Data
• Negatives
• Slower to search than a numeric index
• Can become fragmented because data does not tend
to be sequential
Date-Related Index Data
• Negatives
• Small data types will be more dense
GUID Index Data
• Negatives
• Updates and deletes do not perform as well
BIT Index Data
• Negatives
• Frequent changes can impair performance
• Computed columns must be deterministic
Lesson 3: Heaps, Clustered, and Nonclustered
Indexes
Heaps
Operations on a Heap
Clustered Indexes
Operations on a Clustered Index
Primary Keys and Clustering Keys
Nonclustered Indexes
Operations on Nonclustered Indexes
• Demonstration: Working with Clustered and
Nonclustered Indexes
Heaps
• INSERT
• Each new row can be placed in the first available page with
sufficient space
• UPDATE
• The row can remain on the same page if it still fits; otherwise, it
can be removed from the current page and placed on the first
available page with sufficient space
• DELETE
• Frees up space on the current page
• Data is not overwritten, space is just flagged as available for reuse
• SELECT
• Entire table needs to be scanned
Clustered Indexes
A clustered index:
• Has pages that are logically ordered
• Has rows that are logically ordered, and where
possible, physically ordered within pages
• Can only be declared once on a table
• INSERT
• Each new row must be placed into the correct logical position
• May involve splitting pages of the table
• UPDATE
• The row can remain in the same place if it still fits and if the
clustering key value is still the same
• If the row no longer fits on the page, the page needs to be split
• If the clustering key has changed, the row needs to be removed
and placed in the correct logical position within the table
• DELETE
• Frees up space by flagging the data as unused
• SELECT
• Queries related to the clustering key can seek
• Queries related to the clustering key can scan and avoid sorts
Primary Keys and Clustering Keys
• Primary key
• Must be unique
• Cannot contain NULL values
• Only one per table
• Implemented as a constraint
• Clustering key
• Must be unique
• Specifies the logical ordering of rows
• Only one per table
• Can be automatically created
Nonclustered Indexes
A nonclustered index :
• Can be on a heap or clustered index
• Will take up extra space
• Needs to be updated when the underlying
data is modified
A table can have a maximum of 999 nonclustered
indexes.
Operations on Nonclustered Indexes
• INSERT
• Each nonclustered index that is added to a table will decrease the
performance of inserts
• UPDATE
• The index will need to be kept up to date if the location of the
data changes
• DELETE
• Similar to updates, deleted data needs to be removed from the
index
• SELECT
• Performance improvements for queries that the index covers
Demonstration: Working with Clustered and
Nonclustered Indexes
Logon Information
Virtual machine: 20762C-MIA-SQL
User name: ADVENTUREWORKS\Student
Password: Pa55w.rd
• Best Practice