SlideShare a Scribd company logo
Using indexes
Index architecture
clustered
nonclustered
CREATE INDEX (T-SQL)
Useful tips
Index options
Spatial indexes

1
Index architecture

2
Index architecture

3
Index architecture (clustered)

4
Index architecture (nonclustered)

5
CREATE INDEX (T-SQL)
CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX
index_name
ON <object> ( column [ ASC | DESC ] [ ,...n ] )
[ INCLUDE ( column_name [ ,...n ] ) ]
[ WHERE <filter_predicate> ]
[ WITH ( <relational_index_option> [ ,...n ] ) ]
[ ON { partition_scheme_name ( column_name )
| filegroup_name
| default
}
]
[ FILESTREAM_ON { filestream_filegroup_name |
partition_scheme_name | "NULL" } ]

!
[;]

6
CREATE INDEX (T-SQL)
<relational_index_option> ::=
{
PAD_INDEX = { ON | OFF }
| FILLFACTOR = fillfactor
| SORT_IN_TEMPDB = { ON | OFF }
| IGNORE_DUP_KEY = { ON | OFF }
| STATISTICS_NORECOMPUTE = { ON | OFF }
| DROP_EXISTING = { ON | OFF }
| ONLINE = { ON | OFF }
| ALLOW_ROW_LOCKS = { ON | OFF }
| ALLOW_PAGE_LOCKS = { ON | OFF }
| MAXDOP = max_degree_of_parallelism
| DATA_COMPRESSION = { NONE | ROW | PAGE}
[ ON PARTITIONS ( { <partition_number_expression> | <range> }
[ , ...n ] ) ]
}
7
Useful tips
Consider using a clustered index for queries that do the following:

!
•
•
•
•

Return a range of values by using operators such as BETWEEN, >, >=, <, and <=.
Return large result sets.
Use JOIN clauses; typically these are foreign key columns.
Use ORDER BY, or GROUP BY clauses.

•
•
•
•

Are unique or contain many distinct values
Are accessed sequentially
Defined as IDENTITY because the column is guaranteed to be unique within the table.
Used frequently to sort the data retrieved from a table.

•
•

Columns that undergo frequent changes
Wide keys

!
Consider columns that have one or more of the following attributes:
!

!
Clustered indexes are not a good choice for the following attributes:
!

8
Useful tips
Consider the characteristics of the database when designing nonclustered indexes.

!

• Databases or tables with low update requirements, but large volumes of data can benefit
from many nonclustered indexes to improve query performance. Consider creating filtered
indexes for well-defined subsets of data to improve query performance, reduce index storage
costs, and reduce index maintenance costs compared with full-table nonclustered indexes.
• Online Transaction Processing applications and databases that contain heavily updated
tables should avoid over-indexing. Additionally, indexes should be narrow, that is, with as few
columns as possible.

!
Consider using a nonclustered index for queries that have the following attributes:
!

• Use JOIN or GROUP BY clauses.
• Queries that do not return large result sets.
• Contain columns frequently involved in search conditions of a query, such as WHERE clause,
that return exact matches.

!
Consider columns that have one or more of these attributes:
!

• Cover the query.
• Lots of distinct values, such as a combination of last name and first name, if a clustered index
is used for other columns.

9
Useful tips
Query in which the
column predicate is
one of these
Exact match to a specific
value

Query description and example

Index to consider

Searches for an exact match in which the query uses the WHERE
Nonclustered or
clause to specify a column entry with a specific value. For example: clustered index on the
Id column.

! Id, [Login], Email FROM Users WHERE Id = 202
SELECT

Searches for an exact match to a value in a specified list of values.
Exact match to a value in an For example:
IN (x,y,z) list

! Id, [Login], Email FROM Users WHERE Id IN (603, 658, 1371)
SELECT

Nonclustered or
clustered index on the
Id column.

Searches for a range of values in which the query specifies any entry
that has a value between two values. For example:

Range of values

! Id, UserId, CreateDate, Content FROM Posts WHERE CreateDate
SELECT
>= '2013-03-01 12:00' AND CreateDate <= '2013-03-01 12:30‘
!
Or
! Id, UserId, CreateDate, Content FROM Posts WHERE CreateDate
SELECT

Clustered or
nonclustered index on
the CreateDate column.

BETWEEN '2013-03-01 12:00' AND '2013-03-01 12:30'

Join between tables

Searches for rows in a table that match a row in another table based
Nonclustered or
on a join predicate. For example:
clustered index on the
Id and UserId columns.
SELECT u.Id, u.[Login], p.Content

!

FROM Users u JOIN Posts p ON u.Id = p.UserId WHERE u.Id = 202

10
Useful tips
Query in which the
column predicate is
one of these
LIKE comparison

Sorted or aggregated

PRIMARY KEY or UNIQUE
constraint

Query description and example
Searches for matching rows that start with a specific character
string such as 'abc%'. For example:

! Id, [Login], Email FROM Users WHERE [Login] LIKE 'ma%'
SELECT

Nonclustered or
clustered index on the
Login column.

Nonclustered or
Requires an implicit or explicit sort order or an aggregation (GROUP clustered index on the
BY). For example:
sorted or aggregated
column.
SELECT p.Id, p.UserId, u.[Login], p.Content
For sort columns,
FROM Posts p JOIN Users u ON p.UserId = u.Id
consider specifying the
ORDER BY p.UserId, p.CreateDate
ASC or DESC order of
Searches for duplicates of new index key values in insert and update Clustered or
operations, to enforce PRIMARY KEY and UNIQUE constraints. For
nonclustered index on
example:
the column or columns

!

! Users ([Login], [Password], Email)
INSERT

VALUES ('xdfyht', 'dh4G57hn1', 'xdfyht@gmail.com')

UPDATE or DELETE operation Searches for rows in an update or delete operation in which the
in a PRIMARY KEY/FOREIGN column participates in a PRIMARY KEY/FOREIGN KEY relationship,
KEY relationship
with or without the CASCADE option.
Contains one or more columns in the select list that are not used for
searching and lookups. For example:
Column is in the select list
but not in the predicate.

Index to consider

! UserId, CreateDate, Content FROM Posts
SELECT

WHERE UserId = 202 ORDER BY CreateDate DESC

defined in the
constraint.
Nonclustered or
clustered index on the
foreign key column.
Nonclustered index with
Content specified in the
INCLUDE clause.

11
Index options (fill factor)

12
Index options (included columns)

13
Index options (filtered)
Views vs. Filtered Indexes
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

14
Spatial indexes

15
Spatial indexes

16
Spatial indexes

17
Spatial indexes

18
Spatial indexes (conditions)
•
•
•
•
•
•
•
•

geometry1.STContains(geometry2) = 1
geometry1.STDistance(geometry2) < @r
geometry1.STDistance(geometry2) <= @r
geometry1.STEquals(geometry2) = 1
geometry1.STIntersects(geometry2) = 1
geometry1.STOverlaps(geometry2) = 1
geometry1.STTouches(geometry2) = 1
geometry1.STWithin(geometry2) = 1

•
•
•
•

geography1.STEquals(geography2) = 1
geography1.STDistance(geography2) < @r
geography1.STDistance(geography2) <= @r
geography1.STIntersects(geography2) = 1
19
about:me
http://
unknowntransfer.blogspot.com
akor@ciklum.com
Skype: unknowntransfer

20

More Related Content

PPTX
SQL Server Index and Partition Strategy
PPTX
Introduction of sql server indexing
PDF
Database Indexes
PPTX
Geek Sync | SQL Server Indexing Basics
PPTX
Database index
PPT
Module08
PPT
Mysql Indexing
SQL Server Index and Partition Strategy
Introduction of sql server indexing
Database Indexes
Geek Sync | SQL Server Indexing Basics
Database index
Module08
Mysql Indexing

What's hot (20)

ODP
Database index by Reema Gajjar
PPTX
Indexes: The Second Pillar of Database Wisdom
PPT
Database indexing framework
PDF
Columnstore indexes in sql server 2014
PPTX
Sql server ___________session_17(indexes)
PPTX
dotnetMALAGA - Sql query tuning guidelines
PPTX
Sql server lesson6
PPSX
Index Tuning
PPTX
Database indexing techniques
PPTX
9. index and index organized table
PPTX
SQL_Part1
PPTX
Dynamic Data Validation Lists
PDF
Effective Use of Excel
PDF
MySQL: Indexing for Better Performance
PPT
New slides access
PDF
Sql server 2014 x velocity – updateable columnstore indexes
PPTX
Tunning sql query
PPTX
Advance microsoft excel institute in delhi
PPTX
Dynamic Publishing with Arbortext Data Merge
Database index by Reema Gajjar
Indexes: The Second Pillar of Database Wisdom
Database indexing framework
Columnstore indexes in sql server 2014
Sql server ___________session_17(indexes)
dotnetMALAGA - Sql query tuning guidelines
Sql server lesson6
Index Tuning
Database indexing techniques
9. index and index organized table
SQL_Part1
Dynamic Data Validation Lists
Effective Use of Excel
MySQL: Indexing for Better Performance
New slides access
Sql server 2014 x velocity – updateable columnstore indexes
Tunning sql query
Advance microsoft excel institute in delhi
Dynamic Publishing with Arbortext Data Merge
Ad

Viewers also liked (20)

PDF
Scaling the Content Repository with Elasticsearch
PDF
Elasticsearch and Symfony Integration - Debarko De
DOCX
Index in sql server
PPTX
Before you optimize: Understanding Execution Plans
PDF
Discovering ElasticSearch
PDF
Strategies for SQL Server Index Analysis
PDF
Presentation interpreting execution plans for sql statements
PPTX
Consultas en MS SQL Server 2012
PPTX
MS SQL SERVER: Creating Views
PDF
Sql db optimization
PDF
Using grouping sets in sql server 2008 tech republic
PPT
Sql tuning guideline
PPS
Sql xp 04
PPTX
Statistics
PPT
e computer notes - Subqueries
PPTX
Sub-queries,Groupby and having in SQL
PPSX
Locking in SQL Server
PPT
Overview of query evaluation
PPT
Review of SQL
Scaling the Content Repository with Elasticsearch
Elasticsearch and Symfony Integration - Debarko De
Index in sql server
Before you optimize: Understanding Execution Plans
Discovering ElasticSearch
Strategies for SQL Server Index Analysis
Presentation interpreting execution plans for sql statements
Consultas en MS SQL Server 2012
MS SQL SERVER: Creating Views
Sql db optimization
Using grouping sets in sql server 2008 tech republic
Sql tuning guideline
Sql xp 04
Statistics
e computer notes - Subqueries
Sub-queries,Groupby and having in SQL
Locking in SQL Server
Overview of query evaluation
Review of SQL
Ad

Similar to "Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1 (20)

PPTX
Sql performance tuning
PDF
Mysql query optimization
PPT
Filtered Indexes In Sql 2008
PPT
Module08
PPTX
Index_2
PPTX
Query Optimization in SQL Server
PDF
Indexing techniques
PDF
SQA server performance tuning
PDF
Geek Sync I Consolidating Indexes in SQL Server
PPTX
SAG_Indexing and Query Optimization
PPTX
Database Performance Tuning
PDF
Microsoft SQL Server Filtered Indexes & Sparse Columns Feb 2011
PPS
07 qmds2005 session10
PDF
MySQL Indexing Crash Course
DOCX
Indexes in ms sql server
PDF
Scaling MySQL Strategies for Developers
PPT
Indexing
PPTX
presentation is on database for sql and stored procedures
PDF
MySQL INDEXES
Sql performance tuning
Mysql query optimization
Filtered Indexes In Sql 2008
Module08
Index_2
Query Optimization in SQL Server
Indexing techniques
SQA server performance tuning
Geek Sync I Consolidating Indexes in SQL Server
SAG_Indexing and Query Optimization
Database Performance Tuning
Microsoft SQL Server Filtered Indexes & Sparse Columns Feb 2011
07 qmds2005 session10
MySQL Indexing Crash Course
Indexes in ms sql server
Scaling MySQL Strategies for Developers
Indexing
presentation is on database for sql and stored procedures
MySQL INDEXES

Recently uploaded (20)

PDF
cuic standard and advanced reporting.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Big Data Technologies - Introduction.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
Cloud computing and distributed systems.
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Modernizing your data center with Dell and AMD
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Empathic Computing: Creating Shared Understanding
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
DOCX
The AUB Centre for AI in Media Proposal.docx
cuic standard and advanced reporting.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Big Data Technologies - Introduction.pptx
Chapter 3 Spatial Domain Image Processing.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Spectral efficient network and resource selection model in 5G networks
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Encapsulation_ Review paper, used for researhc scholars
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
NewMind AI Monthly Chronicles - July 2025
Cloud computing and distributed systems.
Digital-Transformation-Roadmap-for-Companies.pptx
Network Security Unit 5.pdf for BCA BBA.
Modernizing your data center with Dell and AMD
Unlocking AI with Model Context Protocol (MCP)
Empathic Computing: Creating Shared Understanding
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
The AUB Centre for AI in Media Proposal.docx

"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1

  • 1. Using indexes Index architecture clustered nonclustered CREATE INDEX (T-SQL) Useful tips Index options Spatial indexes 1
  • 6. CREATE INDEX (T-SQL) CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name ON <object> ( column [ ASC | DESC ] [ ,...n ] ) [ INCLUDE ( column_name [ ,...n ] ) ] [ WHERE <filter_predicate> ] [ WITH ( <relational_index_option> [ ,...n ] ) ] [ ON { partition_scheme_name ( column_name ) | filegroup_name | default } ] [ FILESTREAM_ON { filestream_filegroup_name | partition_scheme_name | "NULL" } ] ! [;] 6
  • 7. CREATE INDEX (T-SQL) <relational_index_option> ::= { PAD_INDEX = { ON | OFF } | FILLFACTOR = fillfactor | SORT_IN_TEMPDB = { ON | OFF } | IGNORE_DUP_KEY = { ON | OFF } | STATISTICS_NORECOMPUTE = { ON | OFF } | DROP_EXISTING = { ON | OFF } | ONLINE = { ON | OFF } | ALLOW_ROW_LOCKS = { ON | OFF } | ALLOW_PAGE_LOCKS = { ON | OFF } | MAXDOP = max_degree_of_parallelism | DATA_COMPRESSION = { NONE | ROW | PAGE} [ ON PARTITIONS ( { <partition_number_expression> | <range> } [ , ...n ] ) ] } 7
  • 8. Useful tips Consider using a clustered index for queries that do the following: ! • • • • Return a range of values by using operators such as BETWEEN, >, >=, <, and <=. Return large result sets. Use JOIN clauses; typically these are foreign key columns. Use ORDER BY, or GROUP BY clauses. • • • • Are unique or contain many distinct values Are accessed sequentially Defined as IDENTITY because the column is guaranteed to be unique within the table. Used frequently to sort the data retrieved from a table. • • Columns that undergo frequent changes Wide keys ! Consider columns that have one or more of the following attributes: ! ! Clustered indexes are not a good choice for the following attributes: ! 8
  • 9. Useful tips Consider the characteristics of the database when designing nonclustered indexes. ! • Databases or tables with low update requirements, but large volumes of data can benefit from many nonclustered indexes to improve query performance. Consider creating filtered indexes for well-defined subsets of data to improve query performance, reduce index storage costs, and reduce index maintenance costs compared with full-table nonclustered indexes. • Online Transaction Processing applications and databases that contain heavily updated tables should avoid over-indexing. Additionally, indexes should be narrow, that is, with as few columns as possible. ! Consider using a nonclustered index for queries that have the following attributes: ! • Use JOIN or GROUP BY clauses. • Queries that do not return large result sets. • Contain columns frequently involved in search conditions of a query, such as WHERE clause, that return exact matches. ! Consider columns that have one or more of these attributes: ! • Cover the query. • Lots of distinct values, such as a combination of last name and first name, if a clustered index is used for other columns. 9
  • 10. Useful tips Query in which the column predicate is one of these Exact match to a specific value Query description and example Index to consider Searches for an exact match in which the query uses the WHERE Nonclustered or clause to specify a column entry with a specific value. For example: clustered index on the Id column. ! Id, [Login], Email FROM Users WHERE Id = 202 SELECT Searches for an exact match to a value in a specified list of values. Exact match to a value in an For example: IN (x,y,z) list ! Id, [Login], Email FROM Users WHERE Id IN (603, 658, 1371) SELECT Nonclustered or clustered index on the Id column. Searches for a range of values in which the query specifies any entry that has a value between two values. For example: Range of values ! Id, UserId, CreateDate, Content FROM Posts WHERE CreateDate SELECT >= '2013-03-01 12:00' AND CreateDate <= '2013-03-01 12:30‘ ! Or ! Id, UserId, CreateDate, Content FROM Posts WHERE CreateDate SELECT Clustered or nonclustered index on the CreateDate column. BETWEEN '2013-03-01 12:00' AND '2013-03-01 12:30' Join between tables Searches for rows in a table that match a row in another table based Nonclustered or on a join predicate. For example: clustered index on the Id and UserId columns. SELECT u.Id, u.[Login], p.Content ! FROM Users u JOIN Posts p ON u.Id = p.UserId WHERE u.Id = 202 10
  • 11. Useful tips Query in which the column predicate is one of these LIKE comparison Sorted or aggregated PRIMARY KEY or UNIQUE constraint Query description and example Searches for matching rows that start with a specific character string such as 'abc%'. For example: ! Id, [Login], Email FROM Users WHERE [Login] LIKE 'ma%' SELECT Nonclustered or clustered index on the Login column. Nonclustered or Requires an implicit or explicit sort order or an aggregation (GROUP clustered index on the BY). For example: sorted or aggregated column. SELECT p.Id, p.UserId, u.[Login], p.Content For sort columns, FROM Posts p JOIN Users u ON p.UserId = u.Id consider specifying the ORDER BY p.UserId, p.CreateDate ASC or DESC order of Searches for duplicates of new index key values in insert and update Clustered or operations, to enforce PRIMARY KEY and UNIQUE constraints. For nonclustered index on example: the column or columns ! ! Users ([Login], [Password], Email) INSERT VALUES ('xdfyht', 'dh4G57hn1', 'xdfyht@gmail.com') UPDATE or DELETE operation Searches for rows in an update or delete operation in which the in a PRIMARY KEY/FOREIGN column participates in a PRIMARY KEY/FOREIGN KEY relationship, KEY relationship with or without the CASCADE option. Contains one or more columns in the select list that are not used for searching and lookups. For example: Column is in the select list but not in the predicate. Index to consider ! UserId, CreateDate, Content FROM Posts SELECT WHERE UserId = 202 ORDER BY CreateDate DESC defined in the constraint. Nonclustered or clustered index on the foreign key column. Nonclustered index with Content specified in the INCLUDE clause. 11
  • 12. Index options (fill factor) 12
  • 13. Index options (included columns) 13
  • 14. Index options (filtered) Views vs. Filtered Indexes 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 14
  • 19. Spatial indexes (conditions) • • • • • • • • geometry1.STContains(geometry2) = 1 geometry1.STDistance(geometry2) < @r geometry1.STDistance(geometry2) <= @r geometry1.STEquals(geometry2) = 1 geometry1.STIntersects(geometry2) = 1 geometry1.STOverlaps(geometry2) = 1 geometry1.STTouches(geometry2) = 1 geometry1.STWithin(geometry2) = 1 • • • • geography1.STEquals(geography2) = 1 geography1.STDistance(geography2) < @r geography1.STDistance(geography2) <= @r geography1.STIntersects(geography2) = 1 19