Indexing in a Database

Lecture 8 Β· indexing is a technique used in a database to make data searching faster. An index works like the index of a book: instead of scanning the whole table, the DBMS uses the index to directly find the required rows.

The problem β€” tables are unsorted

A table usually stores data in an unsorted manner and is not optimized for fast searching. Notice that Emp_ID is not sorted here:

Row LocationEmp_IDNameDept
R1101RahimCSE
R2105RonyCSE
R3103KarimEEE
R4110NilaBBA
R5102HasanCSE
the query
SELECT *
FROM Employee
WHERE Emp_ID = 105;
Without an index the DBMS may check rows like this: R1 β†’ R2 β†’ R3 β†’ R4 β†’ R5. It searches the table row by row β€” a full table scan, O(n).

The fix β€” create an index

create index
CREATE INDEX idx_emp_id
ON Employee(Emp_ID);

The index stores the values of the selected column in a sorted / searchable structure, along with pointers to the actual table rows:

Emp_IDRow Pointer
101R1
102R5
103R3
105R2
110R4

Notice that the index is sorted by Emp_ID, not by table row order.

Step 1 β€” search the smaller SORTED index
binary search finds Emp_ID 105
Get the actual row pointer R2
Step 2 β€” go directly to row R2 in the actual table
The DBMS does not scan the full table
Search index β†’ find Emp_ID 105 β†’ get row pointer R2 β†’ fetch row from table.

Uses / benefits of indexing

BenefitExample
To search records quicklySELECT * FROM Student WHERE Student_ID = 105;
To speed up sortingSELECT * FROM Student ORDER BY CGPA DESC;
To improve join performanceSELECT Student.Name, Enrolls.Course_ID FROM Student, Enrolls WHERE Student.Student_ID = Enrolls.Student_ID; β€” if Student_ID is indexed in both tables, the join becomes faster.
To enforce uniquenessCREATE UNIQUE INDEX idx_email ON Student(Email); β€” ensures duplicate emails are not allowed.
Reduces disk I/OInstead of reading the whole table, the DBMS reads only the required index pages and data pages.

Demerits of indexing

DemeritExplanation
Extra storage is requiredIndexes need additional disk space; a table with many indexes makes the database bigger.
Insert becomes slowerINSERT INTO Employee VALUES (106,'Sakib','CSE',52000); β€” the DBMS inserts the row and also updates the index.
Update becomes slowerIf an indexed column is updated, the index must also be modified.
Delete becomes slowerWhen a row is deleted, the related index entry must also be deleted.
Too many indexes reduce performanceIndexes improve read performance, but too many of them reduce write performance.
Not useful for small tablesIf a table has very few rows, a full table scan may be faster than using an index.
The one-sentence trade-off: an index buys read speed with storage and write speed. Index the columns you search, join and sort on β€” and nothing else.

Next: Evaluation metrics β†’