<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/making-a-postgres-query-1-000-times-faster-x2eh9cpkk" -->

---
title: Making a Postgres query 1,000 times faster | daily.dev
description: 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...
canonical: https://daily.dev/posts/making-a-postgres-query-1-000-times-faster-x2eh9cpkk
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Making a Postgres query 1,000 times faster | daily.dev
og:description: 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...
og:url: https://daily.dev/posts/making-a-postgres-query-1-000-times-faster-x2eh9cpkk
og:image: https://api.daily.dev/og/posts/x2EH9CPkK.png
og:image:alt: Making a Postgres query 1,000 times faster
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.

# 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!

---

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)

```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":"Making a Postgres query 1,000 times faster","url":"https://daily.dev/posts/making-a-postgres-query-1-000-times-faster-x2eh9cpkk","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/making-a-postgres-query-1-000-times-faster-x2eh9cpkk"},"datePublished":"2024-05-15T21:48:48.714Z","dateModified":"2024-05-15T21:48:46.831Z","description":"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...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/0ed8de85951f44155517e9095a89c798?_a=AQAEuiZ","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/0ed8de85951f44155517e9095a89c798?_a=AQAEuiZ","isAccessibleForFree":true,"articleSection":"Hacker News","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":"Hacker News","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/hn","url":"https://daily.dev/sources/hn"},"commentCount":10,"discussionUrl":"https://daily.dev/posts/making-a-postgres-query-1-000-times-faster-x2eh9cpkk","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":383},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":10}],"keywords":"database,postgresql,elk","timeRequired":"PT16M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Hacker News","item":"https://daily.dev/sources/hn"},{"@type":"ListItem","position":3,"name":"Making a Postgres query 1,000 times faster"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/making-a-postgres-query-1-000-times-faster-x2eh9cpkk","comment":[{"@type":"Comment","text":"A worth reading article!\nMy TLDR: In Postgres you can replace the condition:\n(CreateAt &gt; ?1 OR (CreateAt = ?1 AND Id &gt; ?2))\nwith this (CreateAt, Id) &gt; (?1, ?2) to use the index. It’s called “lexicographical comparisons”.","datePublished":"2024-05-22T10:21:33.384Z","url":"https://daily.dev/posts/x2EH9CPkK#c-JbyCKLs8t","author":{"@type":"Person","name":"Eugene","url":"https://daily.dev/winnix","image":"https://media.daily.dev/image/upload/s--wipmeGgH--/f_auto/v1716373478/avatars/avatar_al3myC1d4QbgyRkaWBvee"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":2}},{"@type":"Comment","text":"awesome post!","datePublished":"2024-05-16T12:24:17.507Z","url":"https://daily.dev/posts/x2EH9CPkK#c-ekoi4e2sg","author":{"@type":"Person","name":"Filip Stojakovic","url":"https://daily.dev/filipstojakovic","image":"https://avatars.githubusercontent.com/u/32577154?v=4"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1}},{"@type":"Comment","text":"Really interesting one, Thanks for sharing this awesome post","datePublished":"2024-05-30T03:07:48.163Z","url":"https://daily.dev/posts/x2EH9CPkK#c-nqN5CIV4A","author":{"@type":"Person","name":"Rahul Jindal","url":"https://daily.dev/rahuljindal234","image":"https://lh3.googleusercontent.com/a/ALm5wu2RIeDG7fdKqCU0lc9TbITpbOsTdoZCi-mww8eu=s96-c"}},{"@type":"Comment","text":"Amazing !","datePublished":"2024-05-21T08:35:51.740Z","dateModified":"2024-05-21T08:36:07.967Z","url":"https://daily.dev/posts/x2EH9CPkK#c-YYOCiZzcU","author":{"@type":"Person","name":"Hữu Đoàn Đức","url":"https://daily.dev/ddhuu","image":"https://lh3.googleusercontent.com/a/ACg8ocL1miEsh1RRjr5XUwwje8cW1sl9xhlmqKIXNhH-LkKLZX909g=s96-c"}},{"@type":"Comment","text":"Excellent write up of the problem and the outcome. Took a few good tips away from this. Nice work!","datePublished":"2024-05-16T15:09:23.889Z","url":"https://daily.dev/posts/x2EH9CPkK#c-gk3SRMvOz","author":{"@type":"Person","name":"Michael","url":"https://daily.dev/mmbmf1","image":"https://media.daily.dev/image/upload/s--O0TOmw4y--/f_auto/v1715772965/public/noProfile"}}]}
```

