Types of Indexing

Lecture 8 · ten index types, each with the definition and the SQL or diagram that goes with it.

TypeIn one line
1. PrimaryCreated on the primary key of a table (automatic).
2. SecondaryCreated on a non-primary-key attribute.
3. UniqueDoes not allow duplicate values.
4. CompositeCreated using more than one column.
5. ClusteredRows are physically stored in index order — only one per table.
6. Non-clusteredIndex stored separately; key + pointer; many per table.
7. OrderedIndex entries kept in sorted order of the search key.
8. DenseOne index entry for every search-key value.
9. SparseOne index entry per block, not per row.
10. MultilevelAn index built on top of another index.

1 · Primary index

A primary index is created on the primary key of a table.

primary index
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.

secondary index
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.

unique index
CREATE UNIQUE INDEX idx_email ON Student(Email);
-- ensures that one email cannot be used by more than one student

4 · Composite index

A composite index is created using more than one column.

composite index
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

In a clustered index the actual data rows are stored in the same order as the index. If a table is clustered by 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

In a non-clustered index the index is stored separately from the actual data table.
  • 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.

RowStudent_IDName
R1101Rahim
R2102Karim
R3103Nila
R4104Hasan
R5105Rony
Student_IDPointer
101R1
102R2
103R3
104R4
105R5

So ordered index means the index entries are sorted.

8 · Dense index

A dense index has one index entry for every search-key value in the data file. If the table has 5 different Student_ID values, the dense index also has 5 entries. If Student_ID is unique, then there is one index entry for every row.
dense lookup
SELECT * FROM Student WHERE Student_ID = 104;
-- the DBMS finds 104 in the dense index and follows the pointer to row R4

Dense indexes are commonly used for secondary indexes, but they can also be used with primary indexes.

9 · Sparse index

A sparse index does not contain every search-key value. It contains index entries only for some records — usually one entry per block.

(a) Data blocks

Block 1: 101, 102
Block 2: 103, 104
Block 3: 105, 106

(b) The index stores only the first key of each block

Student_IDPointer to Block
101Block 1
103Block 2
105Block 3
To find 104: ① 104 is between 103 and 105 → ② so go to Block 2 → ③ then search inside Block 2.

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_IDPointer
101Block 1
201Block 10
301Block 20
401Block 30
501Block 40
601Block 50

Second-level index over it

Student_IDPointer to First-Level Index Block
101Index Block 1
401Index Block 2
Search second-level index
Find correct first-level index block
Search first-level index
Find data block
Search actual data block
Multilevel indexing is the idea behind B-tree and B+ tree indexes.

Next: B-Tree indexes →