Skip to main content

Command Palette

Search for a command to run...

SQL Indexing: The 7.8s → 120ms Performance Upgrade

Published
3 min readView as Markdown
SQL Indexing: The 7.8s → 120ms Performance Upgrade
V

I’m a senior full-stack developer working across system design, DevOps, and AI. I build scalable systems, optimize performance, and explore emerging tech. I’ve written “Machine Learning in iOS” and am now writing a new book on system design, DevOps, and ML. This blog is where I share what I learn, build, and discover.

1️⃣ The Real Production Failure (Hook)

A few months ago, one of my production APIs suddenly slowed to 11–13 seconds.

Users complained.
CPU spiked.
Servers looked overloaded.

The query looked harmless:

SELECT * FROM orders WHERE user_id = 19283;

The root cause?

No index on user_id.

One missing index nearly broke the entire product.

That’s when indexing stopped being “database theory” and became production survival.

2️⃣ What Actually Went Wrong? (Visual Explanation)

❌ Without Index (Full Table Scan)

Request → DB → Scans 10M rows → Finds 1 row → Response
             ⏱ 11 seconds
             🔥 High CPU

✅ With Index (Direct Lookup)

Request → DB → Index Lookup → Fetch 1 row → Response
             ⏱ 120ms
             ❄️ Low CPU

Indexes turn O(N) scans into O(log N) lookups.

That’s the difference between slow apps and scalable systems.

3️⃣ What Is an Index? (Human Explanation)

Think of a book:

  • ❌ No index → You scan every page

  • ✅ With index → You jump directly to the topic

A database index works exactly the same way.

It stores:

Value → Row Location

So the database doesn’t search — it jumps directly.

4️⃣ When You MUST Create an Index

Create indexes on columns used in:

WHERE
JOIN
ORDER BY
GROUP BY
✅ Filtering dashboards
✅ Search APIs

Rule of Thumb:

If a column is used to find something, it needs an index.

5️⃣ The Most Common Index

CREATE INDEX idx_orders_user_id
ON orders(user_id);

This makes:

SELECT * FROM orders WHERE user_id = 10;

20x–100x faster instantly

6️⃣ Composite Index (Multi-Column)

If your query is:

SELECT * FROM orders 
WHERE user_id = 10 AND status = 'completed';

Create:

CREATE INDEX idx_orders_user_status
ON orders(user_id, status);

⚠️ Order Matters

  • Most selective column first

  • Avoid random combinations

7️⃣ When You Should NOT Index

Do NOT index:

❌ Boolean fields (true/false)
❌ Low-cardinality columns
❌ Highly updated columns
❌ Unused composite indexes

Too many indexes = slower writes + more storage + more memory.

8️⃣ Real Fintech Case Study

A dashboard query filtered by:

  • user_id

  • status

  • transaction_date

It took:

7.8 seconds

Correct index:

CREATE INDEX idx_tx_user_status_date
ON transactions(user_id, status, transaction_date);

After indexing:

7.8s → 120ms

The dashboard instantly felt real-time.

9️⃣ How to Find Missing Indexes (Pro Tip)

Always analyze slow queries using:

EXPLAIN ANALYZE

This instantly shows:

  • Full table scan ❌

  • Index scan ✅

  • Rows scanned

  • Execution time

🔥 6 Production-Proven Indexing Rules

✅ Index columns in WHERE, JOIN, ORDER BY
✅ Use composite indexes for multi-filter queries
✅ Avoid indexing low-cardinality fields
✅ Keep composite indexes minimal
✅ Too many indexes slow down writes
✅ Always verify using EXPLAIN ANALYZE

🧠 Final Takeaway

Indexes are the single biggest performance multiplier in SQL.

Most “scaling issues” are not infrastructure problems.

They are:
❌ Missing indexes
❌ Poor query design
❌ Weak database planning

One index can:

  • Save servers

  • Reduce costs

  • Fix slow APIs

  • Save your startup

🏁 Mini Challenge (For You)

1️⃣ Run EXPLAIN on your slowest API query
2️⃣ Add one correct index
3️⃣ Re-run EXPLAIN
4️⃣ Watch execution time drop

Backend magic in 5 minutes.