DBMS Tutorial chapters (4)
  1. 01Database Concepts & Relational Architecture
  2. 02Functional Dependencies & Normalization
  3. 03Indexing, B+ Trees & NoSQL Systems
  4. 04Transactions & Concurrency Control

Chapter 03

Indexing, B+ Trees & NoSQL Systems

3 topics · 2 sections · beginner-friendly study notes · Updated

Share with friends:WhatsAppTelegram

Embed on your site

Free for teachers, schools and blogs. Please keep the attribution link.

Preview

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.

1

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.

Index: fast lookup like a book index

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.

Example

Code
Creating 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.

2

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.

Key: 50 Key: 20 | 35 Key: 70 | 85 [10, 15, 20] ➜ [25, 30, 35] ➜ [55, 60, 70] ➜ [75, 80, 85]
Indexing, B+ Trees & NoSQL Systems

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.
3

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.

Transactions keep data safe under concurrency

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).

Example

Code
A 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

Example 01 · Intermediate

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.

Given Data:
    Required To Find:

    Block accesses in both cases

    Formula Applied:—
    Step-by-Step Solution:
    Step 1: Data blocks = 100,000 / 10 = 10,000.
    Step 2: Binary search on data: ceil(log2 10,000) = 14 block reads.
    Step 3: Sparse index has one entry per data block: 10,000 entries.
    Step 4: Index entry size = 10 + 6 = 16 bytes, so 1024 / 16 = 64 entries per block.
    Step 5: Index blocks = ceil(10,000 / 64) = 157.
    Step 6: Binary search on the index: ceil(log2 157) = 8, plus 1 data block read = 9.
    Final Answer: 14 block reads without the index, 9 with the sparse index.

    Approach note: Index search cost = ceil(log2 index blocks) + 1 data block.

    Example 02 · Advanced

    Block size 1024 bytes, search key 10 bytes, block pointer 6 bytes. What is the order p of an internal B+ tree node?

    Given Data:
      Required To Find:

      Order of the B+ tree

      Formula Applied:—
      Step-by-Step Solution:
      Step 1: Internal node holds p pointers and p − 1 keys.
      Step 2: 6p + 10(p − 1) ≤ 1024
      Step 3: 16p ≤ 1034, so p ≤ 64.6
      Step 4: Largest whole number p = 64.
      Final Answer: p = 64 (up to 63 keys per internal node)

      Approach note: Pointers = p, keys = p − 1. Always round down.

      Example 03 · Basic

      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?

      Given Data:
        Required To Find:

        CP or AP

        Formula Applied:—
        Step-by-Step Solution:
        Step 1: Exact balances means strong Consistency.
        Step 2: Network failures happen, so Partition tolerance is required.
        Step 3: Rejecting some requests means giving up Availability.
        Final Answer: CP (Consistency + Partition tolerance)

        Approach note: Money and exactness → CP. Feeds and likes → AP.

        Example 04 · Intermediate

        A B+ tree leaf holds at most 3 keys and is currently full: 5, 10, 15. Insert key 12. Describe the split.

        Given Data:
          Required To Find:

          Leaf contents after split + separator

          Formula Applied:—
          Step-by-Step Solution:
          Step 1: Temporarily the leaf would hold 5, 10, 12, 15 (overflow)
          Step 2: Split into two leaves of 2 keys each: left 5,10 and right 12,15
          Step 3: Copy the smallest key of the right leaf (12) up into the parent as separator
          Step 4: Leaves stay linked for ordered range scans
          Final Answer: Left leaf: 5,10; right leaf: 12,15; parent gains separator key 12.

          Approach note: On overflow, split evenly and push/copy a separator to the parent; leaves remain chained.

          Real-world applications

          DBMS

          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.

          Mechanism: Write small programs, read errors carefully, then grow complexity only after the basics are solid.

          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

          1. Index = fast search, slower writes, extra space
          2. Dense: one entry per record. Sparse: one entry per block (sorted file)
          3. One primary/clustering index per file; many secondary indexes
          4. B+ tree: balanced, data in leaves, leaves linked
          5. Order: p·P + (p−1)·K ≤ block size
          6. SQL = fixed schema + ACID. NoSQL = flexible schema + BASE
          7. CAP: pick two; P is unavoidable so choose CP or AP

          Practice questions

          Question 1 · MCQ (GATE / semester)

          Which index has one entry for every record?

          Question 2 · MCQ (GATE)

          In a B+ tree, record pointers are stored in:

          Question 3 · MCQ (GATE)

          A file can have at most how many primary (or clustering) indexes?

          Question 4 · MCQ (Interview)

          A system that is always available but may return old data during a network partition is:

          Question 5 · MCQ (GATE / semester)

          Which is an advantage of a B+ tree over a B tree?