Open In App

PL/SQL Create Index

Last Updated : 01 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In PL/SQL, indexes are used to improve the performance of queries by reducing the number of rows that need to be scanned when retrieving data. The Indexes work like pointers to rows in the table and they can be created on one or more columns of a table to facilitate faster searching.

In this article, We will learn about CREATE INDEX command in PL/SQL explaining its purpose, usage, types and examples to help optimize query performance.

PL/SQL Create Index

  • In PL/SQL, an index is a database object that improves the performance of query execution by allowing faster retrieval of records from a table.
  • Indexes are particularly useful when dealing with large datasets, as they help the database locate rows without needing to scan the entire table.

Why Use Indexes?

  • Faster retrieval: They help retrieve data faster by narrowing down the rows to be scanned.
  • Performance improvement: It is useful in the SELECT, JOIN, WHERE and ORDER BY operations.

Indexes are not automatically created on tables they need to be created explicitly for the optimal query performance.

Syntax:

CREATE INDEX index_name
ON table_name (column1, column2, ...);

Explanation:

  • index_name: The name you want to give to the index.
  • table_name: The name of the table on which the index is being created.
  • column1, column2, ...: The column(s) of the table that you want to index

Important Points

  • We can create an index on a single column or multiple columns.
  • Once created an index is automatically used by the database engine when optimizing queries.
  • Indexes do not change the data in the table but make searching faster.

Types of Indexes

There are different types of the indexes that can be created in the PL/SQL:

  • B-tree Index : The default and most commonly used index type. Efficient for the equality and range queries.
  • Unique Index: Ensures that the values in the indexed columns are unique.
  • Composite Index: An index on the multiple columns.
  • Bitmap Index: Used in the environments with the low cardinality.
  • Function-based Index: The Index based on the expressions or functions applied to the columns.
  • Clustered Index: Typically created on primary keys and sorts the table data to the match the index.

Creating an Index in PL/SQL

Let’s assume we have a table Employees with the columns EmployeeID, FirstName, LastName and DepartmentID. We want to the create an index on the LastName column to the improve search performance when querying based on the last name.

Create the Employees Table

First, create the Employees table with the columns EmployeeID, FirstName, LastName and DepartmentID.

CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY AUTO_INCREMENT,
FirstName VARCHAR(50),
LastName VARCHAR(50),
DepartmentID INT
);

Insert Sample Data

Now, insert some sample data into the Employees table.

INSERT INTO Employees (FirstName, LastName, DepartmentID)
VALUES
('John', 'Smith', 101),
('Jane', 'Doe', 102),
('Alice', 'Johnson', 103),
('Bob', 'Smith', 104),
('Charlie', 'Brown', 105);

Employees Table:

EmployeeID

FirstName

LastName

DepartmentID

1

John

Smith

101

2

Jane

Doe

102

3

Alice

Johnson

103

4

Bob

Smith

104

5

Charlie

Brown

105

Let's Create an Index on LastName

Now, create an index on the LastName column to the improve search performance when querying by the last name.

CREATE INDEX idx_lastname
ON Employees (LastName);

This creates an index called idx_lastname on the LastName column of the Employees table.

Query Using the Index

We can now run queries that filter by the LastName. The index will improve performance for the queries like this one:

SELECT * FROM Employees
WHERE LastName = 'Smith';

The above query will utilize the idx_lastname index to the quickly find employees with the last name "Smith".

Query-Using-the-Index

Creating Unique Indexes

A unique index ensures that no two rows have the same value for the indexed column(s). The Unique indexes are often used to enforce the uniqueness of a column such as in the case of the PRIMARY KEY.

Syntax for Creating a Unique Index:

CREATE UNIQUE INDEX idx_unique_employeeid
ON Employees (EmployeeID);

This command ensures that the EmployeeID in the Employees table is unique across the all rows.

Using Composite Indexes

A composite index is created on the multiple columns. It is particularly useful when queries involve filtering based on the multiple columns.

Syntax for Creating a Composite Index:

CREATE INDEX idx_composite
ON Employees (LastName, DepartmentID);

This index helps in queries that filter by both the LastName and DepartmentID.

Example:

SELECT * FROM Employees8
WHERE LastName = 'Smith' AND DepartmentID = 101;

In this case, the composite index idx_composite will be used to the optimize the query.

Creating-a-Composite-Index

Dropping an Index

If an index is no longer needed it can be dropped using DROP INDEX command.

Syntax:

DROP INDEX index_name;

Example:

DROP INDEX idx_lastname ON Employees;

This will remove the index idx_lastname from the Employees table.

Advantages of Using Indexes

  • Improved Query Performance: The Indexes significantly speed up query execution by the reducing the number of rows scanned.
  • Reduced Disk I/O: It Helps in reducing disk I/O especially in the large datasets.
  • Efficient Data Access: The Indexes are particularly useful in the queries involving the WHERE clauses and JOIN operations.

Disadvantages of Using Indexes

  • Additional Storage: The Indexes take up extra storage space in the database.
  • Slower Data Modification: It Inserts, updates and deletes can be slower because the index must also be modified.
  • Complexity: The Managing too many indexes can add complexity to the database management and maintenance.

Conclusion

The Indexes play a crucial role in optimizing query performance in the PL/SQL. By creating and managing indexes appropriately we can significantly improve the efficiency of the database operations. However, it's essential to the use indexes judiciously as they come with their own overheads like storage space and maintenance costs. Understanding the types of indexes and when to use them will help we make informed decisions when designing the database.


Next Article
Article Tags :

Similar Reads