PostgreSQL Join Optimization: Nested Loop, Hash, and Merge

📰 Dev.to · Philip McClarence

Learn how PostgreSQL optimizes joins using Nested Loop, Hash, and Merge algorithms to improve query performance

intermediate Published 28 Apr 2026
Action Steps
  1. Analyze the query plan using EXPLAIN to identify the join algorithm used
  2. Configure work_mem to optimize hash join performance
  3. Use indexes to improve sort performance for merge joins
  4. Test and compare the performance of different join algorithms
  5. Optimize queries to reduce the number of joins and improve overall performance
Who Needs to Know This

Database administrators and developers who work with PostgreSQL can benefit from understanding how to optimize joins for better performance

Key Insight

💡 Understanding the factors that influence the choice of join algorithm can help improve query performance

Share This
🚀 Improve PostgreSQL query performance by optimizing joins with Nested Loop, Hash, and Merge algorithms! 📈

Key Takeaways

Learn how PostgreSQL optimizes joins using Nested Loop, Hash, and Merge algorithms to improve query performance

Full Article

Title: PostgreSQL Join Optimization: Nested Loop, Hash, and Merge

URL Source: https://dev.to/philip_mcclarence_2ef9475/postgresql-join-optimization-nested-loop-hash-and-merge-1cn9

Published Time: 2026-04-28T14:00:09Z

Markdown Content:
[Skip to content](https://dev.to/philip_mcclarence_2ef9475/postgresql-join-optimization-nested-loop-hash-and-merge-1cn9#main-content)

[![Image 1: DEV Community](https://media2.dev.to/dynamic/image/quality=100/https://dev-to-uploads.s3.amazonaws.com/uploads/logos/resized_logo_UQww2soKuUsjaOGNB38o.png)](https://dev.to/)

[Powered by Algolia](https://www.algolia.com/developers/?utm_source=devto&utm_medium=referral)

[Log in](https://dev.to/enter?signup_subforem=1)[Create account](https://dev.to/enter?signup_subforem=1&state=new-user)

## DEV Community

![Image 2](https://assets.dev.to/assets/heart-plus-active-9ea3b22f2bc311281db911d416166c5f430636e76b15cd5df6b3b841d830eefa.svg)0 Add reaction

![Image 3](https://assets.dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg)0 Like ![Image 4](https://assets.dev.to/assets/multi-unicorn-b44d6f8c23cdd00964192bedc38af3e82463978aa611b4365bd33a0f1f4f3e97.svg)0 Unicorn ![Image 5](https://assets.dev.to/assets/exploding-head-daceb38d627e6ae9b730f36a1e390fca556a4289d5a41abb2c35068ad3e2c4b5.svg)0 Exploding Head ![Image 6](https://assets.dev.to/assets/raised-hands-74b2099fd66a39f2d7eed9305ee0f4553df0eb7b4f11b01b6b1b499973048fe5.svg)0 Raised Hands ![Image 7](https://assets.dev.to/assets/fire-f60e7a582391810302117f987b22a8ef04a2fe0df7e3258a5f49332df1cec71e.svg)0 Fire

0 Jump to Comments 0 Save Boost

Copy link

Copied to Clipboard

[Share to X](https://twitter.com/intent/tweet?text=%22PostgreSQL%20Join%20Optimization%3A%20Nested%20Loop%2C%20Hash%2C%20and%20Merge%22%20by%20Philip%20McClarence%20%23DEVCommunity%20https%3A%2F%2Fdev.to%2Fphilip_mcclarence_2ef9475%2Fpostgresql-join-optimization-nested-loop-hash-and-merge-1cn9)[Share to LinkedIn](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fdev.to%2Fphilip_mcclarence_2ef9475%2Fpostgresql-join-optimization-nested-loop-hash-and-merge-1cn9&title=PostgreSQL%20Join%20Optimization%3A%20Nested%20Loop%2C%20Hash%2C%20and%20Merge&summary=PostgreSQL%20has%20three%20join%20algorithms.%20The%20planner%20picks%20between%20them%20for%20every%20join%20in%20every%20query%2C...&source=DEV%20Community)[Share to Facebook](https://www.facebook.com/sharer.php?u=https%3A%2F%2Fdev.to%2Fphilip_mcclarence_2ef9475%2Fpostgresql-join-optimization-nested-loop-hash-and-merge-1cn9)[Share to Mastodon](https://s2f.kytta.dev/?text=https%3A%2F%2Fdev.to%2Fphilip_mcclarence_2ef9475%2Fpostgresql-join-optimization-nested-loop-hash-and-merge-1cn9)

[Share Post via...](https://dev.to/philip_mcclarence_2ef9475/postgresql-join-optimization-nested-loop-hash-and-merge-1cn9#)[Report Abuse](https://dev.to/report-abuse)

[![Image 8: Philip McClarence](https://media2.dev.to/dynamic/image/width=50,height=50,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2690053%2F913499a1-620d-4487-a868-d677f1aca106.png)](https://dev.to/philip_mcclarence_2ef9475)

[Philip McClarence](https://dev.to/philip_mcclarence_2ef9475)
Posted on Apr 28

# PostgreSQL Join Optimization: Nested Loop, Hash, and Merge

[#database](https://dev.to/t/database)[#performance](https://dev.to/t/performance)[#postgres](https://dev.to/t/postgres)[#sql](https://dev.to/t/sql)

PostgreSQL has three join algorithms. The planner picks between them for every join in every query, driven by several things at once: the estimated sizes of the two inputs, whether they arrive already sorted on the join key, the type of join (inner vs left/semi/anti), which operators are `mergejoinable` or `hashjoinable`, whether a hash table will fit in `work_mem`, and the cost parameters that weigh I/O against CPU. Get the decision right and a three-way join across millions of rows runs in ten
Read full article → ← Back to Reads