Instructor: Dr. Mohsin Dar
Position: Assistant Professor, Cloud & Software Operations Cluster (SOCS)
Institution: UPES
Course: Database Systems - MTech First Semester
An index is a database object that improves the speed of data retrieval operations on a database table. It works similarly to an index in a book, allowing the database to find data without having to scan the entire table.
| Column Name | Data Type | Width | Attributes |
|---|---|---|---|
| Employee_id | CHAR | 10 | PRIMARY KEY |
| First_Name | VARCHAR | 30 | NOT NULL |
| Last_Name | VARCHAR | 30 | NOT NULL |
| DOB | DATE | - | - |
| Salary | NUMBER | 25 | NOT NULL |
| Department_id | CHAR | 10 | - |
CREATE TABLE EMPLOYEES ( Employee_id CHAR(10) PRIMARY KEY, First_Name VARCHAR(30) NOT NULL, Last_Name VARCHAR(30) NOT NULL, DOB DATE, Salary NUMBER(25) NOT NULL, Department_id CHAR(10) );
-- Inserting sample data into EMPLOYEES table INSERT INTO EMPLOYEES VALUES ('E1001', 'John', 'Doe', '1990-05-15', 75000, 'D101'); INSERT INTO EMPLOYEES VALUES ('E1002', 'Jane', 'Smith', '1985-08-22', 85000, 'D101'); INSERT INTO EMPLOYEES VALUES ('E1003', 'Robert', 'Johnson', '1992-03-10', 65000, 'D102'); INSERT INTO EMPLOYEES VALUES ('E1004', 'Emily', 'Williams', '1988-11-05', 90000, 'D103'); INSERT INTO EMPLOYEES VALUES ('E1005', 'Michael', 'Brown', '1991-07-18', 72000, 'D102'); INSERT INTO EMPLOYEES VALUES ('E1006', 'Sarah', 'Davis', '1993-09-25', 78000, 'D101');
The EMPLOYEES table has been created with 6 sample records. This table will be used to demonstrate different types of indexes in the following sections.
Let's create an index on the Last_Name and Department_id columns to speed up searches on these columns.
-- Create an index on Last_Name and Department_id columns CREATE INDEX employee_idx ON EMPLOYEES(Last_Name, Department_id);
ROWID is a pseudo-column that returns the address of each row in the database table. Let's find the ROWID for our EMPLOYEES table.
-- Query to find ROWID for the EMPLOYEES table SELECT ROWID, Employee_id, First_Name, Last_Name FROM EMPLOYEES;
Let's create a unique index on the Employee_id column to ensure there are no duplicate employee IDs.
-- Create a unique index on Employee_id CREATE UNIQUE INDEX emp_id_idx ON EMPLOYEES(Employee_id);
A reverse key index stores the bytes of the indexed column in reverse order. This is useful for columns with sequential values to avoid I/O bottlenecks.
-- Create a reverse key index on Employee_id CREATE INDEX emp_id_reverse_idx ON EMPLOYEES(Employee_id) REVERSE;
Let's create a composite unique index on multiple columns to ensure the combination is unique.
-- Create a composite unique index on First_Name and Last_Name CREATE UNIQUE INDEX emp_name_unique_idx ON EMPLOYEES(First_Name, Last_Name);
Attempting to insert duplicate values in columns with a unique index will result in an error. For example, trying to insert another employee with the same Employee_id will fail.
Function-based indexes allow you to create indexes on the results of functions or expressions. This is useful for case-insensitive searches.
-- Create a function-based index for case-insensitive search on Last_Name CREATE INDEX emp_lastname_upper_idx ON EMPLOYEES(UPPER(Last_Name));
Now you can perform case-insensitive searches efficiently:
-- This query will use the function-based index SELECT * FROM EMPLOYEES WHERE UPPER(Last_Name) = 'SMITH';
Let's see all the indexes we've created on the EMPLOYEES table.
-- For Oracle SELECT index_name, index_type, uniqueness, status FROM user_indexes WHERE table_name = 'EMPLOYEES';
-- For MySQL SHOW INDEX FROM EMPLOYEES;
Let's drop the function-based index we created earlier.
-- Drop the function-based index DROP INDEX emp_lastname_upper_idx;
It's important to monitor whether your indexes are being used effectively.
-- Check if indexes are being used (Oracle) SELECT name, used FROM v$object_usage WHERE index_name = 'EMPLOYEE_IDX';