Prod Squad

Databases: SQL vs NoSQL, indexes, and why queries get slow

How products store data, how to reason about SQL vs NoSQL, and the handful of reasons queries slow down as you grow.

6 min readAll levels#databases#sql#nosql#indexes
Ask about this lesson
On this page

Almost every feature reads or writes data, and many performance problems are really data problems. You won't design schemas yourself, but understanding how databases work lets you ask good questions, spot risky designs, and understand why "just add a filter" can be expensive.

Why interviewers ask this

System design questions always involve data: what you store, how you query it, and how it scales. Interviewers check whether you can reason about choosing a database, understand indexes and query performance at a conceptual level, and think about data growth. In execution rounds, "the app is getting slow" often leads to database discussions.

The framework

1. Relational (SQL) databases

Data lives in tables with rows and columns, linked by keys (orders reference users). Examples: PostgreSQL, MySQL.

  • Strengths: structured data with clear relationships, flexible queries with SQL, and transactions (all-or-nothing updates, essential for money and inventory).
  • Tradeoffs: a fixed schema means changes need migrations; scaling writes beyond one big machine takes work.

2. NoSQL databases

A family of alternatives:

  • Document stores (MongoDB, Firestore): flexible JSON-like documents.
  • Key-value stores (Redis, DynamoDB): very fast lookups by key.
  • Wide-column (Cassandra): huge write volumes, like event logs.
  • Graph databases (Neo4j): relationships, like social networks.
  • Strengths: flexible schemas, horizontal scale, speed for specific access patterns.
  • Tradeoffs: weaker support for complex queries and joins; consistency guarantees vary.

Rule of thumb: start with SQL for core business data (users, orders, payments). Add specialised stores when a specific need appears: a cache, search, analytics, or massive event volume.

3. Indexes: why lookups are fast (or slow)

An index is like the index at the back of a book. Without one, the database scans every row to find matches, which is fine for 1,000 rows and painful for 100 million.

flowchart LR
    Q["Find orders for user 42"] --> I{"Index on user_id?"}
    I -->|yes| F["Jump straight to the rows<br/>milliseconds"]
    I -->|no| S["Scan every order<br/>seconds as data grows"]

Indexes speed up reads but slow down writes slightly and use storage, so teams add them for common queries rather than every column.

4. Why queries get slow

  • Missing indexes on columns used for filtering or sorting.
  • Data growth: a query that scanned 10,000 rows now scans 10 million.
  • Expensive joins across large tables.
  • N+1 queries: loading a list, then making one extra query per item.
  • Heavy analytics on the production database, competing with user traffic.
  • Lock contention: many writes to the same rows.

Fixes include adding indexes, caching frequent reads, read replicas, pagination, moving analytics to a separate warehouse, and denormalising (storing precomputed data).

Worked example

Question: "Our order history page takes 8 seconds to load for long-time customers. How would you work with engineering to fix it?"

Sample answer:

"First I'd understand the pattern: it's slow for long-time customers, which suggests the cost grows with the amount of data per user.

Questions I'd ask engineering:

  • What does the page query? If it loads all of a customer's orders at once, users with 500 orders pay the price.
  • Is there an index on the user ID and order date? Without it, every load scans a huge orders table.
  • Are we making a separate query for each order's items or restaurant details (the N+1 pattern)?

Likely fixes, cheapest first:

  1. Pagination: show the 20 most recent orders, with 'load more'. Few users scroll past the first page anyway, and I'd check that in analytics.
  2. An index on (user_id, created_at), which matches exactly how the page queries.
  3. Batch the related lookups instead of one query per order.
  4. Cache the first page for frequent visitors if it's still slow.

Product angle: I'd set a target, such as p95 load under one second, and measure before and after. I'd also ask whether the page needs all that data at all. For example, can older orders show a lighter summary?"

Common mistakes

  • "NoSQL scales better, so use NoSQL" without considering query needs and transactions.
  • Ignoring data growth when estimating performance.
  • Running analytics queries on the production database.
  • Treating indexes as free. They have write and storage costs.
  • Jumping to complex fixes (sharding) before simple ones (pagination, indexes).

Interviewer tips

What "great" sounds like:

  • "For money and inventory, I want transactions, so a relational database is the default."
  • "It's slow for long-time users, which suggests it scales with data per user."
  • "Is there an index that matches how this page queries?"
  • "Pagination is the cheapest fix, and it's better UX too."
  • "I'd keep analytics off the production database."

Practice questions

Answer one in a mock interview and get graded instantly.

  1. 1.Our order history page takes 8 seconds to load for long-time customers. How would you work with engineering to fix it?

    Practice this →
  2. 2.When would you choose a NoSQL database over a relational one for a new feature?

    Practice this →
  3. 3.Explain a database index to a non-technical stakeholder.

    Practice this →

Finished reading?