Indexing, SQL Vs NoSQL
Balance query power, scale, and data modeling trade-offs.
Curriculum
System Design Foundations
Scalability Basics
Database Design
Indexing, SQL Vs NoSQL — Beginner Friendly System Design Guide
Why Databases Become Slow
Imagine Flipkart database contains:
50 crore products
Now user searches:
iPhone 16
Without optimization:
- database may scan entire table
- query becomes very slow
This is where:
Indexing
becomes important.
At the same time, system architects must also decide:
Should we use SQL or NoSQL?
This lesson explains:
- indexing
- SQL vs NoSQL
- scalability trade-offs
- real production examples
What Is Indexing?
Indexing helps database:
- find data faster
without scanning entire table.
Real-Life Analogy
Imagine book with:
- 1000 pages
Without index:
- finding topic is slow
With index:
- jump directly to correct page
Database indexing works similarly.
Example Without Index
Suppose user logs into Instagram using:
phone number
Database checks:
- row 1
- row 2
- row 3
- millions more
This is called:
Full Table Scan
Very slow.
Example With Index
Database already maintains:
- optimized lookup structure
Now it directly jumps to correct row.
Much faster.
Indexing Flow
User Query
│
▼
Check Index
│
┌──┴─────────┐
▼ ▼
Index Found No Index
│ │
▼ ▼
Fast Lookup Full Table Scan
Real Example — Swiggy Restaurant Search
Suppose Swiggy stores:
1 crore restaurants
User searches:
Pizza in Jaipur
Without indexes:
- search becomes slow
Indexes improve:
- filtering
- sorting
- searching
dramatically.
Commonly Indexed Fields
Usually indexes created on:
| Field | Why |
|---|---|
| Login lookup | |
| phone_number | Authentication |
| order_id | Fast order retrieval |
| product_id | Ecommerce lookup |
| created_at | Sorting/filtering |
Example SQL Query
SELECT * FROM users
WHERE phone_number = '9876543210';
Without index:
- scans entire users table
With index:
- fast lookup
Database Internals Simplified
Indexes often use:
- B-Trees
- Hash structures
to optimize searches.
Simplified Index Structure
Phone Number Index
│
┌─────┼─────┐
▼ ▼ ▼
Row1 Row20 Row5000
Why Indexes Improve Performance
Without indexes:
Time Complexity ≈ O(n)
With indexes:
Time Complexity ≈ O(log n)
Huge improvement at scale.
But Indexes Have Trade-Offs
Indexes are powerful but not free.
Problems With Too Many Indexes
| Problem | Why Happens |
|---|---|
| Extra storage | Indexes consume memory |
| Slower writes | Index updates required |
| More complexity | Query planning harder |
Example — Blinkit Inventory
Inventory updates happen rapidly.
Too many indexes may slow:
- writes
- stock updates
Trade-offs matter.
What Is SQL?
SQL databases store:
- structured data
- rows and columns
like Excel sheets.
Example SQL Table
| user_id | name | city |
|---|---|---|
| 1 | Rahul | Jaipur |
| 2 | Aman | Delhi |
Popular SQL Databases
| Database | Usage |
|---|---|
| PostgreSQL | SaaS apps |
| MySQL | Websites |
| Oracle | Banking |
| SQL Server | Enterprises |
SQL Database Flow
Application
│
▼
SQL Query
│
▼
SQL Database
│
▼
Structured Result
Why SQL Is Popular
SQL provides:
- strong consistency
- transactions
- joins
- structured relationships
Excellent for:
- banking
- ecommerce
- ERP
- financial systems
Real Example — PhonePe
PhonePe payment systems require:
- correct balances
- reliable transactions
- no duplicate deductions
SQL databases are ideal because:
- consistency is critical
ACID Properties
SQL databases often support:
ACID
ACID Means
| Property | Meaning |
|---|---|
| Atomicity | All or nothing |
| Consistency | Valid state maintained |
| Isolation | Concurrent transactions safe |
| Durability | Data survives crashes |
Banking Example
Suppose:
₹500 transferred
Both operations must happen:
- sender debited
- receiver credited
System cannot partially fail.
What Is NoSQL?
NoSQL databases are more flexible.
Schema can vary.
Example Documents
{
"name": "iPhone",
"storage": "256GB"
}
Another document:
{
"name": "T-Shirt",
"size": "XL"
}
Flexible structure.
Popular NoSQL Databases
| Database | Usage |
|---|---|
| MongoDB | Flexible apps |
| Cassandra | Massive scale |
| DynamoDB | Cloud systems |
| Redis | Caching |
| Elasticsearch | Search |
NoSQL Database Flow
Application
│
▼
Document Query
│
▼
NoSQL Database
│
▼
Flexible Result
Why NoSQL Exists
As internet scale exploded:
- SQL databases became difficult to scale infinitely
NoSQL systems optimized for:
- huge scale
- distributed systems
- high write throughput
Real Example — Instagram
Instagram handles:
- billions of likes
- comments
- posts
- reels
NoSQL helps with:
- scalability
- flexibility
- distributed workloads
SQL vs NoSQL Comparison
| SQL | NoSQL |
|---|---|
| Structured schema | Flexible schema |
| Strong consistency | High scalability |
| Tables | Documents / Key-value |
| Complex joins | Faster distributed writes |
| ACID transactions | Eventual consistency common |
What Is Eventual Consistency?
In distributed systems:
- data may not update everywhere instantly
but eventually syncs.
Example — WhatsApp
Message may:
- appear on one device first
- sync to another device later
Acceptable trade-off.
SQL vs NoSQL — Real Production Examples
| Company | Database Usage |
|---|---|
| PhonePe | SQL for payments |
| Zerodha | SQL for trading |
| Swiggy | SQL + Redis |
| SQL + NoSQL | |
| Netflix | Distributed NoSQL systems |
One Application Usually Uses Multiple Databases
Modern architectures rarely use:
- one database only
Example — Ecommerce Architecture
Frontend App
│
▼
Application Server
│
┌────┼────────────┐
▼ ▼
PostgreSQL Redis
Orders Cache
│
▼
Elasticsearch
Search Engine
Why Multiple Databases?
Different problems need different tools.
Example
| Requirement | Better Database |
|---|---|
| Transactions | PostgreSQL |
| Caching | Redis |
| Search | Elasticsearch |
| Analytics | ClickHouse |
Query Power vs Scalability
This is core trade-off.
SQL Advantage
Powerful queries:
- joins
- transactions
- relationships
NoSQL Advantage
Better:
- scalability
- distributed systems
- write-heavy workloads
Real Example — YouTube
YouTube may use:
- SQL for payments/subscriptions
- NoSQL for recommendations
- Redis for caching
because:
- one database cannot optimize everything
Common Beginner Mistakes
Mistake 1 — "NoSQL Is Better"
Wrong.
Different tools solve different problems.
Mistake 2 — Index Every Column
Too many indexes:
- slow writes
- waste memory
Mistake 3 — Premature Scaling
Small startup with:
- 1000 users
does NOT need:
- Cassandra clusters
Simple PostgreSQL may be enough.
How Architects Choose Databases
Good architects ask:
What are the access patterns?
What are the consistency needs?
What is expected scale?
What are latency requirements?
Not:
- which database is trendy.
Final Mental Model
SQL =
Consistency + Structured Queries
NoSQL =
Scalability + Flexibility
Indexes =
Fast Data Lookup
Complete Scalable Data Architecture
Users
│
▼
Application Server
│
┌────┼──────────────┐
▼ ▼
Redis Cache PostgreSQL
│
▼
Elasticsearch
One-Line Interview Definition
Indexing improves database query performance by creating optimized lookup structures, while SQL and NoSQL databases offer different trade-offs between consistency, scalability, flexibility, and query capabilities.
Module context
Choosing the right database, indexing strategies, and the SQL vs NoSQL debate with real production examples from Indian startups.