Chapter 03
Indexing, B+ Trees & NoSQL Systems
3 topics · 2 sections · beginner-friendly study notes · Updated
An index is like the index at the back of a book: it helps the database find rows quickly without reading the whole table.
B+ trees are the most common index structure.
NoSQL systems are databases that do not use the usual table model.
Indexing calculations and B+ tree questions are common in GATE.
NoSQL and CAP are common in interviews.
Making a bank search by account number fast, and choosing the right database for a social media or e-commerce app.
Database Indexing Principles
An index is an extra data structure that stores search-key values with pointers to the matching rows, so the DBMS can find rows without scanning the whole table.
In short: Without an index, the database reads the whole table like reading a book page by page. With an index, it jumps straight to the right page.
Think about it
Searching is the most common operation. Indexes make it fast on large tables. Searching a customer by account number in a bank table with millions of rows takes seconds without an index and milliseconds with one.
Data on disk is read in blocks.
A table with 10,000 blocks needs up to 10,000 block reads for a full scan.
An index is small, so searching it needs far fewer reads.
Types: a primary index is on the sorted ordering key of the file.
A clustering index is on a sorted non-key column.
A secondary index is on any other column and is always dense.
A dense index has one entry for every row.
A sparse index has one entry for every block, so it is smaller.
A file can have only one primary or clustering index (because data can be sorted only one way), but many secondary indexes.
Trade-off: indexes make reads faster but make INSERT, UPDATE and DELETE slower, and take extra space.
Key points
- Search key: The
column(s)on which the index is built. - Dense index: One index entry for every record.
- Sparse index: One index entry for every block; needs a sorted file.
- Clustering index: Index on a sorted non-key column of the file.
- Secondary index: Index on a non-ordering column; always dense.
- Only one primary/clustering index per file; many secondary indexes allowed.
- Binary search on a sorted file with b blocks needs about
ceil(log2 b)block reads.
Read more
Example
CodeCreating and dropping an index: CREATE INDEX idx_account_no ON account(account_no); DROP INDEX idx_account_no ON account;
Advantages of indexing
Much faster searching, sorting and range queries; fewer disk reads.
Disadvantages of indexing
Extra storage, slower inserts and updates because indexes must also be changed, and too many indexes can hurt performance.
B+ Trees Indexing
A B+ tree is a balanced, multi-level search tree used for indexes. All data pointers are in the leaf nodes, all leaves are at the same level, and the leaves are linked together in sorted order.
In short: It is like a tree of signboards. Each signboard sends you to the next level until you reach the exact leaf with your key.
Think about it
B+ trees keep search, insert and delete fast, even for huge tables. Almost all relational databases use them. A bank index with 10 million accounts can be searched in about 3 or 4 block reads using a B+ tree.
Internal nodes store only keys and child pointers (they are signposts).
Leaf nodes store keys with record pointers, and a pointer to the next leaf.
So range queries (for example salary between 20,000 and 40,000) are fast: find the first key, then walk along the leaves.
The tree stays balanced, so every search takes the same number of steps, which equals the height.
Order p of an internal node: the maximum number of child pointers.
Every node (except the root) must be at least half full.
When a node overflows, it splits and the middle key moves up.
If the root splits, the tree grows by one level.
Finding the order: p × (pointer size) + (p − 1) × (key size) ≤ block size.
B-trees store data pointers in internal nodes also, but B+ trees keep them only in leaves, so B+ trees have a higher fan-out and a lower height.
Key points
- Order (fan-out): Maximum number of child pointers of an internal node.
- Leaf node: Bottom node holding keys with record pointers and a link to the next leaf.
- Split: Dividing an overflowing node into two and pushing a key up.
- Height: Number of levels; equals the block reads needed for a search.
- Internal node with order p: at most p children and p − 1 keys; at least
ceil(p/2)children (root at least 2). - B+ tree vs B tree: B+ has all data in leaves, linked leaves, and a better range search.
- Search, insert and delete cost O(log n) block accesses.
Read more
SQL vs NoSQL & CAP Theorem
SQL (relational) databases store data in tables with a fixed schema and follow ACID rules. NoSQL databases use other models (key-value, document, column family, graph) with a flexible schema and usually scale across many machines. The CAP theorem says a distributed system can guarantee only two of three: Consistency, Availability and Partition tolerance.
In short: SQL is a neat register with fixed columns. NoSQL is a flexible box where each item can have its own shape. CAP says that when the network breaks, you must choose between always answering and always being exactly correct.
Think about it
Different applications need different trade-offs. Choosing the right database type avoids costly redesign later. A bank uses SQL because money transfers must be exact. A social media app uses NoSQL because it must store huge data of different shapes and always respond.
SQL: fixed schema, joins, strong ACID, usually scales up (a bigger machine).
Examples
MySQL, PostgreSQL, Oracle.
NoSQL: flexible schema, easy horizontal scaling, usually BASE (Basically Available, Soft state, Eventually consistent).
Types: key-value (Redis), document (MongoDB), column family (Cassandra), graph (Neo4j).
CAP: Consistency means every read sees the latest write.
Availability means every request gets a response.
Partition tolerance means the system works even if the network between nodes breaks.
Network partitions cannot be avoided in a distributed system, so the real choice is CP or AP.
CP systems (such as a bank ledger) refuse some requests to stay correct.
AP systems (such as a social media feed) always answer but may show slightly old data for a while.
Key points
- BASE: Basically Available, Soft state, Eventually consistent.
- Horizontal scaling: Adding more machines to share the load.
- Partition tolerance: System keeps working even when network links between nodes fail.
- Eventual consistency: Replicas become the same after some time if no new writes arrive.
- CAP: pick any two, but P is unavoidable in distributed systems, so the real choice is CP or AP.
- Typical: MongoDB (CP-leaning), Cassandra (AP), traditional single-node RDBMS (CA, no partitions).
Read more
Example
CodeA NoSQL document:
// A document in a NoSQL store (MongoDB style)
{
"_id": 101,
"name": "Asha",
"accounts": [ { "type": "savings", "balance": 5000 } ]
}Advantages of NoSQL
Flexible schema, easy scaling across servers, fast for huge simple reads and writes, good for unstructured data.
Disadvantages of NoSQL
Weaker consistency in many systems, no standard query language, limited joins, and fewer transaction guarantees.
Solved examples
A file has 100,000 records, 10 records per block, sorted on the search key. Block size is 1024 bytes, key is 10 bytes, block pointer is 6 bytes. Compare block reads: binary search on the file vs a sparse primary index.
Block accesses in both cases
Formula Applied:—Approach note: Index search cost = ceil(log2 index blocks) + 1 data block.
Block size 1024 bytes, search key 10 bytes, block pointer 6 bytes. What is the order p of an internal B+ tree node?
Order of the B+ tree
Formula Applied:—Approach note: Pointers = p, keys = p − 1. Always round down.
A bank wants a system where money transfers are always exact, even if a few requests must be rejected during a network failure. Which CAP choice fits?
CP or AP
Formula Applied:—Approach note: Money and exactness → CP. Feeds and likes → AP.
A B+ tree leaf holds at most 3 keys and is currently full: 5, 10, 15. Insert key 12. Describe the split.
Leaf contents after split + separator
Formula Applied:—Approach note: On overflow, split evenly and push/copy a separator to the parent; leaves remain chained.
Real-world applications
Using Indexing, B+ Trees & NoSQL Systems in real projects
Making a bank search by account number fast, and choosing the right database for a social media or e-commerce app.
Common mistakes
❌ Myth: More indexes always make the database faster.
Why this is wrong: Every INSERT, UPDATE and DELETE must also update every index.
✅ Verified Truth: Indexes speed up reads but slow down writes and use space.
⚠️ Exam Tip: Say "read-write trade-off" in the answer.
❌ Myth: NoSQL databases never have a schema or never use any rules.
Why this is wrong: They have a flexible schema, and many support transactions in some form.
✅ Verified Truth: NoSQL means "not only SQL" with different trade-offs, usually BASE.
⚠️ Exam Tip: Compare ACID and BASE.
❌ Myth: In CAP you can freely drop Partition tolerance.
Why this is wrong: Network failures cannot be avoided in a distributed system.
✅ Verified Truth: The real choice is CP or AP once the system is distributed.
⚠️ Exam Tip: Remember "P is not optional".
Revision notes
An index is like the index at the back of a book: it helps the database find rows quickly without reading the whole table. B+ trees are the most common index structure. NoSQL systems are databases that do not use the usual table model.
High-Yield Quick Revision Points
- Index = fast search, slower writes, extra space
- Dense: one entry per record. Sparse: one entry per block (sorted file)
- One primary/clustering index per file; many secondary indexes
- B+ tree: balanced, data in leaves, leaves linked
- Order: p·P + (p−1)·K ≤ block size
- SQL = fixed schema + ACID. NoSQL = flexible schema + BASE
- CAP: pick two; P is unavoidable so choose CP or AP
Practice questions
Which index has one entry for every record?
In a B+ tree, record pointers are stored in:
A file can have at most how many primary (or clustering) indexes?
A system that is always available but may return old data during a network partition is:
Which is an advantage of a B+ tree over a B tree?