Types of Indexing
Lecture 8 · ten index types, each with the definition and the SQL or diagram that goes with it.
| Type | In one line |
|---|---|
| 1. Primary | Created on the primary key of a table (automatic). |
| 2. Secondary | Created on a non-primary-key attribute. |
| 3. Unique | Does not allow duplicate values. |
| 4. Composite | Created using more than one column. |
| 5. Clustered | Rows are physically stored in index order — only one per table. |
| 6. Non-clustered | Index stored separately; key + pointer; many per table. |
| 7. Ordered | Index entries kept in sorted order of the search key. |
| 8. Dense | One index entry for every search-key value. |
| 9. Sparse | One index entry per block, not per row. |
| 10. Multilevel | An index built on top of another index. |
1 · Primary index
A primary index is created on the primary key of a table.
CREATE TABLE Student (
Student_ID INT PRIMARY KEY,
Name VARCHAR(50),
Department VARCHAR(20)
);
-- Here, the DBMS automatically creates an index on Student_ID.2 · Secondary index
A secondary index is created on a non-primary-key attribute.
CREATE INDEX idx_department ON Student(Department);
-- now searching by department becomes faster:
SELECT * FROM Student WHERE Department = 'CSE';3 · Unique index
A unique index does not allow duplicate values.
CREATE UNIQUE INDEX idx_email ON Student(Email);
-- ensures that one email cannot be used by more than one student4 · Composite index
A composite index is created using more than one column.
CREATE INDEX idx_dept_cgpa ON Student(Department, CGPA);
-- useful for queries like:
SELECT * FROM Student
WHERE Department = 'CSE'
ORDER BY CGPA DESC; Column order matters: this index helps WHERE Department = … and WHERE Department = … ORDER BY CGPA, but not a query that filters on CGPA alone (the leftmost-prefix rule).
5 · Clustered index
Student_ID, then rows are physically arranged according to Student_ID. A table usually has only one clustered index, because data can be physically sorted in only one order. 6 · Non-clustered index
- The index contains the search key and a pointer to the actual row.
- A table can have multiple non-clustered indexes.
7 · Ordered index
An ordered index keeps the search-key values in sorted order.
| Row | Student_ID | Name |
|---|---|---|
| R1 | 101 | Rahim |
| R2 | 102 | Karim |
| R3 | 103 | Nila |
| R4 | 104 | Hasan |
| R5 | 105 | Rony |
| Student_ID | Pointer |
|---|---|
| 101 | R1 |
| 102 | R2 |
| 103 | R3 |
| 104 | R4 |
| 105 | R5 |
So ordered index means the index entries are sorted.
8 · Dense index
SELECT * FROM Student WHERE Student_ID = 104;
-- the DBMS finds 104 in the dense index and follows the pointer to row R4Dense indexes are commonly used for secondary indexes, but they can also be used with primary indexes.
9 · Sparse index
(a) Data blocks
Block 2: 103, 104
Block 3: 105, 106
(b) The index stores only the first key of each block
| Student_ID | Pointer to Block |
|---|---|
| 101 | Block 1 |
| 103 | Block 2 |
| 105 | Block 3 |
A sparse index works only when the data file is sorted/ordered on the search key, and is commonly used for primary indexes.
10 · Multilevel index
A multilevel index means an index is built on top of another index. It is used when the first-level index itself becomes large.
Large sparse (first-level) index
| Student_ID | Pointer |
|---|---|
| 101 | Block 1 |
| 201 | Block 10 |
| 301 | Block 20 |
| 401 | Block 30 |
| 501 | Block 40 |
| 601 | Block 50 |
Second-level index over it
| Student_ID | Pointer to First-Level Index Block |
|---|---|
| 101 | Index Block 1 |
| 401 | Index Block 2 |
Next: B-Tree indexes →