0% found this document useful (0 votes)
3 views18 pages

SQL_8

The document provides an overview of implementing indexes in MS SQL Server, detailing the types of indexes including clustered, non-clustered, and unique indexes. It explains the advantages and disadvantages of indexed views, how clustered indexes organize data in a B-tree structure, and the role of non-clustered indexes in data retrieval. Additionally, it highlights good candidates for indexing to enhance query performance.

Uploaded by

nidhit102646
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
3 views18 pages

SQL_8

The document provides an overview of implementing indexes in MS SQL Server, detailing the types of indexes including clustered, non-clustered, and unique indexes. It explains the advantages and disadvantages of indexed views, how clustered indexes organize data in a B-tree structure, and the role of non-clustered indexes in data retrieval. Additionally, it highlights good candidates for indexing to enhance query performance.

Uploaded by

nidhit102646
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

MS SQL SERVER

SESSION # : 8
IMPLEMENTING
INDEXES
Focus Points :

Indexes
Clustered Indexes
Non clustered indexes
Contents

 Indexes
 Clustered indexes
 Unique indexes
 Non Clustered indexes
Indexes

 Indexes provide a set of logical pointers to your data, much


like an index in the back of a book helps you find things
you're looking for.
 Without indexes, SQL Server accesses data by reading every
page of data on each table you have specified in your SQL
statement.
 Another reason to create an index is to enforce uniqueness.
Having two identical rows in a table is not an error
condition.
 The index provides a fast way to look up data based on the
values within those columns.
Indexes

 Advantages of indexed views


 To Improve the performance of select queries
 Index data on columns on multiple tables
 The data from multiple tables is denormalized and helps in fastening the
queries that fetch data
 Avoid Complex joins at run time and make the joined data readyly available
 Joins that are frequently used can be converted into indexed views and thus
reducing the query resopnse time of multiple queries
 Advantage of having Higher Disk Space can be converted into having high
query performance using Indexed views
 Disadvantages of indexed views
 Increases the disc space used by the database as the views are created with
physical data
 Slows down the performance of the insert,update,delete statements on the
tables used in indexed views.
Clustered Index

 Clustered indexes sort and store the data rows in the table
or view based on their key values. These are the columns
included in the index definition.
 There can be only one clustered index per table,
because the data rows themselves can be sorted in only one
order.
 The only time the data rows in a table are stored in sorted
order is when the table contains a clustered index.
 When a table has a clustered index, the table is called a
clustered table. If a table has no clustered index, its data
rows are stored in an unordered structure called a heap.
How clustered indexes work

 All table data is stored in 8 KB data pages. When a table


contains a clustered index, the clustered index tells SQL
Server how to order the table's data pages. It does this by
organizing those data pages into a B-tree structure
How clustered indexes work
How clustered indexes work
 To compare the B-tree to an actual tree. You can visualize the root node as
the trunk of a tree, the intermediate levels as the branches of a tree, and the
leaf level as the actual leaves on a tree.
 The leaf level of the B-tree is always level 0, and the root level is always the
highest level. Figure 1 shows only one intermediate level but the number of
intermediate levels actually depends on the size of the table. A large index
will often have more than one intermediate level, and a small index might
not have an intermediate level at all.
 Index pages in the root and intermediate levels contain the clustering key
and a page pointer down into the next level of the B-tree. This pattern will
repeat until the leaf node is reached. You'll often hear the terms "leaf node"
and "data page" used interchangeably, as the leaf node of a clustered index
contains the data pages belonging to the table. In other words, the leaf level
of a clustered index is where the actual data is stored, in an ordered fashion
based on the clustering key.
 Let's look at the B-tree again. Figure 2 represents the clustered index
structure for a fictional table with 1 million records and a clustering key
on EmployeeID.
How clustered indexes work
Clustered indexes

 Primary Key can be Clustered or Non-clustered but it is a


common best practice to create a Primary Key as
Clustered Index.

Primary key [clustered] [nonclustered]


Or
Unique [clustered]

CREATE TABLE dbo.Table_1


( Id int NOT NULL IDENTITY (1, 1) PRIMARY KEY
NONCLUSTERED,
iContact int NOT NULL CONSTRAINT uk-icontact
UNIQUE CLUSTERED )
Non Clustered Index

 Nonclustered indexes have a structure separate from the data


rows. A nonclustered index contains the nonclustered index
key values and each key value entry has a pointer to the data
row that contains the key value.
 The pointer from an index row in a nonclustered index to a
data row is called a row locator. The structure of the row
locator depends on whether the data pages are stored in a
heap or a clustered table. For a heap, a row locator is a pointer
to the row. For a clustered table, the row locator is the
clustered index key.
 You can add nonkey columns to the leaf level of the
nonclustered index to by-pass existing index key limits, 900
bytes and 16 key columns, and execute fully covered, indexed,
queries.
How Non Clustered Index work
Create Non Clustered Index

Create [unique] index index_name on tablename


(
Columnname [asc][desc]
)
Unique Index

 Creates a unique index on a table or view. A unique


index is one in which no two rows are permitted to
have the same index key value.
 A clustered index on a view must be unique.
 The Database Engine does not allow creating a
unique index on columns that already include
duplicate values.
 Columns that are used in a unique index should be
set to NOT NULL, because multiple null values are
considered duplicates when a unique index is
created.
Good candidates for indexes include

 Primary key columns


 Foreign key columns
 Columns on which you use the ORDER BY clause
 Columns on which you use the GROUP BY clause
Columns that you specify exactly in
your WHERE clause
Examples
Summary

 Indexes
 Clustered indexes
 Unique indexes
 Non Clustered indexes

You might also like