---
title: "Discussion on \"My PostgreSQL query went from 57ms to 1.4ms on a 1 million +  row table. I didn't change the query. Here's what I did.\" | Hashnode"
description: "Discussion on \"My PostgreSQL query went from 57ms to 1.4ms on a 1 million +  row table. I didn't change the query. Here's what I did.\". Learn how to use EXPLAIN ANALYZE to find slow PostgreSQL queries, when to add an index, and why over-indexing hurts."
url: "https://hashnode.com/posts/my-postgresql-query-went-from-57ms-to-1-4ms-on-a-1-million-row-table-i-didn-t-change-the-query-here-s-what-i-did/6a2da274457244362172f9f5"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [My PostgreSQL query went from 57ms to 1.4ms on a 1 million + row table. I didn't change the query. Here's what I did.](https://sharafath.hashnode.dev/my-postgresql-query-went-from-57ms-to-1-4ms-on-a-1-million-row-table-i-didn-t-change-the-query-here-s-what-i-did?utm_source=hashnode&utm_medium=feed)

Learn how to use EXPLAIN ANALYZE to find slow PostgreSQL queries, when to add an index, and why over-indexing hurts.

Jun 13 · 10 min read · sharafath.hashnode.dev  
Read the article

-   [#indexing](https://hashnode.com/tag/indexing)
-   [#sql](https://hashnode.com/tag/sql)
-   [#postgresql](https://hashnode.com/tag/postgresql)
-   [#query-optimization](https://hashnode.com/tag/query-optimization)
-   [#backend](https://hashnode.com/tag/backend)
-   [#api](https://hashnode.com/tag/api)
-   [#api-basics](https://hashnode.com/tag/api-basics)
-   [#apis](https://hashnode.com/tag/apis)
-   [#api-gateway](https://hashnode.com/tag/api-gateway)
-   [#nodejs](https://hashnode.com/tag/nodejs)
-   [#reactjs](https://hashnode.com/tag/reactjs)
-   [#frontend-development](https://hashnode.com/tag/frontend-development)
-   [#frontend](https://hashnode.com/tag/frontend)
-   [#mern](https://hashnode.com/tag/mern)
-   [#full-stack](https://hashnode.com/tag/full-stack)
-   [#databases](https://hashnode.com/tag/databases)
-   [#database](https://hashnode.com/tag/database)

## Written by

[Sharafath Ali](https://hashnode.com/@sharafath)

@sharafath · india

  
[View profile](https://hashnode.com/@sharafath)

Code, Coffee, and Creativity – Full-Stack Developer on a Mission!

I'm a Full-Stack Developer with 3+ years of experience building scalable applications and solving real-world engineering challenges. Here, I write about software development, backend systems, DevOps, cloud technologies, system design, and lessons learned from building production-ready software.

## Responses 4

Markdown supported · ⌘↵ to post

[databas](https://hashnode.com/@jkjhiik)

[Jun 20](https://hashnode.com/posts/my-postgresql-query-went-from-57ms-to-1-4ms-on-a-1-million-row-table-i-didn-t-change-the-query-here-s-what-i-did/6a2da274457244362172f9f5/comment/6a3722c608769ccc8d2b490a)

did u check if the planner just cached the plan after an analyze run. its wierd how often that happens untill u realize the statistics were just stale and needed a quick refresh.

[Sharafath Ali](https://hashnode.com/@sharafath)

[Jun 20](https://hashnode.com/posts/my-postgresql-query-went-from-57ms-to-1-4ms-on-a-1-million-row-table-i-didn-t-change-the-query-here-s-what-i-did/6a2da274457244362172f9f5/comment/6a3722c608769ccc8d2b490a#reply-6a37250117713937ee285bfd)

nice point. I actually ran "ANALYZE users;" and executed the query multiple times before taking the before/after samples. Initially the timings were a bit higher and varied, but after refreshing statistics and running the query a few times, they stabilized around the numbers I used. The earlier variance may have been due to stale PostgreSQL statistics and cache warming effects.

[GameNetic](https://hashnode.com/@daddy69)

[Jun 14](https://hashnode.com/posts/my-postgresql-query-went-from-57ms-to-1-4ms-on-a-1-million-row-table-i-didn-t-change-the-query-here-s-what-i-did/6a2da274457244362172f9f5/comment/6a2ec4d7616dd58b7baba9f3)

It's an informative article. Thanks for posting it man

[Sharafath Ali](https://hashnode.com/@sharafath)

[Jun 14](https://hashnode.com/posts/my-postgresql-query-went-from-57ms-to-1-4ms-on-a-1-million-row-table-i-didn-t-change-the-query-here-s-what-i-did/6a2da274457244362172f9f5/comment/6a2ec4d7616dd58b7baba9f3#reply-6a2ec7ff6c630fb5e12f9470)

❤️❤️
