You are on page 1of 24

Indices and Views

TCS Internal

To be able to learn the implementation of Indexes

and Views.

TCS Internal

 Introduction to Indexes
 Implementing Indexes
 Creating and dropping Indexes
 Creating Indexes with Enterprise Manager
 Creating Indexes with the Index Tuning Wizard
 Implementation of Views
 Dropping Views
 Displaying Information
 Modifying data through views

TCS Internal
Introduction to Indexes

 Indexes are SQL Server’s internal method of organizing the

data in a table in such a way, that the data can be retrieved

 Indexes are another kind of objects, similar to tables, that

are stored in the databases.

 An index is a collection of data values of a column and their

corresponding pointers to the rows where those values are
physically represented in the table.

 Indexes require pages of data to store their rows, like


TCS Internal
Types of Indexes

 There are two types of indexes

(a) Clustered indexes

(b) Nonclustered indexes.

TCS Internal
Clustered Index

 The file being indexed is stored in data pages with several

records in each page.

 After the creation of a clustered index records are in the order

of the index.

 Actual data pages are a part of the index.

 The physical order


 Removes the need for sequential scanning of the indexed file.

 UPDATE and DELETE operations are accelerated since these

operations require much reading.

 Clustered index are used for Queries that return large result
TCS Internal
Clustered Index Guidelines

 There can be only one clustered index per


 A clustered index should always be the first

index to be created.

 If CLUSTERED is not specified, a non clustered

index would be created, which is the default.

 Space required to create an index comes from

the table’s database.

TCS Internal
Non-Clustered Index

 Physical ordering of the rows is not same as their indexed


 Non-clustered index specifies the logical ordering of the


 Leaf level pages of a non-clustered index are index pages.

 These actual data pages are unordered.

 Conceptually, there is a level of indirection between the

index structure and the data itself.

TCS Internal
Clustered Vs Non clustered indexes

 Number of non clustered index permissible on one

table is 249.

 Clustered indexes should be created before the non

clustered indexes, because clustered indexes change
the physical order of the rows, and non clustered
indexed would have to be rebuilt.

 Queries that return a small or single row result sets.

TCS Internal
Non-Clustered Index Guidelines

TCS Internal
Implementing indexes

 Reasons for using Indexes :

 Enforces the uniqueness of rows.

 Speeds the execution of SQL statements with search
conditions, that refer to the indexed column.
 Reduces the number of I/Os required by the SQL Server
query optimizer, to reach any given piece of data in a table.
 In certain cases, front-end application might require an

 Indexing guidelines :

 Indexes is appropriate for columns that are used frequently

in search conditions.
 Indexes should be made for the tables for which queries are
more frequent than inserts and updates.
 Primary keys should be indexed uniquely.
 Indexing on foreign keys help process joins more efficiently.
 A column that is often accessed in sorted order should be
given a clustered index.
TCS Internal
Implementing indexes

 Good Indexing Candidates :

 Foreign Keys
 Large result set queries
 Order by and group support

 Optimizing Indexes based on Usage Patterns :

 In a system where lots of inserts and updations are expected, the

indexes on the table must be limited.
 In a query-centric system, the indexes are needed to improve the
query performance.

 Reasons for not indexing every column:

 Building an index takes time.

 Disk space is required for each index in addition to the space used by
the table.
 When updating, inserting, or deleting rows the index is also dynamically
maintained to reflect any changes

TCS Internal
Creating and Dropping Indexes

 SQL Server automatically creates an index for the PRIMARY

KEY and UNIQUE constraints.

 The other indexes have to be created explicitly using the

CREATE INDEX statement.

TCS Internal
Index Rules

 Only the owner of the table can CREATE or DROP the index.

 Indexes can be created on tables in another database by

qualifying the database.table_name.

 Indexes speed data retrieval but can slow data update.

 Index cannot be created on a view.

 Index cannot be created on columns defined with bit, text

and image data types.

TCS Internal
Unique Indexes



ON applicant ( appl_id)


Creates an index called app_ind on the appl_id column of the

applicant table that enforces uniqueness.

TCS Internal
Clustered Indexes

 If the option is specified, the index will be created as a

clustered index.


CREATE UNIQUE clustered INDEX app_clind

ON applicant (appl_id)


Creates an index called appl_clind on the appl_id

column of the applicant table that enforces

TCS Internal
Non Clustered Indexes

 Specifies that a non-clustered index is to be created.

 If no index type is specified, a non-clustered index is


CREATE nonclustered INDEX app_nonind

ON applicant (fname)


Creates a noclustered index called appl_nonind on the

fname column of the applicant table .

TCS Internal
Information about the indexes

 SQL Server has two ways to show information about indexes


 The graphical method via SQL server management


 The command-line method via the system stored

procedure sp_helpindex.


sp_helpindex table_name

TCS Internal
Dropping indexes

 When an index is no longer needed, it can be removed from

a database.

 Only the owners of the table can drop the index.

 Doesn’t apply to indexes created by PRIMARY KEY or

UNIQUE constraints.


DROP INDEX [owner.] table_name.index_name

[, [owner.]table_name.index_name...] DROP INDEX

TCS Internal
Implementation of views

Views :

 A view is an alternate way of looking at data derived from

one or more tables in the database.
 The user is allowed to see only selected pieces of information
in the database.

Advantages of view :

 Allow users to focus on the data of their interest only.

 Manipulation of the data is simplified by defining frequently
used queries as views.
 It provides security by allowing users to query and modify
the data only which they see; rest of the data is neither
visible nor accessible.
 With views data can be exported to other applications using
bcp utility.

TCS Internal
Creation of views

 Views can be created by using SQL server management

Studio or by Transact-SQL.

 A view is stored as a separate object in the database.


CREATE VIEW [owner.] view_name

[(column_name [, column_name…])]
AS select_statement [WITH CHECK OPTION]

TCS Internal
Creation of views

With Check Option :

 The WITH CHECK OPTION forces all data modification
statements executed against the view to adhere to the
criteria set within the SELECT statement defining the view.
 By default, data modification statements on view are not
checked .
 When this option is set, on row modification through a view
guarantees, that data will remain visible through the view
after the modification has been committed.
With Encryption Option:
 This option encrypts the syscomments entries that contain
the text of the CREATE VIEW statement.
 Querying the syscomments table or using sp_helptext will
not allow the user to see the view definition.
 To unencrypt, the view should be dropped and recreated.

CREATE VIEW twocolumns AS
SELECT appl_no, last_name FROM applicant

TCS Internal
Dropping views

 When a view is dropped, the definition of the view is deleted.

 The DROP VIEW statement removes a view from the



DROP VIEW [owner.] view_name [, [owner.]


TCS Internal

 An index is a collection of data values of a column in a table

and their corresponding pointers to the rows where those
values are physically represented in the table.
 There are two types of indexes:
 Clustered indexes - Actual data pages are a part of
index. Physical order of the rows is same.
 b) Nonclustered indexes - Actual data pages are
unordered. Index is created using CREATE INDEX
 A view is created by using CREATE VIEW statement.

TCS Internal