What if you could find any piece of data instantly without flipping through everything?
Sequential scan vs index scan in PostgreSQL - When to Use Which
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a huge phone book and you want to find all people named "John." You start at the first page and look at every single name until you find all the Johns.
This method is very slow and tiring because you check every name, even if the Johns are only on a few pages. It wastes time and energy, especially when the phone book is very big.
Using an index is like having a special list that tells you exactly which pages have the name "John." You can jump straight to those pages without flipping through the whole book.
SELECT * FROM people WHERE name = 'John'; -- scans all rowsCREATE INDEX idx_name ON people(name);
SELECT * FROM people WHERE name = 'John'; -- uses index to find rows fasterThis lets the database find data quickly and efficiently, saving time and resources.
When you search for a product on an online store, the system uses indexes to quickly show matching items instead of checking every product one by one.
Sequential scan checks every row one by one, which is slow for big data.
Index scan uses a special shortcut to find data faster.
Indexes make searching large databases much quicker and efficient.
Practice
sequential scan do in PostgreSQL?Solution
Step 1: Understand sequential scan behavior
A sequential scan reads all rows in the table from start to end without using any index.Step 2: Compare with other options
Using an index is an index scan, reading only first 10 rows or deleting rows are unrelated actions.Final Answer:
Reads every row in the table one by one -> Option DQuick Check:
Sequential scan = full table read [OK]
- Confusing sequential scan with index scan
- Thinking sequential scan reads only some rows
- Assuming sequential scan deletes rows
Solution
Step 1: Identify command to check query plan
TheEXPLAINcommand shows how PostgreSQL executes a query, including scan type.Step 2: Eliminate other options
SELECT runs query but doesn't show plan; CREATE and DROP INDEX modify indexes, not show plans.Final Answer:
EXPLAIN SELECT * FROM table WHERE id = 1; -> Option BQuick Check:
EXPLAIN shows scan type [OK]
- Running SELECT without EXPLAIN to check scan
- Confusing index creation with scan checking
- Using DROP INDEX to check scans
users(id SERIAL PRIMARY KEY, name TEXT) with 1 million rows, which scan is PostgreSQL likely to use for this query?SELECT * FROM users WHERE id = 500000;Solution
Step 1: Analyze query condition and table size
The query filters by primary keyid, which has an index, and the table is large (1 million rows).Step 2: Determine efficient scan type
PostgreSQL uses an index scan to quickly find the single matching row instead of scanning all rows.Final Answer:
Index scan using the primary key index -> Option AQuick Check:
Selective query on indexed column = index scan [OK]
- Assuming sequential scan for large tables with indexed filter
- Confusing bitmap heap scan with index scan
- Thinking query fails without reason
SELECT * FROM orders WHERE customer_id = 123;But EXPLAIN shows a sequential scan instead of an index scan. What could be the reason?
Solution
Step 1: Check index presence on filter column
If no index exists oncustomer_id, PostgreSQL must scan all rows sequentially.Step 2: Evaluate other options
Empty table would still show scan but no rows; syntax is correct; PostgreSQL chooses scan type based on indexes.Final Answer:
There is no index oncustomer_id-> Option AQuick Check:
No index on filter column = sequential scan [OK]
- Assuming syntax error causes scan type
- Thinking PostgreSQL always uses sequential scan
- Ignoring missing index as cause
products with millions of rows and an index on category_id. You run:SELECT * FROM products WHERE category_id IN (1, 2, 3, 4, 5);PostgreSQL chooses a sequential scan instead of an index scan. Why might this happen?
Solution
Step 1: Understand query selectivity
The query matches many rows because it filters on multiple categories, reducing selectivity.Step 2: Explain PostgreSQL scan choice
When many rows match, PostgreSQL prefers sequential scan as it can be faster than many index lookups.Final Answer:
The query is not selective enough; many rows match -> Option CQuick Check:
Low selectivity = sequential scan preferred [OK]
- Assuming index corruption without evidence
- Thinking sequential scan is always slower
- Believing IN clause is invalid syntax
