Non-Brand Data

Non-Brand Data

Module 13: When a SQL query needs optimization

In this module, we will learn how to read a PostgreSQL query plan and decide whether an index is worth keeping.

Cornellius Yudha Wijaya's avatar
Cornellius Yudha Wijaya
Oct 04, 2026
∙ Paid

Previous module: Module 12, Checking whether your result is right.

A slow query doesn’t always need another index. Before changing anything, we need to understand how PostgreSQL reads the table and how many rows the filter keeps.

For example, a tracking-code lookup returns one row from 30,000 orders. An index is useful in this case because the filter is very selective. A status filter that keeps 24,000 rows is different because PostgreSQL still needs most of the table.

For me, query optimization starts after the result is correct. We should read the plan, change one thing, and confirm that the query still returns the same result.

We will compare a sequential scan with an index scan, then check why PostgreSQL can still choose a sequential scan when an index exists.


User's avatar

Continue reading this post for free, courtesy of Cornellius Yudha Wijaya.

Or purchase a paid subscription.
© 2026 Cornellius Yudha Wijaya · Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture