<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/when-postgres-picks-the-wrong-index-and-how-to-fix-it--fqpb6k8mg" -->

---
title: When Postgres Picks the Wrong Index (And How to Fix It)
description: A worked Postgres experiment shows how adding a new index can make an existing query slower rather than faster. With only a user_id index, a query fetching a...
canonical: https://daily.dev/posts/when-postgres-picks-the-wrong-index-and-how-to-fix-it--fqpb6k8mg
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: When Postgres Picks the Wrong Index (And How to Fix It) | daily.dev
og:description: A worked Postgres experiment shows how adding a new index can make an existing query slower rather than faster. With only a user_id index, a query fetching a...
og:url: https://daily.dev/posts/when-postgres-picks-the-wrong-index-and-how-to-fix-it--fqpb6k8mg
og:image: https://api.daily.dev/og/posts/fqpB6k8Mg.png
og:image:alt: When Postgres Picks the Wrong Index (And How to Fix It)
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# When Postgres Picks the Wrong Index (And How to Fix It)

**[Milan Jovanović](https://daily.dev/sources/milanjovanovic)** · 7 min read · 8 upvotes · 0 comments

## Summary

A worked Postgres experiment shows how adding a new index can make an existing query slower rather than faster. With only a user_id index, a query fetching a user's ten most recent comments runs in 16.5ms. Adding an index on created_at DESC gives the planner a tempting but wrong path: it scans newest-first and discards 495,944 rows before finding the target user's comments, ballooning execution time to 241ms. A composite index on (user_id, created_at DESC) fixes it, dropping execution time to 0.067ms because it supplies both the filter and the ordering. The piece walks through EXPLAIN ANALYZE output, planner cost estimates, and buffer statistics to explain why the planner misjudged the cost, and recommends comparing query plans before and after adding indexes using representative parameter values.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://milanjovanovic.tech/blog/when-postgres-picks-the-wrong-index>

## Questions this post answers

### Why did adding a new index make my Postgres query slower instead of faster?

A new index gives the query planner another execution path, and for ORDER BY with LIMIT queries it can look cheap because Postgres expects to find enough matching rows early in the index order. If those matches actually occur far into the index, filtering them out costs more than fetching and sorting through the original index. In one measured case, execution time went from 16.5ms to 241ms after adding an index on created_at DESC, because the scan discarded 495,944 non-matching rows before finding the needed ten.

_daily.dev surfaces practical postgres indexing writeups for developers debugging plan regressions like this._

### How do I fix a Postgres query plan that shows a large 'Rows Removed by Filter' count?

Build a composite index that puts the equality condition first and the sort column second, so the index both filters and preserves the needed order. For a query filtering by user_id and ordering by created_at DESC with LIMIT 10, an index on (user_id, created_at DESC) let Postgres read ten entries and stop, cutting execution time from 241ms to 0.067ms compared to a date-only index.

_Developers tuning composite indexes can find similar postgres performance breakdowns on daily.dev._

### Will running ANALYZE fix a bad Postgres query plan caused by a new index?

Not necessarily. Stale statistics can cause bad plan estimates, but running VACUUM ANALYZE immediately after seeding data still produced a bad plan in one test, because ordinary per-column statistics don't capture where a specific user's rows fall in a different column's sort order. The fix required a composite index rather than re-running ANALYZE.

_daily.dev helps developers dig into postgres planner quirks like this before they hit production._

## Similar posts on daily.dev

- [PostgreSQL Query Optimization: The Index That Made Our Fastest Query 40 Times Slower](https://daily.dev/posts/postgresql-query-optimization-the-index-that-made-our-fastest-query-40-times-slower-w73b0y9pb) · Medium · 1 upvotes · 0 comments
- [SQL Indexing Explained: Composite Indexes and Column Order](https://daily.dev/posts/sql-indexing-explained-composite-indexes-and-column-order-exxme3lqv) · Milan Jovanović · 42 upvotes · 1 comments
- [How to optimize when you can’t do anything\!](https://daily.dev/posts/how-to-optimize-when-you-can-t-do-anything--31j5al2rl) · Planet PostgreSQL · 0 upvotes · 0 comments
- [But I Added an Index\! Why Is My SQL Query Still Slow?](https://daily.dev/posts/but-i-added-an-index-why-is-my-sql-query-still-slow--tzn94gf7j) · Medium · 0 upvotes · 0 comments

---

Tags: [#database](https://daily.dev/tags/database), [#sql](https://daily.dev/tags/sql), [#postgresql](https://daily.dev/tags/postgresql)

[View this post on daily.dev](https://daily.dev/posts/when-postgres-picks-the-wrong-index-and-how-to-fix-it--fqpb6k8mg)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"When Postgres Picks the Wrong Index (And How to Fix It)","url":"https://daily.dev/posts/when-postgres-picks-the-wrong-index-and-how-to-fix-it--fqpb6k8mg","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/when-postgres-picks-the-wrong-index-and-how-to-fix-it--fqpb6k8mg"},"datePublished":"2026-09-18T20:30:29.390Z","dateModified":"2026-09-18T20:30:51.894Z","description":"A worked Postgres experiment shows how adding a new index can make an existing query slower rather than faster. With only a user_id index, a query fetching a...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/533bc376b183c4a6ec139f0028c0bccc?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/533bc376b183c4a6ec139f0028c0bccc?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Milan Jovanović","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Milan Jovanović","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/78d8abfb692746538410dee47f93e566","url":"https://daily.dev/sources/milanjovanovic"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/when-postgres-picks-the-wrong-index-and-how-to-fix-it--fqpb6k8mg","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":8},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"database,sql,postgresql","timeRequired":"PT7M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Milan Jovanović","item":"https://daily.dev/sources/milanjovanovic"},{"@type":"ListItem","position":3,"name":"When Postgres Picks the Wrong Index (And How to Fix It)"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/when-postgres-picks-the-wrong-index-and-how-to-fix-it--fqpb6k8mg#faq","mainEntity":[{"@type":"Question","name":"Why did adding a new index make my Postgres query slower instead of faster?","acceptedAnswer":{"@type":"Answer","text":"A new index gives the query planner another execution path, and for ORDER BY with LIMIT queries it can look cheap because Postgres expects to find enough matching rows early in the index order. If those matches actually occur far into the index, filtering them out costs more than fetching and sorting through the original index. In one measured case, execution time went from 16.5ms to 241ms after adding an index on created_at DESC, because the scan discarded 495,944 non-matching rows before finding the needed ten. daily.dev surfaces practical postgres indexing writeups for developers debugging plan regressions like this."}},{"@type":"Question","name":"How do I fix a Postgres query plan that shows a large 'Rows Removed by Filter' count?","acceptedAnswer":{"@type":"Answer","text":"Build a composite index that puts the equality condition first and the sort column second, so the index both filters and preserves the needed order. For a query filtering by user_id and ordering by created_at DESC with LIMIT 10, an index on (user_id, created_at DESC) let Postgres read ten entries and stop, cutting execution time from 241ms to 0.067ms compared to a date-only index. Developers tuning composite indexes can find similar postgres performance breakdowns on daily.dev."}},{"@type":"Question","name":"Will running ANALYZE fix a bad Postgres query plan caused by a new index?","acceptedAnswer":{"@type":"Answer","text":"Not necessarily. Stale statistics can cause bad plan estimates, but running VACUUM ANALYZE immediately after seeding data still produced a bad plan in one test, because ordinary per-column statistics don't capture where a specific user's rows fall in a different column's sort order. The fix required a composite index rather than re-running ANALYZE. daily.dev helps developers dig into postgres planner quirks like this before they hit production."}}]}
```

