Index-only scans mental model in PostgreSQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how fast an index-only scan works as the data grows.
How does the time to find data change when using only the index?
Analyze the time complexity of this index-only scan query.
SELECT column1, column2
FROM table_name
WHERE column1 = 'value';
-- Assume an index exists on column1 including column2
This query uses an index that has all needed data, so it does not read the main table rows.
Look for repeated steps in the scan process.
- Primary operation: Scanning index entries matching the condition.
- How many times: Once per matching index entry, no extra table row fetch needed.
As the number of matching rows grows, the work grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 index entries read |
| 100 | About 100 index entries read |
| 1000 | About 1000 index entries read |
Pattern observation: The time grows linearly with the number of matching rows.
Time Complexity: O(n)
This means the time to complete the scan grows directly with how many rows match.
[X] Wrong: "Index-only scans always take constant time no matter how many rows match."
[OK] Correct: The scan still reads each matching index entry, so more matches mean more work.
Knowing how index-only scans scale helps you explain query speed and optimization clearly.
What if the index did not include all needed columns and the query had to fetch table rows? How would the time complexity change?
Practice
index-only scan in PostgreSQL?Solution
Step 1: Understand what an index-only scan does
An index-only scan uses the index to get all needed data without reading the main table.Step 2: Compare options to this behavior
Only It reads data only from the index without accessing the main table. describes reading data solely from the index, which is the key benefit.Final Answer:
It reads data only from the index without accessing the main table. -> Option AQuick Check:
Index-only scan = reads from index only [OK]
- Thinking index-only scans update data
- Assuming they lock tables
- Believing they create indexes automatically
Solution
Step 1: Recall index-only scan requirements
PostgreSQL can use index-only scans only if the index has all columns the query needs.Step 2: Evaluate each option
The index contains all columns needed by the query. matches the requirement; others do not enable index-only scans.Final Answer:
The index contains all columns needed by the query. -> Option BQuick Check:
Index-only scan requires full column coverage in index [OK]
- Thinking index-only scans work without indexes
- Assuming aggregates always use index-only scans
- Believing empty tables affect index-only scans
users(id, name, email) with an index on (id, email), which query can use an index-only scan?Solution
Step 1: Check index columns and query columns
The index covers columns id and email. Query C requests only id and email.Step 2: Determine if index-only scan is possible
Query C can use index-only scan because all requested columns are in the index. Others request columns not in the index.Final Answer:
SELECT id, email FROM users WHERE id = 10; -> Option AQuick Check:
Query columns ⊆ index columns = index-only scan [OK]
- Choosing queries requesting columns not in index
- Ignoring WHERE clause columns
- Assuming * always uses index-only scan
(username, email) but your query SELECT email FROM users WHERE username = 'bob'; is not using an index-only scan. What could be the reason?Solution
Step 1: Confirm index covers needed columns
The index includes username and email, so columns are covered.Step 2: Understand visibility map role
Index-only scans require the visibility map to confirm tuples are visible without table access. If not updated, PostgreSQL reads the table.Final Answer:
The table's visibility map is not updated, so PostgreSQL must check the table. -> Option CQuick Check:
Visibility map must be updated for index-only scan [OK]
- Assuming index missing columns
- Believing WHERE clause blocks index-only scan
- Thinking data type prevents index-only scans
SELECT id, status FROM orders WHERE status = 'shipped'; for index-only scans. The current index is on (id). What is the best way to enable index-only scans?Solution
Step 1: Identify columns needed for index-only scan
The query selects id and status, so the index must cover both columns.Step 2: Choose index that covers all columns
Creating an index on (status, id) includes both columns, enabling index-only scans.Final Answer:
Create a new index on(status, id)including both columns. -> Option DQuick Check:
Index covering all query columns enables index-only scan [OK]
- Thinking partial index alone enables index-only scan
- Dropping index reduces performance
- Adding WHERE clause doesn't affect index structure
