IBPS IT Officer · Government IT exam preparation
Clustered vs Nonclustered Indexes: Simple Database Guide
Dense/sparse indexes, selectivity, covering and composite keys.
Clustered & nonclustered indexes: the core idea
An index is an extra access structure that can help a database find rows with less work. A clustered index and a nonclustered index organise access differently. The exact meaning of clustering depends on the database product.
A book’s main pages can be organised in one order, while its back-of-book index offers many other lookup paths. This is a memory aid. A real database stores pages, pointers and row versions rather than one perfectly sorted sheet.
| Concept | Meaning | Example / distinction |
|---|---|---|
| SQL Server clustered rowstore index | Its leaf level contains the data rows | Only one clustered organisation per table |
| SQL Server nonclustered index | A separate structure with keys and row locators | Several can serve different access paths |
| InnoDB clustered index | Usually organised by the primary key | Secondary indexes include the clustered key |
| PostgreSQL CLUSTER | Rewrites a table using an index order | That order is not maintained automatically after every later write |
| Covering index | Contains the values needed by a query | Can reduce extra table lookups; product details differ |
Exam essentials
Dense versus sparse
A dense index has an entry for each search-key value or record, according to the representation. A sparse index has entries for selected positions, often one per data block. Classic sparse primary indexes rely on the data file being ordered on that search key.
Primary is not always clustered
In textbook file organisation, a primary index is on the ordering key of a sorted file; a clustering index may be on an ordering non-key field. In products such as SQL Server, a PRIMARY KEY can be nonclustered. Do not assume every product uses those textbook names identically.
Composite index order
A B-tree on (branch_id, balance) is naturally ordered by branch first, then balance within each branch. It commonly helps branch-only and branch-plus-balance filters. A balance-only query may require more scanning; some engines can use skip scans or other optimisations.
Covering and selectivity
A covering index contains everything needed by a query, though visibility checks and engine details still matter. Selectivity describes how narrowly a predicate or key identifies rows. If most rows are needed, a sequential scan can be cheaper than many index lookups.
Index cost
Indexes consume space and add work to INSERT, UPDATE and DELETE. They do not guarantee faster queries. Estimate page reads, maintain statistics and consider the complete workload. A separate nonclustered index may store a row locator or clustered key, depending on the engine.
Worked exam problem
A sorted file has 10,000 records, 100 per block. A sparse index has one entry per data block. How many entries are needed?
- Data blocks = ceiling(10,000 / 100) = 100. The sparse index has 100 entries.
- A dense representation with one entry per record would have 10,000 entries. These are entry counts, not byte sizes.
- The sparse lookup first identifies a candidate block, then searches within it. An unordered file cannot use this simple ordered sparse lookup in the same way.
- Search index
- Locate candidate data block
- Find required row
Practical example
A query finds accounts by CustomerID. An index on CustomerID may help. If it returns most of the table, scanning can be cheaper. Adding one index per column is not a sound universal strategy.
CREATE INDEX idx_accounts_customer ON Accounts(customer_id);
-- Study the plan; the optimiser may still choose a scan.
EXPLAIN SELECT account_id FROM Accounts WHERE customer_id = 12;Common exam traps
- An index does not guarantee it will be used or that a query becomes faster.
- A primary key is a logical constraint. Its physical index arrangement depends on the product.
- Never rely on an index alone to guarantee output order; use ORDER BY.
Verified previous-year questions
GATE questions, paraphrased for clarity and checked against official answer keys. These are not IBPS past-paper claims. MSQ means more than one option may be correct.
Exam practice: apply the rules
Six original questions with plausible alternatives and worked explanations. These are practice questions, not previous-year questions.
Current usage and product differences
Modern systems also use columnstore, partial, expression and specialised indexes. These are workload choices. Learn the rowstore comparison first and label product-specific claims clearly.
References
Technical reference · Database System Concepts: chapter references