0
0
PostgreSQLquery~10 mins

Sequential scan vs index scan in PostgreSQL - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Sequential scan vs index scan
Start Query
Check if index exists
Index Scan
Read matching rows
Return results
End
The database decides if it can use an index to find rows quickly. If yes, it uses an index scan; if not, it reads all rows one by one with a sequential scan.
Execution Sample
PostgreSQL
EXPLAIN ANALYZE SELECT * FROM employees WHERE department_id = 5;
This query shows whether PostgreSQL uses a sequential scan or an index scan to find employees in department 5.
Execution Table
StepActionScan TypeRows ExaminedRows ReturnedNotes
1Start query executionN/A00Query begins
2Check for index on department_idN/A00Index exists
3Use index to find matching rowsIndex Scan1010Only rows with department_id=5 scanned
4Return matching rowsIndex Scan1010Results sent to client
5End query executionN/A1010Query finished
💡 Query ends after returning all matching rows found by index scan
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Rows Examined00101010
Rows Returned00101010
Scan TypeN/AN/AIndex ScanIndex ScanIndex Scan
Key Moments - 2 Insights
Why does the database choose an index scan instead of a sequential scan here?
Because an index exists on department_id, the database can quickly find only matching rows without reading the whole table, as shown in execution_table step 3.
What happens if there is no index on the searched column?
The database will perform a sequential scan, reading every row one by one, which is slower but guaranteed to find all matches.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, how many rows does the index scan examine?
A0
BAll rows in the table
C10
D5
💡 Hint
Check the 'Rows Examined' column at step 3 in the execution_table.
At which step does the database confirm the index exists?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column in the execution_table for index checking.
If the index did not exist, what scan type would replace 'Index Scan' in step 3?
ASequential Scan
BBitmap Scan
CHash Scan
DNo Scan
💡 Hint
Recall the concept_flow where absence of index leads to sequential scan.
Concept Snapshot
Sequential scan reads every row in the table one by one.
Index scan uses an index to quickly find matching rows.
PostgreSQL chooses index scan if an index exists and is efficient.
Sequential scan is slower but always works.
Use EXPLAIN ANALYZE to see which scan is used.
Full Transcript
When PostgreSQL runs a query with a WHERE condition, it first checks if there is an index on the searched column. If an index exists, it uses an index scan to quickly find only the rows that match the condition. This means it reads fewer rows and returns results faster. If no index exists, it performs a sequential scan, reading every row in the table to find matches. The execution table shows each step: starting the query, checking for an index, using the index to find rows, returning results, and finishing. Variables like rows examined and scan type change as the query runs. Understanding when PostgreSQL uses each scan helps write faster queries.