1. Home
SQL

SQL Tutorial: Learn Structured Query Language Basics

Learn all SQL tutorial concepts in detail and master your career today.

  • 59
  • 9 Hours
right-top-arrow

Tutorial Playlist

46 Lessons
21

Index in SQL

Updated on 11/06/202473 Views

Introduction

Indexes play an important role in SQL (Structured Query Language). Index in SQL helps in making queries lightning-fast and efficient. There is a world of difference between the databases in the early days of computing and the ones we have today. Databases used to be slow, clunky, and contain moderate volumes of data. The advent of indexing has wholly changed how databases function.

Nowadays, indexing is an integral part of database management systems such as SQL. One must consider indexes before one can think about querying databases. They not only speed up query execution but also simplify data retrieval, thus making your interaction with the database smooth.

This guide takes an in-depth look at SQL indexing. We will cover how to create indexes in SQL, the different types of indexes, and how they can be created and used effectively. Finally, we will discuss best practices for indexing in SQL.

Overview

Indexes in SQL act as pointers to the data, thus making it faster for you to retrieve data from tables and improving query performance. Efficient database operations depend on them, making it easy for you to manage.

You can choose among different types of indexes in SQL, such as non-clustered, clustered, composite, unique, hash, and covering indexes.

Whether you want to speed up queries, retrieve data quicker, or boost database responsiveness, using indexes right is crucial for top-notch results in your SQL database.

What is Index in SQL and Why Should You Use Indexes in SQL?

As discussed above, indexing is a potent tool in SQL that makes data retrieval operations efficient. 

An index is simply a data structure accompanying the table, which helps you locate particular rows quickly, given one or more columns’ value(s). As if in an orderly library catalog, it saves one from sifting through all records of the table.

Here is the internal working of indexes in SQL;

Data Structure: SQL databases use different kinds of data structures to implement indexes like B-trees, hash tables, or bitmap indexes, depending on the database management system (DBMS) and the type of index.

Index Creation: Upon index creation on columns in a table, the DBMS maintains and builds an index data structure together with table data. This index structure orders the sorted values of column indexes (in case they are B-trees), or it maps them to their respective rows (in case they are hash tables or bitmap indexes).

Fast Lookup: While searching for a particular book title, for instance, a DBMS could navigate through this index quite efficiently to find precisely where this title is within its structure. If you run a query that entails any indexed column(s), then you can expect the DBMS optimizer to use this index to locate all relevant rows quickly.

Reduce Disk I/O: Indexes lower the number of disk I/O operations needed to locate particular rows by offering a direct route to the data sought. Rather than scanning an entire table, the DBMS can traverse the index structure, requiring fewer disk reads and improving query performance.

Sequential Access: In sorted indexes like B-trees, the index structure promotes efficient sequential access to data. This is useful for range queries or sorting operations since DBMS can predictably traverse order in the index and retrieve contiguous data blocks from disk.

Now let’s imagine a situation where a table named Books contains information about thousands of books in your database. Each book entry has certain attributes such as title, author, genre, publication_year, etc.

Assume that we have an index on the title column. When you run a query that fetches details for a book with the name “The Great Gatsby,” the DBMS optimizer uses this index to quickly find “The Great Gatsby” within this index structure; consequently, it accesses the corresponding row from the table using pointer kept in index leading to fast and efficient retrieval of data.

So why should you use an index in SQL?

1. Boosting the Performance of Data Retrieval Operations

Let’s say there is no index within your vast customer database. In order to get a specific customer’s record, the database engine has to go through every row sequentially, which is inefficient and consumes large amounts of resources for such tables. 

However, creating an index on any key column like “customer_id” greatly improves search performance. The index organizes values for “customer_id”, thus enabling the database engine to quickly find that required record. This saves time and resources which would have been used to access client information.

2. Improving Other Database Activities

Furthermore, the use of indexes in SQL can increase the efficiency of other DBMS processes, including sorting as well as joining tables.

3. Enforcing Data Integrity

Indexes also play a vital role in enforcing data integrity by preventing the insertion of duplicate values in the indexed columns. Primary keys are implemented with unique indexes, guaranteeing that each row has a distinct identification.

4. Minimize I/O Operations

Efficient use of indexes can also minimize the number of I/O operations required to satisfy a query. This is accomplished by reducing the search space, reading fewer data pages, and improving disc access.

Creating an Index in SQL

1. The Index Syntax in SQL:

Creating an index in SQL involves using the CREATE INDEX statement followed by specifying the name of the index, the table on which the index is created, and the column(s) to be indexed. The basic syntax for creating an index in SQL is as follows:

CREATE INDEX index_name

ON table_name (column1, column2, ...);

Let's say we have a table named "employees" with columns "employee_id" and "last_name". To create an index on the "last_name" column, you would use the following SQL statement:

CREATE INDEX  index_of_last_name

ON employees (last_name);

This creates a non-clustered index named "index_of_last_name" on the "last_name" column of the "employees" table.

2. Parameters for Index Creation in SQL:

When creating an index in SQL, you may encounter various parameters that allow you to customize the index creation process. Some common parameters include:

(i) UNIQUE: This specifies that the values in the indexed column(s) must be unique across all rows in the table. For example;

CREATE UNIQUE INDEX Index_name ON TableName(Column_name);

(ii) CLUSTERED/NONCLUSTERED: This specifies the type of index to be created. A clustered index determines the physical order of rows in the table, while a non-clustered index does not.

For Clustered

CREATE CLUSTERED INDEX Index_name ON TableName(Column_name);

For Non clustered

CREATE NONCLUSTERED INDEX Index_name ON TableName(Column_name);

(iii) INCLUDE: Specifies additional non-key columns to be included in the index, which can improve query performance by covering more queries. 

Consider a table named "products" with columns "product_id", "product_name", and "description".

product_id

product_name

description

1

Laptop

High-performance laptop

2

Smartphone

Latest model smartphone

3

Headphones

Noise-canceling headphones

4

Tablet

Lightweight and portable tablet

 Use this SQL command to create a non-clustered index on the "product_name" column and include the "description" column to cover more queries.

CREATE NONCLUSTERED INDEX idx_product_name

ON products (product_name)

INCLUDE (description);

(iv) WHERE: This specifies a filter condition to create a filtered index that includes only rows that satisfy the specified condition.

Suppose we have a table named "invoices" with a column "status" indicating the status of each invoice (e.g., "paid", "pending"). Use this SQL command to create a filtered index that includes only "paid" invoices;

CREATE INDEX idx_paid_invoices

ON invoices (invoice_id)

WHERE status = 'paid';

(v) Fill Factor: Let's say we have a table named "orders" with columns "order_id" and "order_date". To

CREATE INDEX idx_order_date

ON orders (order_date)

WITH FILLFACTOR = 80;

3. How to View Index in SQL

To view existing indexes in SQL, you can query the system catalog views provided by your database management system. For example, in SQL Server, you can use the following query to view all indexes associated with a specific table:

SELECT *

FROM sys.indexes

WHERE object_id = OBJECT_ID('table_name');

 

SELECT *: Retrieves all columns.

FROM sys.indexes: Queries the system catalog view containing index metadata.

WHERE object_id = OBJECT_ID('table_name'): Filters results to include only indexes associated with the specified table.

4. How to Drop Index in SQL

To drop an index in SQL, you use the DROP INDEX statement followed by the name of the index to be dropped. For example, to drop an index named  "idx_last_name", you would use the following SQL statement:

DROP INDEX idx_last_name

ON employees;

This removes the "idx_last_name" index from the "employees" table.

5. Rename an INDEX in SQL

The following SQL query renames the index 'index_Date' to 'index_Order_Date' for the Orders table:

ALTER INDEX index_Date ON Orders RENAME TO index_Order_Date;

Important Types of Indexes in SQL

In SQL databases, four important types of indexes in SQL play an important role in improving data retrieval and query performance. Let’s discuss them.

1. Clustered Index

A clustered index determines the physical order of data within a table. When you create a clustered index on a table, the rows are stored in the order of the index key. This means that the data is physically arranged on disk according to the indexed column(s).

Each table can have only one clustered index because the rows can only be sorted in one order. Sorting like this makes it easier to find data, but remember that changing the clustered index also changes the data's order.

Let's consider an example to illustrate the concept of a clustered index.

Suppose you have a table called "Employees" with columns such as "EmployeeID," "FirstName," "LastName," and "DepartmentID."

EmployeeID

FirstName

LastName

DepartmentID

1

John

Doe

101

2

Jane

Smith

102

3

Michael

Johnson

103

4

Emily

Brown

104

If you create a clustered index on the "EmployeeID" column, the rows in the table will be physically sorted based on the values of the "EmployeeID" column. This arrangement allows for efficient retrieval of data based on the clustered index key.

Here's a query to create a clustered index on the "EmployeeID" column in SQL Server:

CREATE CLUSTERED INDEX clusterof_EmployeeID ON Employees(EmployeeID);

2. Non-clustered Index

In contrast with cluster indexes, non-clustered indexes do not physically alter the data sequence in their table. In place of this, however, the non-clustered index creates a different data structure, which contains index key values and pointers pointing to the rows that go ahead and point to their respective rows on tables.

For example, let us continue with our employees’ table. If you create a non-clustered index on the "LastName'' column, SQL Server will create a separate data structure that stores the "LastName" values along with pointers to the corresponding rows in the table. This non-clustered index allows for efficient retrieval of data based on the "LastName'' column without changing the physical order of the table.

Here’s how one would write this in SQL Server:

CREATE NONCLUSTERED INDEX nonclusterof _LastName ON Employees(LastName);

3. Unique Index

This one-of-a-kind index guarantees the uniqueness of values within an indexed column or columns in the whole table. This implies that two different rows cannot have the same value in the indexed column(s).

Unique indexes are commonly used to prevent data integrity constraints by enforcing unique values in some columns thereby disallowing duplication. For instance, you can create a unique email field in a user’s table so as to ensure all users have unique email addresses.

Here’s an example query showing how to create a unique index on email:

CREATE UNIQUE INDEX idx_email ON users(email);

In this example, we're creating a unique index named "idx_email" on the "email" column of the "users" table. This index will ensure that no two users can have the same email address, thus maintaining data integrity.

4. Composite Index

A Composite Index is an index created on several columns in a table. Unlike single-column indexes, composite indexes are created on multiple columns together to facilitate efficient queries that involve filtering or sorting based on multiple criteria.

For example, you may create a composite index upon the “first_name” and “last_name” columns of the employees table to increase the speed of searches for employees by their full names.

Below is an example query which shows how to create a composite index:

 CREATE INDEX idx_full_name ON employees(first_name, last_name);

Best Practices for Utilizing Indexes in SQL

Using indexes correctly in your SQL database is important. Adhering to best practices can greatly improve query performance and overall database efficiency. However, there are a few helpful pointers that you need to keep in mind:

1. Pick the Correct Columns for Indexing

Select columns often used in search predicates or join conditions. These columns should have high selectivity, meaning they have a wide range of values instead of only a few. For example, it is more advantageous to put an index on “customer_id” rather than binary data columns.

 2. Avoid Over-Indexing

While indexes boost query performance, they overload data manipulation operations. Have a balance between optimization and overhead by considering query frequency and types

3. Look at Composite Indexes

If several fields are involved in the query, create composite indexes that cover those fields. An instance would be creating a composite index on ‘last_name’ and ‘first name’ within the table employee, which would help improve query performance.

4. Have Regular Checks and Maintain Indexes

Monitor index usage and key performance indicators to identify bottlenecks. Review execution plans for queries periodically and apply maintenance actions, such as rebuilding or reorganizing indexes, for optimal performance.

5. Think About Indexing for Joins and Sorting

Frequently use indexed columns when you have to join tables or sort query results for better performance. Join indexes make it easier to extract similar data sets, while sort indexes expedite the sorting process, particularly for massive result sets.

6. Test and Validate Index Impact: 

Before launching, carry out extensive tests on the impact of indexing on query performance using real-life workloads. Compare query execution times with and without indices to determine their effectiveness and highlight possible regressions.

Conclusion

In summary, indexes in SQL databases speed up searches of stored data. They organize data so that it can be found quickly.

Knowing how to use indexes in SQL is vital if you want your database queries to run faster. Choosing the right columns to index and avoiding over-indexing will help your database respond more quickly to your queries. 

Keep an eye on your index’s performance and update them when necessary due to changes made in the database. Following these hints will let you maximize your SQL database by getting faster answers.

FAQs

  1. What are the two types of indexes in SQL? 

In SQL, there are two types of indexes: clustered and non-clustered. Clustered indexes define the order in which data is stored in a table, while non-clustered indexes create a separate data structure to support efficient data retrieval.

  1. Is the primary key an index? 

Yes, the primary key in SQL is automatically indexed. It guarantees that each row in a table is individually recognized and acts as the foundation for effective data retrieval and table interactions.

  1. Why do we use indexes in SQL? 

They are used by SQL queries to enhance performance by enabling fast access to data. These organize information into structured format thereby decreasing the time and resources necessary for finding specific details within database tables.

  1. How many indexes are there in SQL? 

Several indexes are available with SQL, such as clustered, non-clustered, unique, composite, covering, and hash indexes, depending on the DBMS used. These could be limited or unlimited, depending on the DBMS being utilized.

  1. What is index size in SQL? 

The term ‘index size’ in SQL denotes the space on a storage device used by an index. This size is determined by several factors, such as the number of indexed columns, the data types employed in those columns, and how the database management system implements them.

  1. How to find indexes in SQL? 

You can find indexes in SQL by querying system catalog views or using database management tools. An example is querying sys.indexes view to get details about indexes defined in a database within SQL Server.

  1. What is the largest index in SQL? 

The largest index depends on the database management system and the availability of resources. Generally, the clustered index is considered the largest one since it defines the physical order of data stored in the table.

  1. Which is the fastest index in SQL? 

The fastest index changes depending on aspects like request workload, distribution of data, and database design among other issues. In general, using a well-designed and properly accessed clustered index will make retrieval faster than other indexing methods.

Amit Chandra

Amit Chandra

Amit Chandra, PMP, SCPM, is a program and product management professional with over 15 years of experience in publishing, EDA and Insurance domai… Read More

Get Free Career Counselling
form image
+91
*
By clicking, I accept theT&Cand
Privacy Policy
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
right-top-arrowleft-top-arrow

upGrad Learner Support

Talk to our experts. We’re available 24/7.

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...