RezanRezan
← All writing
Performance

Why Your Database Is Still Slow Even With Indexes?

Hi everyone! 👋 If you’ve never heard the sentence "We added an index but the query is still slow" in production, you’re either very lucky—or you haven’t spent enough time in…

In this article↗

5 Common Production Mistakes

Codes

Hi everyone! 👋

If you’ve never heard the sentence "We added an index but the query is still slow" in production, you’re either very lucky—or you haven’t spent enough time in production yet 🙂

In this article, we’ll walk through the 5 most common mistakes that cause slow queries even when indexes exist, using a reproducible example with synthetic orders.

An Order History Example

Imagine an orders table with millions of rows:

sql
CREATE INDEX idx_orders_user_created_at
ON orders(user_id, created_at);

Suppose a customer opens their order history:

sql
SELECT *
FROM orders
WHERE DATE(created_at) = '2026-01-24'
AND user_id = 42;

The index exists. But the query is slow.

Why?

1. Checking Which Predicates Reach the Index ❌

PostgreSQL can still use the user_id prefix of this index. Applying DATE() changes how it uses the second column. In a 100,000-row sample run with PGlite, the index located 1,000 orders for one customer. A separate date filter discarded 985, leaving 15.

The range query below returned the same 15 order IDs. This time both timestamp bounds appeared in the index condition. The example uses timestamps without time zones; for a local calendar day, choose the bounds in the intended time zone.

Better approach: ☑️

sql
SELECT *
FROM orders
WHERE created_at >= '2026-01-24'
AND created_at < '2026-01-25'
AND user_id = 42;

2. Incorrect Index Column Order ❌

Read the key order alongside the query.

Index:

(user_id, created_at)

Here the query fixes one customer and searches a time range within that customer's orders. A query for all customers on one day has a different access pattern. Compare plans for both workloads before choosing the column order.


3. Low Selectivity Columns ❌

Columns like status or is_active often have the same value for most rows.

sql
WHERE status = 'ACTIVE'

Rule of Thumb 🥇

A selective predicate matches a small share of the rows.

For a common status, a table scan can be cheaper. A rare status on the same table can produce a different plan.

4. Blind Trust in ORMs ❌

ORMs don’t always generate optimal SQL:

  • Unnecessary joins
  • Over-fetching columns
  • N+1 query problems

Rule of Thumb 🥇 Teams that don’t inspect generated SQL often detect performance issues too late.

5. Not Using EXPLAIN ANALYZE ❌

Adding an index is not enough. Always ask:

Is the database actually using this index?

sql
EXPLAIN (ANALYZE, BUFFERS)
SELECT * FROM orders WHERE user_id = 42;

This is where most production performance problems become visible.

Final Thoughts

  • More indexes don’t guarantee better performance
  • A wrong index can be worse than no index at all
  • Queries and indexes must be designed together

If this topic caught your interest, feel free to explore more articles on the blog. Hope it’s useful 🙂

A little more about me