Indexing in DBMS: What is, Types of Indexes with EXAMPLES

โšก Smart Summary

Indexing in Database is a data structure technique that retrieves records quickly by mapping a search key to the disk address of its record. Primary, secondary, clustering, multilevel, and B-tree indexes each trade space, speed, and maintenance differently.

  • ๐Ÿ—‚๏ธ Core Idea: An index is a small two-column table pairing a key with a pointer to the record’s disk block.
  • ๐Ÿ“‡ Primary Index: An ordered file on the key, split into dense and sparse variants.
  • ๐Ÿ”Ž Dense vs Sparse: A dense index stores an entry per key; a sparse index stores fewer entries to save space.
  • ๐Ÿท๏ธ Secondary Index: Built on a non-ordering field, it uses buckets to reach every matching record.
  • ๐Ÿ“š Clustering Index: Groups rows that share a non-unique key into one cluster.
  • ๐ŸŒณ B-Tree Index: A balanced multilevel tree whose linked leaf nodes support random and sequential access.
  • โš–๏ธ Trade-off: Indexes speed reads but slow inserts, updates, and deletes and consume extra space.

Indexing in Database

What is Indexing?

Indexing is a data structure technique that allows you to quickly retrieve records from a database file. An index is a small table having only two columns. The first column comprises a copy of the primary or candidate key of a table. Its second column contains a set of pointers holding the address of the disk block where that specific key value is stored.

An index:

  • Takes a search key as input.
  • Efficiently returns a collection of matching records.

Without an index, the database must scan every row to answer a query. With one, it jumps straight to the matching block, which is why the index type chosen has a large effect on performance.

Types of Indexing in DBMS

Type of Indexes in Database
Type of Indexes in Database

Indexing in a database is defined based on its indexing attributes. The two main types of indexing methods are:

  • Primary Indexing
  • Secondary Indexing

Primary Index in DBMS

A primary index is an ordered file of fixed length size with two fields. The first field is the same as the primary key, and the second field points to that specific data block. In the primary index, there is always a one-to-one relationship between the entries in the index table.

The primary index is also further divided into two types:

  • Dense Index
  • Sparse Index

Dense Index

In a dense index, a record is created for every search key value in the database. This helps you search faster but needs more space to store index records. In this method, records contain the search key value and point to the real record on the disk.

Dense index in DBMS

Sparse Index

A sparse index is an index record that appears for only some of the values in the file. Sparse index helps you resolve the issues of dense indexing in DBMS. In this technique, a range of index columns stores the same data block address, and when data needs to be retrieved, that block address is fetched.

A sparse index stores index records for only some search-key values. It needs less space and less maintenance overhead for insertions and deletions, but it is slower than the dense index for locating records.

Below is a database index example of a sparse index.

Sparse index in DBMS

Dense Index vs Sparse Index

The two primary index variants make opposite trade-offs, summarised below.

Aspect Dense Index Sparse Index
Entries One per search key One per block
Space More Less
Search speed Faster Slower
Maintenance Higher Lower

Secondary Index in DBMS

The secondary index in DBMS can be generated by a field that has a unique value for each record, and it should be a candidate key. It is also known as a non-clustering index.

This two-level database indexing technique is used to reduce the mapping size of the first level. For the first level, a large range of numbers is selected, so the mapping size always remains small.

Secondary Index Example

Let us understand secondary indexing with a database index example. In a bank account database, data is stored sequentially by acc_no, but you may want to find all accounts in a specific branch of ABC bank.

Here, you can have a secondary index for every search key. The index record points to a bucket that contains pointers to all the records with that specific search-key value.

Secondary index in DBMS

Clustering Index in DBMS

In a clustered index, the records themselves are stored in the index, not pointers. Sometimes the index is created on non-primary key columns, which might not be unique for each record. In such a situation, you can group two or more columns to get unique values and create an index, which is called a clustered index. This also helps you identify the record faster.

Example: assume that a company has recruited many employees in various departments. In this case, a clustering index should be created for all employees who belong to the same department.

They are considered as a single cluster, and the index points to the cluster as a whole. Here, Department_no is a non-unique key.

What is a Multilevel Index?

Multilevel indexing is created when a primary index does not fit in memory. In this type of indexing method, you can reduce the number of disk accesses to reach any record. The records are kept on a disk as a sequential file, and a sparse index is created on top of that file.

Multilevel index in DBMS

B-Tree Index

The B-tree index is the most widely used data structure for tree-based indexing in DBMS. It is a multilevel format of tree-based indexing that uses balanced binary search trees. All leaf nodes of the B-tree hold the actual data pointers.

Moreover, all leaf nodes are interlinked with a linked list, which allows a B-tree to support both random and sequential access.

B-tree index in DBMS

  • Leaf nodes must have between 2 and 4 values.
  • Every path from the root to a leaf is mostly of equal length.
  • Non-leaf nodes apart from the root node have between 3 and 5 child nodes.
  • Every node that is not a root or a leaf has between n/2 and n children.

Where exact-match lookups dominate and range scans are rare, hashing can be a faster alternative to a B-tree index.

Advantages of Indexing

The important advantages of indexing are:

  • It helps reduce the total number of I/O operations needed to retrieve data, so you do not need to access a row directly from the table.
  • It offers faster search and retrieval of data to users.
  • It can reduce tablespace, because you do not need to store the ROWID in the index for every linked row.
  • Data in the leaf nodes is already ordered by the value of the key.

Disadvantages of Indexing

The important drawbacks of indexing are:

  • To perform indexing, you need a primary key on the table with a unique value.
  • You cannot build another index on data that is already index-organized in the same way.
  • You are not allowed to partition an index-organized table.
  • Indexing decreases performance in INSERT, DELETE, and UPDATE queries.

FAQs

A primary index is built on the field the file is ordered by, usually the primary key. A secondary index is built on a different field, so it needs buckets to reach every matching record.

A B-tree stays balanced, so every lookup takes a similar small number of disk reads, and its linked leaves support range scans. This makes it strong for both point and range queries.

Every insert, update, and delete must also maintain each index. More indexes speed reads but add write overhead and storage, so they should be created only where queries actually benefit.

AI index advisors study the query workload and recommend indexes that would cut the most cost, while flagging existing indexes that are never used and only add overhead.

A clustered index stores the rows themselves in index order, so a table can have only one. A non-clustered index holds pointers to the rows, so a table can have several of them.

Summarize this post with: