Module 06 - Planning Indexes
Module 06 - Planning Indexes
Introduction to Indexes
B Tree
Clustered Index
NonClustered Index
Creating and Dropping Index
Creating Unique Index
Creating Composite Index
Creating Index Options – FillFactor & Pad_Index
Maintaining Indexes
What is an Index
Columns to Index
Primary and foreign keys
Those frequently searched in ranges
Those frequently accessed in sorted order
SQL Server:
11 Determines Whether an Index Exists and Is Useful
22 Navigates Through the Index
33 Evaluates the Search Value Against Each Key Value and
Repeats This Evaluation Until One of Following Occurs:
The search value is not greater than or equal to the
key value
The search value is greater than or equal to the last key
value on the index page
Maintaining Heaps
SQL Server:
Uses Index Allocation Map (IAM) Pages
Contain information on where the extents of a heap
are stored
Navigate through the heap and find available space for
new rows being inserted
Connect data pages
Reclaims Space for New Rows in the Heap When a Row
Is Deleted
B - Tree
1
Root
157
534
Leaf
1 53 104 157 270 410 534
10 65 110 190 310 430 545
20 78 121 210 335 450 557
B - Tree
Leaf Level nodes are the nodes where obtain the leaf
reference to the actual physical data.
Clustered Index
A Clustered index is Unique for any table i.e. can have only
one per table.
The Characteristic of Clustered Index is that Leaf Level
contains the actual Data Pages.
Any new record is inserted according to its actual physical
order in the clustered index.
In case of a new record that needs to be inserted into the
middle of the index structure, a normal Page Split occurs.
Clustered Indexes
Clustered indexes are useful for columns that are searched
frequently for ranges of key values, or are accessed in sorted order.
When you create a clustered index, consider the following facts and
guidelines:
Each table can have only one clustered index.
The physical row order of the table and the order of rows in
the index are the same. You should create clustered
indexes before you create any nonclustered indexes
because a clustered index changes the physical row order
of the table. Rows are sorted into a sequenced order and
maintained in that order.
Key value uniqueness is maintained explicitly, with the
UNIQUE keyword, or implicitly, with an internal unique
identifier. These unique identifiers are internal to SQL
Server and are not accessible to the user.
Clustered Indexes
USE
USE Northwind
Northwind
CREATE
CREATE CLUSTERED
CLUSTERED INDEX
INDEX CL_lastname
CL_lastname
ON
ON employees(lastname)
employees(lastname)
Customers
Customers
CustomerID
CustomerID CompanyName
CompanyName ContactName
ContactName …
…
QUICK
QUICK QUICK-Stop
QUICK-Stop Horst
HorstKloss
Kloss
BONAP
BONAP Bon
Bonapp'
app' Laurence
LaurenceLebihan
Lebihan
12
RANCH
12 Walking
Rancho
Walkinggrande Henry
HenryDavid
Sergio DavidThoreau
Gutiérrez
Thoreau
Duplicate
Duplicatekey
keyvalues
valuesare
arenot
notallowed
allowed
when
whenaanew
newrow
rowis
isadded
addedtotothe
thetable
table
RANCH
RANCH Santé
SantéGourmet
Gourmet Jonas
JonasBergulfsen
Bergulfsen …
…
Unique Index
When you create a unique index, consider the following facts and
guidelines:
SQL Server automatically creates unique indexes on columns in a
table defined with PRIMARY KEY or UNIQUE constraints.
SQL Server checks for duplicate values each time that you use the
INSERT or UPDATE statement. If duplicate key values exist, SQL
Server cancels your statement and returns an error message with
the first duplicate.
Ensure that each row has a unique value—no two rows can have
the same identification number if a unique index is created on that
column. This regulation ensures that each entity is identified
uniquely.
Create unique indexes only on columns in which entity integrity
can be enforced. For example, you would not create a unique index
on the LastName column of the Employees table because some
employees may have the same last names.
can select up to 16 columns
Creating Composite Indexes
USE
USE Northwind
Northwind
CREATE
CREATE UNIQUE
UNIQUE NONCLUSTERED
NONCLUSTERED INDEX
INDEX U_OrdID_ProdID
U_OrdID_ProdID
ON
ON [Order
[Order Details]
Details] (OrderID,
(OrderID, ProductID)
ProductID)
Order
Order Details
Details
OrderID
OrderID ProductID
ProductID UnitPrice
UnitPrice Quantity
Quantity Discount
Discount
10248
10248 11
11 14.000
14.000 12
12 0.0
0.0
10248
10248 42
42 9.800
9.800 10
10 0.0
0.0
10248
10248 72
72 34.800
34.800 55 0.0
0.0
Column 1 Column 2
Composite
CompositeKey
Key
Obtaining Information on Existing Indexes
USE
USE Northwind
Northwind
EXEC
EXEC sp_helpindex
sp_helpindex Customers
Customers
Data Fragmentation
DBCC SHOWCONTIG Statement
DROP_EXISTING Option
Data Fragmentation
Rebuilding an Index
Reorganizes leaf pages
Removes fragmentation
CREATE
CREATE UNIQUE
UNIQUE NONCLUSTERED
NONCLUSTERED INDEX
INDEX U_OrdID_ProdID
U_OrdID_ProdID
ON
ON [Order
[Order Details]
Details] (OrderID,
(OrderID, ProductID)
ProductID)
WITH
WITH DROP_EXISTING,
DROP_EXISTING, FILLFACTOR=65
FILLFACTOR=65
Performance Considerations
a) Storage Engine
b) Relational Engine
c) Open Data Services
d) Server-Net Libraries
Q.2. Which SQL Server service handles alerts?
a) MSSQL Server
b) Server Agent
c) Microsoft Distributed Transaction Coordinator
d) Microsoft Search
Q.3. You have recently created several jobs in order to
automate basic tasks. You discover, however, that the
jobs are not always running properly. In which system
database should you look for job history?
a) Master
b) Msdb
c) Model
d) Distribution
Q.4. You are designing a database. Data integrity is a
prime concern. Which type of database object will you
most likely implement?
Introduction to Indexes
B Tree
Clustered Index
NonClustered Index
Creating and Dropping Index
Creating Unique Index
Creating Composite Index
Creating Index Options – FillFactor & Pad_Index
Maintaining Indexes
NO pain, No Gain
Thank You.