Back to course
    article~27 min readDatabase Design

    Indexing, SQL Vs NoSQL

    Balance query power, scale, and data modeling trade-offs.

    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:

    FieldWhy
    emailLogin lookup
    phone_numberAuthentication
    order_idFast order retrieval
    product_idEcommerce lookup
    created_atSorting/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

    ProblemWhy Happens
    Extra storageIndexes consume memory
    Slower writesIndex updates required
    More complexityQuery 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_idnamecity
    1RahulJaipur
    2AmanDelhi

    Popular SQL Databases

    DatabaseUsage
    PostgreSQLSaaS apps
    MySQLWebsites
    OracleBanking
    SQL ServerEnterprises

    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

    PropertyMeaning
    AtomicityAll or nothing
    ConsistencyValid state maintained
    IsolationConcurrent transactions safe
    DurabilityData 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

    DatabaseUsage
    MongoDBFlexible apps
    CassandraMassive scale
    DynamoDBCloud systems
    RedisCaching
    ElasticsearchSearch

    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

    SQLNoSQL
    Structured schemaFlexible schema
    Strong consistencyHigh scalability
    TablesDocuments / Key-value
    Complex joinsFaster distributed writes
    ACID transactionsEventual 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

    CompanyDatabase Usage
    PhonePeSQL for payments
    ZerodhaSQL for trading
    SwiggySQL + Redis
    InstagramSQL + NoSQL
    NetflixDistributed 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

    RequirementBetter Database
    TransactionsPostgreSQL
    CachingRedis
    SearchElasticsearch
    AnalyticsClickHouse

    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.

    Mark this lesson complete when you finish reviewing it.