Posts

Showing posts with the label sql optimization

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...

How Index and B-Tree Index Work

Image
Introduction Index When creating an Index, PostgreSQL creates a separate physical file on the disk. Each index has a Relfilenode which is a unique identifier number. An index does not store the entire information of a row but only contains Index Entries, each entry including: Key: The value of the column you index. TID (Tuple Identifier): A physical pointer consisting of a BlockNumber and an OffsetNumber, used for reference to point to the location of that row in the main table (HEAP). When executing a query, the Query Planner Cost Model in Postgres will calculate to choose between reading data from the index or retrieving it directly from the HEAP (Sequential Scan). {Index Scan Cost} = {Index file reading cost} + {Random block reading cost in the table} {Seq Scan Cost} = {Sequential block reading cost in the table} Because the Index only contains the TID to point to the data in the main table, after loading the index content and finding the necessary values, it must perform random I...