Posts

Showing posts with the label nested loop

Using IN and NOT IN

Image
Introduction The IN clause is usually used in two main cases: Literal Values : When you already know the exact values to filter and the number of elements is small (from a few elements to a few dozen elements). Note that when parsing, Postgres will treat each item in Literal Values as a single node to execute the Parse Tree, so if the number of items is too large, it will consume a lot of RAM and CPU to process By the time planning is executed, the Query Planner will have to traverse this Tree to process, so you should be careful not to query with too many Literal Values as it will significantly affect performance It can be rewritten with EXISTS but it will make the query more complex, so if using Literal Values , it is best to use IN to keep the query simple and easy to understand An example of usage is filtering out orders with statuses like processing or completed . Dynamic List : When the filter list depends on the data of another table. In most cases, it can be replaced by usin...

Hash Join

Image
Introduction In this article, we will learn about Hash Join, which is the preferred algorithm when joining two large tables without indexes, or when the data volume exceeds the optimization capability of a Nested Loop. How It Works Step 1: Build Phase First, PostgreSQL selects the smaller table to load into memory (RAM). Next, it takes each item and passes it into a Hash Function to receive a hash number. Postgres relies on this value to sort the results into the corresponding bucket, resulting in the creation of a Hash Table stored in RAM. Step 2: Probe Phase Now, PostgreSQL scans the larger table, also taking each item to pass into the Hash Function just like in step 1. Once the hash number is obtained, it only needs to access the corresponding bucket in the Hash Table to check. Hash Collision When using a Hash Function, there are still cases where two different values produce the same result, which is called a Hash Collision. In this case, values with the same Hash Number will resid...