Lesson 6 � Intermediate

DBMS: B-Tree Indexing

Indexing database queries ko fast banata hai. B-Tree aur B+ Tree � ye data structures indexing ke liye use hote hain. Interview mein frequently poochte hain.

? 22 min✓ Intermediate✓ DBMS basics

Indexing hota kya hai?

WHAT

Index ek data structure hai jo database ko data locate karne mein madad karta hai. Bina index ke � full table scan (har row check karo). Index se � seedha jao jahan data hai. Jaise book ka index � topic dhundhne ke liye last page nahi dekhna padta.

WHEN

jab frequently columns pe WHERE, JOIN, ORDER BY karte ho. "SELECT * FROM users WHERE email = 'x'" � email pe index laga do � query 100x fast.

WHERE

Primary Key automatically indexed hota hai. Foreign keys pe index lagao. High-cardinality columns (unique values zyada) pe index effective hai.

# Without Index: Full Table Scan
SELECT * FROM users WHERE email = 'vaibhav@example.com';
? 1 million rows scan karega ✓ SLOW! (O(n))

# With Index on email:
✓ B-Tree mein email sorted hai
✓ Binary search lagao ✓ O(log n)
✓ Seedha row pe pahuncho ✓ FAST!

# Index Types:
1. Primary Index: Primary Key pe automatically
2. Secondary Index: Manually created on columns
3. Clustered Index: Physical order of data = index order
4. Non-Clustered Index: Separate index structure + pointer to data

B-Tree

# B-Tree: Balanced Tree for disk-based storage

# Properties:
1. Har node mein multiple keys (not binary)
2. All leaf nodes at same level (balanced)
3. Keys sorted in each node
4. Order m = maximum children per node

# B-Tree of order 3:
 [10 | 20]
 / | \
 [5|8] [12|15] [25|30]
 / | \ / | \ / | \
 [1-4][6-7][9][11][13-14][16-19][21-24][26-29][31+]

# Search: O(log m n)
✓ Root se start, key dhundho, child pe jao
✓ Har level pe kuch comparisons
✓ Height bahut chhoti hoti hai (3-4 levels for millions of rows)

# Insert: 
✓ Key add karo, agar node full hai toh split karo
✓ Balance maintain hota hai automatically

# Delete:
✓ Key remove karo, agar node underflow hai toh merge karo
✓ Balance maintain hota hai
Mental Model: B-Tree jaise dictionary hai. Tumhe word "mountain" dhundhna hai. Pehle M section mein jaao, phir MO section, phir MOU � har step mein range reduce hoti hai. Ek baar section mil jaaye toh page pe binary search karo. Bahut fast hai!

B+ Tree (More Common)

# B+ Tree = B-Tree ka improved version

# Key Differences:
1. Data sirf leaf nodes mein hota hai
2. Internal nodes sirf keys rakhte hain (routing)
3. Leaf nodes linked hain (range queries easy)
4. Search always leaf tak jaata hai

# Structure:
 [10 | 20] ✓ Internal (routing only)
 / | \
 [5|8] [12|15] [25|30] ✓ Internal
 / | \ / | \ / | \
 [1-4]?[6-7]?[9]?[11]?[13-14]?[16-19]?[21-24]?[26-29]✓ Leaf (data + links)

# Why B+ Tree over B-Tree?
✓ More keys per internal node (no data) ✓ shorter tree
✓ Range queries easy (linked leaf nodes)
✓ Consistent search time (always leaf)
✓ Better cache performance

# Most databases use B+ Tree:
- MySQL InnoDB
- PostgreSQL
- Oracle

Index Types

# 1. Clustered Index
✓ Physical order of data = index order
✓ Table mein sirf EK clustered index ho sakta hai
✓ Usually Primary Key
✓ Data rows stored in index order

# 2. Non-Clustered Index
✓ Separate structure with pointer to data
✓ Table mein multiple non-clustered indexes ho sakte hain
✓ Index leaf node mein row pointer hota hai
✓ Extra step: Index ✓ Pointer ✓ Actual Data

# 3. Covering Index
✓ Index mein saare columns hain jo query chahiye
✓ Table access ki zaroorat nahi (index se data milega)
? "SELECT email FROM users WHERE email = 'x'"
✓ email index = covering index

# 4. Composite Index
✓ Multiple columns pe combined index
✓ CREATE INDEX idx ON users(name, email)
✓ Leftmost prefix rule: name pe pehle, phir name+email

# Indexing Best Practices:
✓ WHERE clause ke columns pe index
✓ JOIN columns pe index
✓ ORDER BY columns pe index
✓ Small tables pe index mat lagao (overhead zyada)
✓ Har column pe index mat lagao (write slow)

Exercise

Question: B-Tree mein search ka time complexity kya hota hai? (2 words ya O notation)

Question: Ek table mein kitne clustered indexes ho sakte hain maximum? (1 word)

Common mistakes

Lesson complete?

B-Tree Indexing samajh aa gayi✓ Ab Transactions & ACID seekhte hain � database consistency kaise maintain hoti hai.