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

L45 SQL-Developer-Lecture-24

Indexes in databases are mechanisms that enhance data retrieval speed and enforce constraints, similar to a book index. They allow for efficient searches, sorting, and grouping of records, significantly improving query performance while also having some drawbacks such as increased disk space usage and maintenance overhead during data modifications. Different types of indexes, including clustered and composite indexes, can be created to optimize specific queries and data access patterns.

Uploaded by

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

L45 SQL-Developer-Lecture-24

Indexes in databases are mechanisms that enhance data retrieval speed and enforce constraints, similar to a book index. They allow for efficient searches, sorting, and grouping of records, significantly improving query performance while also having some drawbacks such as increased disk space usage and maintenance overhead during data modifications. Different types of indexes, including clustered and composite indexes, can be created to optimize specific queries and data access patterns.

Uploaded by

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

Indexes

Intellipaat Software Solutions Pvt. Ltd.


An INDEX can be defined as a mechanism for providing fast access to
table rows and for enforcing constraints

When data volumes increase, organizations face the problems related


to data retrieval and posting. They feel the need for a mechanism
that will increase the speed of data access.

An index, like the index of a book, enables the database retrieve and
present data to the end user with ease

When a SQL Server has NO index to use for searching, the result is
similar to the reader who looks at every page in a book to find a
word: the SQL engine needs to visit every row in a table. In
database terminology we call this behavior a TABLE SCAN, or just
SCAN.
With No Indexes Defined …
Consider the following query on the Products table (with NO index of the Northwind database.
This query retrieves products in a specific price range ( 12.5 – 14).

Intellipaat Software Solutions Pvt. Ltd.


SELECT ProductID, ProductName, UnitPrice FROM Products WHERE (UnitPrice > 12.5)
AND (UnitPrice < 14)

In the diagram below, the database search touches a total of 77 records to find just three matches.
Now imagine if we CREATED an index, just like a book index, on the data
in the UnitPrice column.

Intellipaat Software Solutions Pvt. Ltd.


 Each index entry would contain a copy of the UnitPrice value for a
row, and a reference (just like a page number) to the row where the
value originated.

 SQL will sort these index entries into ascending order.

 The index will allow the database to quickly narrow in on the three
rows to satisfy the query, and avoid scanning every row in the table.
Creating Indexes

The command specifies

Intellipaat Software Solutions Pvt. Ltd.


the name of the index (IDX_UnitPrice),
the table name (Products),
and the column to index (UnitPrice).

CREATE INDEX [IDX_UnitPrice] ON Products (UnitPrice)

To verify that the index is created, use the following stored procedure to see a list
of all indexes on the Products table:
EXEC sp_helpindex Products
How It Works
The database takes the columns specified in a CREATE INDEX command and sorts

Intellipaat Software Solutions Pvt. Ltd.


the values into a special data structure known as a B-tree.
On the left, each index entry contains the index key (UnitPrice).

Intellipaat Software Solutions Pvt. Ltd.


Each entry also includes a reference (which points) to the table rows which
share that particular value and from which we can retrieve the required
information

Much like the index in the back of a book helps us to find keywords quickly,
so the database is able to quickly narrow the number of records it must
examine to a minimum by using the sorted list of UnitPrice values stored in
the index.
We have avoided a table scan to fetch the query results
Index Advantages

Intellipaat Software Solutions Pvt. Ltd.


The database engine can use indexes to boost performance in a number
of different queries.

An important feature of versions SQL Server 2005 and above is a


component known as the query optimizer.

The query optimizer's job is to find the fastest and least resource
intensive means of executing incoming queries.

An important part of this job is selecting the best index or indexes to


perform the task.
Searching For Records

The most obvious use for an index is in finding a record or set of records matching a
WHERE clause.

Intellipaat Software Solutions Pvt. Ltd.


Indexes can aid queries looking for values inside of a range (as we demonstrated earlier),
as well as queries looking for a specific value.

By way of example, the following queries can all benefit from an index on UnitPrice:

DELETE FROM Products WHERE UnitPrice = 1


UPDATE Products SET Discontinued = 1 WHERE UnitPrice > 15
SELECT * FROM PRODUCTS WHERE UnitPrice BETWEEN 14 AND 16

Indexes work just as well when searching for a record in DELETE and UPDATE
commands as they do for SELECT statements.
Sorting Records
When we ask for a sorted dataset, the database will try to find an index and avoid sorting the
results during execution of the query.

Intellipaat Software Solutions Pvt. Ltd.


We control sorting of a dataset by specifying a field, or fields, in an ORDER BY clause, with
the sort order as ASC (ascending) or DESC (descending).

For example, the following query returns all products sorted by price:
SELECT * FROM Products ORDER BY UnitPrice ASC

With no index, the database will scan the Products table and sort the rows to process the
query. However, the index we created on UnitPrice (IDX_UnitPrice) earlier provides the
database with a presorted list of prices.

The database can simply scan the index from the first entry to the last entry and retrieve the
rows in sorted order.
The same index works equally well with the following query, simply by scanning the index in
reverse.

SELECT * FROM Products ORDER BY UnitPrice DESC


Grouping Records
We can use a GROUP BY clause to group records and aggregate values, for
example, counting the number of orders placed by a customer.

Intellipaat Software Solutions Pvt. Ltd.


To process a query with a GROUP BY clause, the database will often sort the
results on the columns included in the GROUP BY.

The following query counts the number of products at each price by grouping
together records with the same UnitPrice value.

SELECT Count(*), UnitPrice FROM Products GROUP BY UnitPrice


The database can use the IDX_UnitPrice index to retrieve the prices in order.

 Since matching prices appear in consecutive index entries, the database is


able count the number of products at each price quickly.
Index Drawbacks
Disk Space

Intellipaat Software Solutions Pvt. Ltd.


Indexes are stored on the disk, and the amount of space required will depend on the size
of the table, and the number and types of columns used in the index.

To see the space required for a table, use the sp_spaceused system stored procedure
in a query window.
EXEC sp_spaceused Orders
Given a table name (Orders), the procedure will return the amount of space used by
the data and all indexes associated with the table, like so:
Name rows reserved data index_size unused
------- -------- ----------- ------ ---------- -------
Orders 830 504 KB 160 KB 320 KB 24 KB
Indexes and Data Modification

Any time a query modifies the data in a table (INSERT, UPDATE, or DELETE), the database needs

Intellipaat Software Solutions Pvt. Ltd.


to update all of the indexes where data has changed.
Indexing can help the database during data modification statements by allowing the database to
quickly locate the records to modify, however, we now caveat the discussion with the
understanding that providing too many indexes to update can actually hurt the performance of
data modifications.

In decision support systems and data warehouses, where information is stored for reporting
purposes, data remains relatively static and report generating queries outnumber data
modification queries. In these types of environments, heavy indexing is commonplace in
order to optimize the reports generated.
In contrast, a database used for transaction processing will see many records added and
updated. These types of databases will use fewer indexes to allow for higher throughput on
inserts and updates.
Clustered Indexes
A common analogy for a clustered index is a phone book.

Intellipaat Software Solutions Pvt. Ltd.


A phone book still sorts entries into alphabetical order.
The difference is, once we find a name in a phone book, we have immediate access to
the rest of the data for the name, such as the phone number and address.
The nonclustered indexes created earlier in the chapter contain only the index key
and a reference to find the data, which is more like a book index. You can only
create one clustered index on each table.
If the database engine can use a clustered index during a query, the database does not
need to follow references back to the rest of\ the data, as happens with a
nonclustered index. The result is less work for the database, and consequently,
better performance for a query using a clustered index.
Intellipaat Software Solutions Pvt. Ltd.
Notice the rows are sorted into the order of the index, there is
no reference to follow from the index back to the data.
To create a clustered index, simply select the Create As CLUSTERED dropdown in
the dialog box we used at the beginning.

Intellipaat Software Solutions Pvt. Ltd.


The SQL syntax for a clustered index simply adds a new keyword to the CREATE
INDEX command, as shown below:

CREATE CLUSTERED INDEX IDX_SupplierID ON testTraining(SupplierID)

Since we can only have one clustered index per table, and the testTraining table
already has a clustered index (PK_Products) on the primary key (ProdId), the above
command should generate the following error:

Cannot create more than one clustered index on table ‘testTraining'. Drop the existing
clustered index 'PK_Products' before creating another.
Sometimes it is better to use a unique nonclustered index on the primary key column,

Intellipaat Software Solutions Pvt. Ltd.


and place the clustered index on a column used by more queries.

For example, if the majority of searches are for the price of a product instead of the
primary key of a product, the clustered index could be more effective if used on the
price field.

A clustered index can also be a UNIQUE index.


Composite Indexes

Intellipaat Software Solutions Pvt. Ltd.


A composite index is an index on two or more columns.

Both clustered and nonclustered indexes can be composite indexes.

Composite indexes are especially useful in two different circumstances.

First, you can use a composite index to cover a query.

Secondly, you can use a composite index to help match the search criteria of
specific queries. We will go onto more detail and give examples of these two
areas in the following sections.
Covering Queries with an Index
Consider the index we created on the Products table for UnitPrice.

Intellipaat Software Solutions Pvt. Ltd.


The database copied the values from the UnitPrice column and sorted them into an index. If we
execute the following query, the database can retrieve all of the information for the query from
the index itself.

SELECT UnitPrice FROM Products ORDER BY UnitPrice

We call these types of queries covered queries, because all of the columns requested in the output are
contained in the index itself.

A clustered index, if selected for use by the query optimizer, always covers a query, since it contains
all of the data in a table.

For the following query, there are no covering indexes on the Products table.

SELECT ProductName, UnitPrice FROM Products ORDER BY UnitPrice

Database will use the index on UnitPrice to avoid sorting records, it will need to follow the
reference in each index entry to find the associated row and retrieve the product name. By
creating a composite index on two columns (ProductName and UnitPrice), we can cover
this query with the new index.
Additional Index Guidelines
Keep Index Keys Short

Intellipaat Software Solutions Pvt. Ltd.


The larger an index key is, the harder a database has to work to use
the index. For instance, an integer key is smaller in size then a
character field for holding 100 characters. In particular, keep
clustered indexes as short as possible.

Distinct Index Keys


The most effective indexes are the indexes with a small percentage of
duplicated values. A good index will allow the database to
disregard as many records as possible during a search.
An index with a high percentage of unique values is a selective index.
Obviously, a unique index is the most selective index of all, because
there are no duplicate values.

You might also like