Sei sulla pagina 1di 59

Module 6:

Planning, Creating &


Maintaining Indexes
Vidya Vrat Agarwal. | MCT, MCSD
Overview

 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

Indexes in databases are similar to indexes in books.


In a book, an index allows you to find information
quickly without reading the entire book.
In a database, an index allows the database program
to find data in a table without scanning the entire
table.
An Index is an Internal Table Structure that Database
uses to provide quick access to rows of a table.
 Introduction to Indexes

 How Data Is Stored


 Rows are stored in data pages
 Heaps are a collection of data pages for a table
 How Data Is Accessed
 Scanning all data pages in a table (Table Scan)
 Using an Index that points to data on a page ( Indexes)
Data Pages
Page 4 Page 5 Page 6 Page 7 Page 8 Page 9
Con ... Rudd ... Akhtar ... Smith ... Martin ... Ganio ...
Funk ... White ... Funk ... Ota ... Phua ... Jones ...
White ... Barr ... Smith ... Jones ... Jones ... Hall ...
... ... ... ... Martin ... ... ... Smith ... ... ...
... ... ... ... ... ... ... ... ... ... ... ...
 Use of Table Scan
When a Table Scan is performed SQL Server starts at the
beginning of table looking through every row in the table. As
it finds rows that match the Criteria of Query, it includes
them in the Result Set.
 Use of Indexes
Once the index is chosen, SQL Server navigates the tree
structure to the point of data that matches criteria and
extracts only the records it needs.
During the query optimization process, the optimizer takes a
look at all the available indexes and choose the best one.
Whether to Create Indexes

 Why to Create an Index ( Advantages)


 Speeds up data access
 Enforces uniqueness of rows
 Speeds joins between tables

 Why Not to Create an Index ( Disadvantages)


 Consumes disk space
 Incurs overhead
 Data Modification takes longer as Indexes have to be
updated.
Indexing Guidelines

 Columns to Index
 Primary and foreign keys
 Those frequently searched in ranges
 Those frequently accessed in sorted order

 Columns Not to Index


 Those seldom referenced in queries
 Those that contain few unique values
 Those defined with bit, text, or image data types
How SQL Server Uses Indexes

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

Heaps are a collection of data pages for a table

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

Non- Leaf 1 157 534


53 270 600
104 410 755

Leaf
1 53 104 157 270 410 534
10 65 110 190 310 430 545
20 78 121 210 335 450 557
B - Tree

Balanced Tree or B –Tree attempts to provide a consistent and


relatively low cost method of finding way to a particular
piece of information.
A Tree starts at the Root node. This Root node can point
directly to the actual location of the data (if there is small
amount of data).
Non- Leaf nodes are in somewhere between the root and the
node that tells, where data is physically stored.

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

 The average size of a clustered index is about five percent


of the table size.
However, clustered index size varies depending on the size
of the indexed column.
 When a row is deleted, space is reclaimed and is available
for a new row.
 During index creation, SQL Server temporarily uses disk
space from the current database. A clustered index requires
about 1.2 times the table size for working space when the
index is created. The disk space that is used during index
creation is reclaimed automatically after the index is created.
Using Nonclustered Indexes
The Leaf Level is not the Data Page – instead it is the level to
obtain a pointer to the data.
 Nonclustered Indexes Are the SQL Server Default
 Existing Nonclustered Indexes Are Automatically Rebuilt
When:
 An existing clustered index is dropped
 A clustered index is created
 You can have up to 249 nonclustered indexes per table.
Creating and Dropping Indexes

 Using the CREATE INDEX Statement


 Indexes are created automatically on tables with
PRIMARY KEY or UNIQUE constraints

USE
USE Northwind
Northwind
CREATE
CREATE CLUSTERED
CLUSTERED INDEX
INDEX CL_lastname
CL_lastname
ON
ON employees(lastname)
employees(lastname)

 Using the DROP INDEX Statement


USE
USE Northwind
Northwind
DROP
DROP INDEX
INDEX employees.CL_lastname
employees.CL_lastname
Create Index Statement
 SQL Server automatically creates indexes when a PRIMARY KEY or
UNIQUE constraint is created on a table. Defining a PRIMARY KEY
or UNIQUE constraint is preferred over creating standard indexes.
 Indexes can be created on views.
 SQL Server stores index information in the sysindexes system
table.
 Before you create an index on a column, determine whether
indexes already exist on that column.
 Keep your indexes small by defining them on columns that are
small in size. Typically, smaller indexes are more efficient than
indexes with larger key values.
 Select columns on the basis of uniqueness so that each key value
identifies a small number of rows.
 When you create a clustered index, all existing nonclustered
indexes are rebuilt.
Drop Index Statement
Use the DROP INDEX statement to remove an index on a table. When you
drop an index, consider the following facts:
 SQL Server reclaims disk space that is occupied by the index when you
execute the DROP INDEX statement.
 You cannot use the DROP INDEX statement on indexes that are created by
PRIMARY KEY or UNIQUE constraints. You must drop the constraint in
order to drop these indexes.
 When you drop a table, all indexes for that table are also dropped.
 When you drop a clustered index, all nonclustered indexes on the table
are
rebuilt automatically.
 You must be in the database in which an index resides in order to drop
that
index.
 The DROP INDEX statement cannot be used on system tables.
Creating Unique Indexes
USE
USE Northwind
Northwind
CREATE
CREATE UNIQUE
UNIQUE NONCLUSTERED
NONCLUSTERED INDEX
INDEX U_CustID
U_CustID
ON
ON customers(CustomerID)
customers(CustomerID)

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

 Using the sp_helpindex System Stored Procedure

USE
USE Northwind
Northwind
EXEC
EXEC sp_helpindex
sp_helpindex Customers
Customers

 Using the sp_help tablename System Stored Procedure


 Creating Index Options

 Using the FILLFACTOR Option


 Using the PAD_INDEX Option
Using the FILLFACTOR Option

 Specifies How Much to Fill the Page


 Impacts Leaf-Level Pages
Data Pages Full
Con ... 470401 Akhtar ... 470601 Martin ... 470801
Funk ... 470402 Funk ... 470602 Phua ... 470802
White ... 470403 Smith ... 470603 Jones ... 470803
Rudd ... 470501 Martin ... 470604 Smith ... 470804
White ... 470502 Smith ... 470701 Ganio ... 470901
Barr ... 470503 Ota ... 470702 Jones ... 470902

Fillfactor 50 = Leaf Pages 50% Full


Con ... 470401 Rudd ... 470501 Akhtar ... 470601 Martin ... 470604 Martin ... 470801 Smith ... 470804
Funk ... 470402 White ... 470502 Funk ... 470402 Smith ... 470701 Phua ... 470802 Ganio ... 470901
White ... 470403 Barr ... 470503 Smith ... 470603 Ota ... 470702 Jones ... 470803 White ... 470902
Using the PAD_INDEX Option

 The PAD_INDEX Option Applies to Non-Leaf-Level Index


Pages
 If PAD_INDEX Is Not Specified, the Default Leaves Space
for One Row Entry in Non-Leaf-Level Pages
 Number of Rows on Non-Leaf-Level Pages Is Never Less
Than Two
 PAD_INDEX Uses the Fillfactor Value
USE
USE Northwind
Northwind
CREATE
CREATE INDEX
INDEX OrderID_ind
OrderID_ind
ON
ON Orders(OrderID)
Orders(OrderID)
WITH
WITH PAD_INDEX,
PAD_INDEX, FILLFACTOR=70
FILLFACTOR=70
 Maintaining Indexes

 Data Fragmentation
 DBCC SHOWCONTIG Statement
 DROP_EXISTING Option
Data Fragmentation

 How Fragmentation Occurs


 SQL Server reorganizes index pages when data is modified
 Reorganization causes index pages to split
 Methods of Managing Fragmentation
 Drop and recreate an index and specify a fillfactor value
 Rebuild an index and specify a fillfactor value
 Business Environment
 Data fragmentation can be good for OLTP environment
 Data fragmentation can be bad for Analysis Services
environment
DBCC SHOWCONTIG Statement

 What DBCC SHOWCONTIG Determines


 Whether a table or index is heavily fragmented
 Whether data and index pages are full
 When to Execute
 If tables have been heavily modified
 If tables contain imported data
 If tables seem to cause poor query performance
DROP_EXISTING Option

Use the DROP_EXISTING option to change the


characteristics of an index or to rebuild indexes without
having to drop the index and recreate it.

 Rebuilding an Index
 Reorganizes leaf pages

 Removes fragmentation

 Recalculates index statistics

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

 Create Indexes on Foreign Keys


 Create the Clustered Index Before Nonclustered Indexes
 Consider Creating Composite Indexes
 Create Multiple Indexes for a Table That Is Read
Frequently
 Use the Index Tuning Wizard
Check Your Understanding.
Q.1. What is an Index.? What are the data access
methods.?
Q.2. What are the Advantages of Creating an Index.?
Q.3. What are the Disadvantages of creating an Index.?
Q.4. What are the Columns to Index.?
Q.5. What are the Columns Not to Index.?
Q.6. How SQL Server uses Index.?
Q.7. What is Heap .?
Q.8. What is IAM .?
Q.9. What is B Tree.?
Q.10. What are the types of Indexes.?
Q.11. What is Clustered Index.?
Q.12. What is NonClustered Index.?
Q.13. How many Clustered and NonClustered Index a
Table can have.?
Q.14. How many Columns a Unique and Composite Index
can have.?
Q.15. What is the Mistake done by a database
Programmer in the given SQL Statement .?

CREATE UNIQUE NONCLUSTERED


INDEX U_OrdID_ProdID
ON Order Details (OrderID, ProductID)
Q.16. What is FillFactor.?
Q.17.What is Pad_index.?
Q.18. How can you check that table is Fragmented.?
Q.19. What is the feature of DROP_EXISTING.?
Want to Be an MCP
Q.1. Which server component is responsible for parsing
Transact-SQL statements?

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?

a) User-defined data type


b) View
c) Constraint
d) Non-clustered index
Q.5. Where is the system catalog found?

a) In the master database


b) In every database
c) In the database catalog
d) In system tables
Q.6. You receive a phone call from a junior database
administrator. She is having trouble writing a query to
update values in an identity field. What should you tell
her to do?
a) Use the IDENTITYCOL keyword.
b) Identity fields cannot be updated.
c) Change the values to null and then repopulate the
column.
d) Make a second identity field and populate the
column.
Review

 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.

Potrebbero piacerti anche