BLOG DETAILS

Read the complete blog details covering in-depth explanations, practical insights, and examples. Designed for computer science learners and technology enthusiasts who want clarity, guidance, and knowledge in simple, structured form.

Indexing in DBMS: Types, Concepts and Examples

Indexing in Database

Indexing is a technique used in a database to make the retrieval of records faster and more efficient. DBMS uses an index to locate the required records without scanning the entire data file.

  • Indexing improves database performance by minimizing the number of disc visits required to fulfill a query.
  • It is a data structure technique used to locate and quickly access data in databases.
  • Indexing in database is used to reduce the I/O cost. The number of blocks transferred from secondary memory to primary memory is called I/O cost.
Important: An index generally contains a search-key value and a pointer that helps locate the corresponding record or block of records.

indexing-in-dbms-types-concepts-examples

Ordered Index

An ordered index is an index in which the index entries are maintained in sorted order according to the search-key values.

Ordered indexes are particularly useful when we need to perform range searches, such as finding all students whose roll numbers are between 100 and 120.

Primary Index

A primary index is an index created on a file that is physically ordered according to its primary key or ordering key.

Conditions for Primary Index
  • DB file should be physically ordered based on search key used in the index file.
  • Search key should be Primary Key or Alternative Key for DB file.

Suppose student records are stored according to Roll_No. as 101, 105, 110, 115, 120, 125.

If the file is physically arranged according to Roll_No, an index created on Roll_No is called a Primary Index.

Note: In the classical DBMS definition, a Primary Index is generally a Sparse Index because the data file is already ordered.
Primary Index : Ordering Key : Usually Sparse

Clustering Index

A clustering index is an index used when the records in a file are physically ordered according to a non-key attribute, called the clustering field.

  • A non-key attribute can contain duplicate values.

For example, consider employee records arranged according to Department:

EmployeeDepartment
E1HR
E2HR
E3IT
E4IT
E5Sales
E6Sales

Here, Department is a non-key attribute because several employees can have the same department. A clustering index can point to the beginning of each group:

DepartmentPointer
HRHR Group
ITIT Group
SalesSales Group

Thus, instead of identifying a single record, the index helps locate a group of records having the same clustering-field value.

Clustering Index : Non-key Ordering Field

Secondary Index

A secondary index is an index created on a search key that does not determine the physical ordering of the data file.

Suppose a student file is physically arranged according to Roll_No as 101, 102, 103, 104, 105, ...

Now suppose we frequently search students by their Name. An index can be created on Name, even though the actual data file is not physically ordered by Name. That index is called a Secondary Index.

Note:
  • A secondary index is generally Dense Index, because the search key is not the ordering field of the data file.
  • Secondary Index : Non-ordering Search Key : Usually Dense
  • We can build more than one secondary index according to requirement.
  • In multi-level indexing, except first level, all other level of index is generally sparse.

Sparse Index

A sparse index contains index entries for only some search-key values. In the classical primary-index arrangement, there is commonly one index entry for each data block.

Index KeyPointer
101Block 1
115Block 2
130Block 3

A sparse index requires less storage than a dense index. After locating the appropriate block through the index, the DBMS may search within that block to find the required record.

Dense Index

A dense index contains an index entry for every search-key value or, in the common record-level case, an entry for every record.

Index KeyPointer
101Record 1
105Record 2
110Record 3
115Record 4

The main advantage of a dense index is that it can provide direct and fast access to records. The disadvantage is that it requires more storage space and more maintenance when records change.

Dense Index vs Sparse Index

FeatureDense IndexSparse Index
Number of EntriesMoreFewer
Storage RequirementHigherLower
Entry for every record/search-key valueYesNo
Search within Data BlockUsually less necessaryMay be required
Classical ExampleSecondary IndexPrimary Index
Important: The Dense and Sparse describe the density of index entries. Primary, Clustering, and Secondary describe the relationship between the index, search key, and physical file ordering.

Hash Index

A hash index uses a hash function to determine the location of a record or bucket.

Unlike an ordered index, the entries are not maintained primarily for sequential ordering. Instead, a search-key value is passed through a hash function.

  • Hash indexing is particularly efficient for exact-match.

Static Hashing

In static hashing, the number of buckets is fixed when the hash structure is created.

Dynamic Hashing

In dynamic hashing, the hash structure can grow or adapt as the number of records changes.

This is useful for databases where records are frequently inserted or deleted. Instead of keeping a permanently fixed number of buckets, the structure can be adjusted as required.

Two important dynamic hashing techniques are, Extendible Hashing and Linear Hashing.

Summary of Types of Indexing

CategoryTypeShort Description (Summary)
Ordered IndexPrimary IndexCreated on the ordering key of a sequentially ordered file. Generally Sparse.
Clustering IndexCreated on a non-key field according to which the file is physically ordered.
Secondary IndexCreated on a search key that does not determine the physical ordering of the file. Generally Dense.
Index DensityDense IndexContains an index entry for every search-key value/record, depending on the specific organization.
Sparse IndexContains entries for only some search-key values, commonly one per data block in a primary index.
Hash IndexStatic HashingUses a fixed number of buckets.
Dynamic HashingAllows the hash structure to grow or adapt as data changes.
Dynamic HashingExtendible HashingUses a directory and dynamically splits buckets.
Linear HashingUses gradual, progressive bucket splitting to grow the file.
Quick Revision:
  • Primary Index : Ordering key : Usually Sparse
  • Clustering Index : Non-key ordering field
  • Secondary Index : Non-ordering search key : Usually Dense
  • Dense Index : Entry for every search-key value/record
  • Sparse Index : Entries for only some search-key values
  • Static Hashing : Fixed number of buckets
  • Dynamic Hashing : Hash structure can grow or adapt
  • Extendible Hashing : Directory + dynamic bucket splitting
  • Linear Hashing : Gradual, progressive bucket splitting
Suresh Kulahry

About the Author

Suresh Kulahry

Suresh Kulahry is the Founder of Suraku Academy and an educator dedicated to helping students prepare for Computer Science examinations through simple explanations, high-quality study material, MCQs, Previous Year Questions, and practical tutorials. His mission is to make computer science learning easy, accessible, and free for every learner.

🎉 Thank You for Reading!

We hope this article helped you understand the topic better. Continue your Computer Science preparation with more free resources available on Suraku Academy.