Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
PL/SQL Drop Index
Next article icon

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
PL/SQL Drop Index

V

vinod19ldr
Improve
Article Tags :
  • Databases
  • PL/SQL

Similar Reads

  • PostgreSQL - CREATE INDEX
    The PostgreSQL CREATE INDEX statement is essential for improving database performance, allowing faster data retrieval by creating indexes on specified columns. Indexes in PostgreSQL act like pointers, significantly reducing the time required for query processing, especially on large tables. In this
    5 min read
  • PL/SQL Drop Index
    In PL/SQL an index is a database object that improves the speed of the data retrieval operations on the table. However, there are situations where we might want to remove an index either to optimize performance or to retrieve disk space. In this article, we will provide a comprehensive guide on how
    4 min read
  • Index in PL/SQL
    PL/SQL, Oracle's extension to SQL, combines SQL with procedural programming features like loops, conditionals, and exception handling. It enables developers to create stored procedures, functions, triggers, and other database applications. As a block-structured language, PL/SQL allows seamless integ
    5 min read
  • SQL CREATE INDEX Statement
    In SQL, indexes are important for optimizing query performance by speeding up data retrieval operations. The CREATE INDEX statement is used to create indexes in tables, enabling quicker searches and improving database efficiency. In this article, we will explain how to use the SQL CREATE INDEX State
    5 min read
  • PL/SQL CREATE VIEW
    PL/SQL CREATE VIEW is a statement used to create a virtual table based on the result of a query. Views in PL/SQL allow users to access and manipulate data stored in one or more underlying tables as if it were a single table. In this article, We will learn about the PL/SQL CREATE VIEW by understandin
    3 min read
  • SQL CREATE DATABASE
    Creating a database is one of the fundamental tasks in SQL and is the first step in structuring your data for efficient management. Whether you're a developer or a database administrator, understanding the CREATE DATABASE statement is essential. Understanding how to use this command effectively is c
    6 min read
  • PL/SQL Insert DateTimes
    In PL/SQL, handling date and time values is a common requirement, whether we are working with databases that store transaction times, schedule events, or track user activities. Understanding how to insert these values correctly is crucial for accurate data management and retrieval. In this article,
    4 min read
  • PL/SQL INSERT INTO
    PL/SQL (Procedural Language/Structured Query Language) is Oracle's procedural extension to SQL. It allows us to write complex queries and scripts that include procedural logic, control structures, and error handling. The INSERT INTO statement in PL/SQL is essential for adding new rows of data to tab
    6 min read
  • PL/SQL Show Indexes
    Indexes are crucial in database management for speeding up data retrieval by creating quick access paths to the data. In PL/SQL, indexes can significantly enhance query performance by avoiding full table scans, especially on columns used in search conditions or joins. In this article, we will PL/SQL
    7 min read
  • PL/SQL Unique Index
    A PL/SQL Unique Index is a powerful database object used to ensure the uniqueness of values in one or more columns of a table. In Oracle databases, indexes play an important role in enhancing query performance by enabling quick retrieval of rows. The unique index specifically enforces a rule that no
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences