Skip to main content

Posts

Showing posts with the label SQL

PostgreSQL Index Scan vs. Seq Scan: Why the Query Planner Ignores Your Index

  You identify a slow query. You identify the missing index. You create the index. You run   EXPLAIN ANALYZE , expecting a millisecond response time, only to see the PostgreSQL Query Planner stubbornly performing a   Sequential Scan . The index exists, it is valid, and the query references the indexed column. Yet, PostgreSQL refuses to use it. This is not a bug. It is a mathematical decision made by the Cost-Based Optimizer (CBO). The planner has calculated that reading the entire table is cheaper than traversing the B-Tree and jumping randomly around the disk to fetch rows. This usually happens for two reasons:  High Selectivity  or  Low Physical Correlation . The Root Cause: Correlation and Random I/O To understand why the planner ignores your index, you must understand how PostgreSQL calculates "Cost." Sequential Scan (Seq Scan):  The engine reads table pages linearly from the disk. This is highly efficient for I/O subsystems (especially HDDs) becau...