---
title: "Making a Postgres query 1,000 times faster"
url: https://daily.dev/posts/making-a-postgres-query-1-000-times-faster-x2eh9cpkk
source_url: https://mattermost.com/blog/making-a-postgres-query-1000-times-faster/
type: article
source: "Hacker News"
published: 2024-05-15T21:48:48.714Z
updated: 2024-05-15T21:48:46.831Z
tags: ["database", "postgresql", "elk"]
reading_time: 16
upvotes: 383
comments: 10
language: 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.

# Making a Postgres query 1,000 times faster

**[Hacker News](https://daily.dev/sources/hn)** · 16 min read · 383 upvotes · 10 comments

## Summary

The author shares their journey of optimizing a Postgres query to make it 1,000 times faster. They discovered that the query was taking longer and longer each time it was executed due to processing all rows in the table and the use of a filter instead of an index condition. By using row constructor comparisons, they were able to significantly improve the query performance.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://mattermost.com/blog/making-a-postgres-query-1000-times-faster/>

## Community discussion

Top comments from developers on daily.dev.

**@winnix** · 2 upvotes

> A worth reading article!
>
> My TLDR: In Postgres you can replace the condition:
> `(CreateAt > ?1 OR (CreateAt = ?1 AND Id > ?2))`
> with this `(CreateAt, Id) > (?1, ?2)` to use the index. It's called "lexicographical comparisons".

**@filipstojakovic** · 1 upvotes

> awesome post!

**@rahuljindal234** · 0 upvotes

> Really interesting one, Thanks for sharing this awesome post

**@ddhuu** · 0 upvotes

> Amazing !

**@mmbmf1** · 0 upvotes

> Excellent write up of the problem and the outcome. Took a few good tips away from this. Nice work!

## Similar posts on daily.dev

- [Don’t just attend KubeCon \+ CloudNativeCon, Merge Forward your experience\!](https://daily.dev/posts/don-t-just-attend-kubecon-cloudnativecon-merge-forward-your-experience--l0rpp73x8) · CNCF · 0 upvotes · 0 comments
- [Announcing H2 2026 KCDs](https://daily.dev/posts/announcing-h2-2026-kcds-m96goajm1) · CNCF · 1 upvotes · 0 comments
- [Two months of Open Community Groups](https://daily.dev/posts/two-months-of-open-community-groups-asf52zhbs) · CNCF · 0 upvotes · 0 comments

---

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

[View this post on daily.dev](https://daily.dev/posts/making-a-postgres-query-1-000-times-faster-x2eh9cpkk)
