Bitmap index scan behavior in PostgreSQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to find rows using a bitmap index scan changes as the table grows.
How does the scan's work increase when there are more rows or matching entries?
Analyze the time complexity of this bitmap index scan query.
EXPLAIN ANALYZE
SELECT * FROM orders
WHERE customer_id = 12345;
-- Assume customer_id has a bitmap index
This query uses a bitmap index scan to find all orders for a specific customer.
Look at what repeats during the scan.
- Primary operation: Reading index entries matching the condition and then fetching table rows.
- How many times: Once for each matching index entry, then once per matching row in the table.
As the number of matching rows grows, the work grows roughly in proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 matching rows | About 10 index reads + 10 table fetches |
| 100 matching rows | About 100 index reads + 100 table fetches |
| 1000 matching rows | About 1000 index reads + 1000 table fetches |
Pattern observation: The total work grows roughly linearly with the number of matching rows.
Time Complexity: O(n)
This means the time to complete the bitmap index scan grows in direct proportion to the number of matching rows.
[X] Wrong: "Bitmap index scans always take constant time regardless of data size."
[OK] Correct: The scan time depends on how many rows match the condition, so it grows as more rows match.
Understanding how bitmap index scans scale helps you explain query performance clearly and shows you know how databases handle large data efficiently.
What if we changed the query to use multiple conditions combined with AND? How would the time complexity of the bitmap index scan change?
Practice
Bitmap Index Scan in PostgreSQL?Solution
Step 1: Understand Bitmap Index Scan role
Bitmap Index Scan creates a bitmap representing matching row positions using indexes.Step 2: Differentiate from other scans
It does not fetch rows directly but prepares a bitmap for efficient row retrieval later.Final Answer:
To create a bitmap of matching row locations from indexes for efficient retrieval -> Option BQuick Check:
Bitmap Index Scan = bitmap of row locations [OK]
- Confusing bitmap scan with direct table scan
- Thinking bitmap scan updates or deletes rows
- Assuming bitmap scan fetches rows immediately
Solution
Step 1: Identify Bitmap Index Scan syntax
Bitmap Index Scan appears as "Bitmap Index Scan on index_name" in EXPLAIN output.Step 2: Differentiate from other scans
Bitmap Heap Scan fetches rows using bitmap, Index Scan and Seq Scan are different methods.Final Answer:
Bitmap Index Scan on index_name (cost=0.29..8.31 rows=5 width=12) -> Option CQuick Check:
Bitmap Index Scan syntax = Bitmap Index Scan on index_name (cost=0.29..8.31 rows=5 width=12) [OK]
- Confusing Bitmap Heap Scan with Bitmap Index Scan
- Choosing Index Scan or Seq Scan syntax incorrectly
- Misreading EXPLAIN output keywords
employees with an index on department_id, what will the Bitmap Index Scan do when you run:EXPLAIN SELECT * FROM employees WHERE department_id = 5;?Solution
Step 1: Understand Bitmap Index Scan on condition
The scan uses the index on department_id to find matching rows and creates a bitmap of their locations.Step 2: Explain how rows are fetched
Using the bitmap, it fetches only those rows efficiently from the table, avoiding full scan.Final Answer:
It creates a bitmap of row locations where department_id = 5, then fetches those rows efficiently -> Option AQuick Check:
Bitmap Index Scan finds matching rows then fetches [OK]
- Thinking it scans the whole table sequentially
- Confusing scan with update or delete operations
- Assuming it fetches rows without bitmap
Bitmap Index Scan followed by Bitmap Heap Scan, but the query is running very slowly. What could be a likely cause?Solution
Step 1: Analyze Bitmap Index Scan and Bitmap Heap Scan behavior
Bitmap Index Scan creates a bitmap of matching rows; Bitmap Heap Scan fetches rows using that bitmap.Step 2: Understand performance impact of large bitmap
If too many rows match, the bitmap is large, causing many random disk accesses and slowing the query.Final Answer:
The bitmap is too large because the condition matches too many rows, causing inefficient heap fetch -> Option DQuick Check:
Large bitmap = slow Bitmap Heap Scan [OK]
- Assuming missing index causes Bitmap Index Scan
- Thinking missing WHERE clause causes Bitmap Index Scan
- Believing empty table causes slow scan
Solution
Step 1: Understand Bitmap Index Scan memory usage
Bitmap Index Scan builds a bitmap in memory; if too large, it spills to disk, slowing performance.Step 2: Improve performance by adding selective conditions
Adding more selective WHERE conditions reduces matching rows, making bitmap smaller and faster.Step 3: Evaluate other options
Increasing work_mem helps but may not be sufficient; dropping index or rewriting query may not improve bitmap scan efficiency.Final Answer:
Add more selective WHERE conditions to reduce matching rows before Bitmap Index Scan -> Option AQuick Check:
More selective WHERE = smaller bitmap = faster scan [OK]
- Dropping index thinking it helps performance
- Assuming increasing work_mem always solves slowness
- Believing rewriting query always improves bitmap scan
