0% found this document useful (0 votes)
9 views

SQL XP 07

SQL SERVER presentation

Uploaded by

parimengal039
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

SQL XP 07

SQL SERVER presentation

Uploaded by

parimengal039
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

Implementing Indexes

Objectives
In this lesson, you will learn to:
 Create a clustered index
 Create a nonclustered index
 Use the Index Tuning Wizard
 Understand Index Enhancements
Implementing Indexes

Getting Started
 An index is an internal table structure that SQL Server uses
to provide quick access to rows of a table based on the
values of one or more columns
 Advantages of Using Indexes
 Improve the speed of the execution of queries
 Enforce uniqueness of data
 Speed up joins between tables
Implementing Indexes

Getting Started (Contd.)


 Disadvantages of Using Indexes
 Takes time to create an index
 Takes large amount of disk space to store data along with
the original data source—the table
 Gets updated each time the data is modified
 Types of Indexes
 Clustered index
 Nonclustered index
Implementing Indexes

Getting Started (Contd.)


 Clustered Index
 In a clustered index:
 The data is physically sorted
 Only one clustered index can be created per table
 Nonclustered Index
 In a nonclustered index:
 The physical order of the rows is not the same as the
index order
Implementing Indexes

Getting Started (Contd.)


 Nonclustered indexes are typically created on columns
used in joins and WHERE clauses, and whose values
may be modified frequently
 SQL Server creates nonclustered indexes by default
when the CREATE INDEX command is given
 There can be as many as 249 nonclustered indexes per
table
Implementing Indexes

Getting Started (Contd.)


 Indexes and Heap Structures
 SQL Server supports indexes defined on any column in a
table, including computed columns
 Ifa table does not have any clustered index, data is not
stored in a particular order. This structure is called a
heap
Implementing Indexes

Getting Started (Contd.)


 Features of Indexes
 Indexes accelerate queries that join tables, and perform
sorting and grouping.
 Indexes can be used to enforce uniqueness of rows.
 Indexes are useful on columns in which the majority of
data is unique.
 When you modify the data of an indexed column, the
associated indexes are updated automatically.
 You require time and resources to maintain indexes. You
should not create an index that is not used frequently.
Implementing Indexes

Getting Started (Contd.)


 A clustered index should be created before a
nonclustered index. A clustered index changes the order
of rows. A nonclustered index would need to be rebuilt if it
is built before a clustered index
 Typically, nonclustered indexes are created on foreign
keys.
 Syntax
CREATE [UNIQUE] [CLUSTERED|NONCLUSTERED]
INDEX index_name
ON table_name(column_name[,column_name]…)
Implementing Indexes

7.D.1 Optimizing Query Execution


 The ExternalCandidate table contains a large amount of data.
The first name of each candidate and the name of the
recruitment agency are required to create a report. However,
it takes a long time to execute the following query.
SELECT vFirstName, cName
FROM ExternalCandidate JOIN
RecruitmentAgencies
ON
ExternalCandidate.cAgencyCode =
RecruitmentAgencies.cAgencyCode
Suggest and implement a solution for faster data retrieval.
Implementing Indexes

Task List
 Identify how to speed up data retrieval
 Draft the statement to create an index
 Create the index in the database
 Verify that the index has been created
 Verify that the query execution is faster
Implementing Indexes

Identify how to speed up data retrieval


 Indexes are used to:
 Speed up data retrieval
 Enforce the uniqueness of rows
 Result:
 To speed up data retrieval, use indexes
Implementing Indexes

Draft the statement to create an index


 Action:
 The tables on which the index would be created are:
ExternalCandidate and RecruitmentAgencies
 The attributes on which the index would be created are:
cAgencyCode of ExternalCandidate and cAgencyCode of
RecruitmentAgencies
 The types of indexes to be created are:
ExternalCandidate - Nonclustered index;
RecruitmentAgencies - Clustered index
 The names of the indexes to be created are idxRecruitment
and idxExternalCandidate
Implementing Indexes

Create the index in the database


 Action:
 In the Query Analyzer window, type:
CREATE NONCLUSTERED INDEX
idxExternalCandidate
ON ExternalCandidate(cAgencyCode)
CREATE CLUSTERED INDEX idxRecruitment
ON RecruitmentAgencies(cAgencyCode)
 Press F5 to execute the code
Implementing Indexes

Verify that the index has been created


 To verify that the index has been created, use the sp_helpindex
command
 Syntax
sp_helpindex table_name
 Action:
 In the Query Analyzer window, type:
sp_helpindex ExternalCandidate
 Press F5 to execute the command
 In the Query Analyzer window, type:
sp_helpindex RecruitmentAgencies
 Press F5 to execute the command
Implementing Indexes

Verify that the query execution is faster


 Action:
 Execute the query after creating the index. If there is a lot of
data, you can note the difference in speed
Implementing Indexes

Just a Minute…
 How many clustered indexes can be created per table?
 Which index organizes data logically but does not store data
physically?
Implementing Indexes

Index Tuning Wizard


 Index Tuning Wizard available in SQL Server is used to select
and create the best possible set of indexes and information
regarding a database
 Uses of the Index Tuning Wizard
 For a given workload, the best possible combination of
indexes for a database is recommended
 The effects of the proposed recommendation about the
indexes, distribution of queries among tables, and the query
performance in the workload will be analyzed
 For a small set of problem queries, the way to tune the
database will be recommended
 It will specify the advanced options such as disk space
constraints that can be customized
Implementing Indexes

7.D.2 Using the Index Tuning Wizard


 You have created the following indexes on the Department
table:
CREATE NONCLUSTERED INDEX idxdepcode
ON DEPARTMENT(cDepartmentCode)
CREATE NONCLUSTERED INDEX idxdepname
ON DEPARTMENT(vDepartmentName)
CREATE NONCLUSTERED INDEX idxdephead
ON DEPARTMENT(vDepartmentHead)
CREATE NONCLUSTERED INDEX idxdeplocation
ON DEPARTMENT(vlocation)
Implementing Indexes

7.D.2 Using the Index Tuning Wizard (Contd.)


 Analyze the use of these indexes on the Department table
when the following query is executed:
SELECT Requisition.cRequisitionCode,
Position.vDescription,vDepartmentName
FROM Position
JOIN Requisition
ON Requisition.cPositionCode=
Position.cPositionCode
JOIN Department
ON Requisition.cDepartmentCode=
Department.cDepartmentCode
Implementing Indexes

Task List
 Identify a method to analyze the use of indexes
 Perform the analysis
 Verify that the analysis has been performed
Implementing Indexes

Identify a method to analyze the use of indexes


 Result:
 Use the Index Tuning Wizard to analyze the use of
indexes
Implementing Indexes

Perform the analysis


 Action:
 Perform the analysis using the Index Tuning Wizard
Implementing Indexes

Verify that the analysis has been performed


 Action:
 You can use the following command to view the indexes
present on the Department table:
sp_helpindex Department
 Observe that, as recommended by the Index Tuning Wizard,
the extra indexes have been removed
Implementing Indexes

Index Enhancements
 Fill Factor
 FILLFACTOR clause improves performance of the system
by minimizing the amount of page splitting that occurs each
time an index page becomes full
 Syntax
CREATE CLUSTERED INDEX index_name
ON table_name (column_name)
WITH FILLFACTOR =
percentage_fillfactor
Implementing Indexes

Index Enhancements (Contd.)


 Implications of NULL in Unique Indexes:
 In a table, a unique index cannot be created on a single
column if that column contains NULL in more than one row
 DBCC SHOWCONTIG:
 The DBCC SHOWCONTIG command is primarily used to
find out why the table or the index is heavily fragmented
 Syntax
DBCC SHOWCONTIG
[
(table_id [, index_id])
]
Implementing Indexes

Index Enhancements (Contd.)


 The DBCC INDEXDEFRAG:
 The DBCC INDEXDEFRAG command is used to
defragment clustered and secondary indexes of the
specified table or view
 Syntax
DBCC INDEXDEFRAG
( { database_name | database_id | 0 }
, { table_name | table_id | 'view_name' | view_id }
, { index_name | index_id }
)
Implementing Indexes

Just a Minute…
 Susan wants to minimize the amount of page splitting that
occurs each time an index page is full. What should she use?
Implementing Indexes

Index Enhancements (Contd.)


 Index Selection:
 A detailed query analysis is required to determine which
index to involve in a query process. This involves:
 Examining the search clauses to identify the columns
referenced
 Knowing the importance of the data to determine the
usefulness of the index
 Ranking the queries in the order of importance
Implementing Indexes

Performance Considerations (Contd.)


 Index Usage Criteria:
 SQL Server cannot use an index until and unless the query
contains a column in a valid search argument or join clause
that matches at least the first column of the index
Implementing Indexes

Summary
In this lesson, you learned that:
 Indexes are created to enhance the performance of queries.
 There are two types of indexes – clustered and nonclustered.
 Indexes are created using the CREATE INDEX statement.
 Data is physically sorted in a clustered index.
 Clustered indexes should be built on an attribute whose values
are unique and do not change often.
 In a nonclustered index, the physical order of rows is not the
same as that of the index order.
Implementing Indexes

Summary (Contd.)
 A nonclustered index should be built on an attribute which is
normally used in joins and the WHERE clause. The values of
this attribute may often change.
 A nonclustered index is the default index that is created with
the CREATE INDEX command.
 The Index Tuning Wizard can be used to analyze the optimal
use of indexes in the query entered in the Query Analyzer
window.
 SQL Server provides the FILLFACTOR clause to improve
performance of the system by minimizing the amount of page
splitting that occurs each time an index page becomes full.
Implementing Indexes

Summary (Contd.)
 The DBCC SHOWCONTIG command is mainly used to find
out whether the table or index is heavily fragmented. Table
fragmentation normally occurs when a large number of insert
and update operations are performed on the table.
 The DBCC INDEXDEFRAG command is used to defragment
clustered and secondary indexes of the specified table or view.

You might also like