Index-only scans help the database find data faster by using just the index without looking at the full table.
Index-only scans mental model in PostgreSQL
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
PostgreSQL
SELECT column1, column2 FROM table WHERE indexed_column = value;
The database decides automatically if it can use an index-only scan.
Index-only scans work only if the index contains all columns needed by the query.
Examples
PostgreSQL
CREATE INDEX idx_name ON employees (last_name, first_name);
PostgreSQL
SELECT last_name, first_name FROM employees WHERE last_name = 'Smith';
PostgreSQL
SELECT last_name FROM employees WHERE last_name = 'Smith';
Sample Program
This example creates a table and an index on last_name and first_name. Then it runs a query that can use an index-only scan because the query only asks for columns in the index.
PostgreSQL
CREATE TABLE employees ( id SERIAL PRIMARY KEY, last_name TEXT, first_name TEXT, department TEXT ); INSERT INTO employees (last_name, first_name, department) VALUES ('Smith', 'John', 'Sales'), ('Doe', 'Jane', 'HR'), ('Smith', 'Anna', 'IT'); CREATE INDEX idx_last_first ON employees (last_name, first_name); EXPLAIN ANALYZE SELECT last_name, first_name FROM employees WHERE last_name = 'Smith';
Important Notes
Index-only scans avoid reading the full table if all requested columns are in the index.
If the table has recent changes, the scan might need to check the table (heap fetches) to confirm data.
Not all queries can use index-only scans; it depends on the columns and index design.
Summary
Index-only scans speed up queries by using only the index.
They work when the index has all columns needed by the query.
This reduces disk reads and improves performance.
Practice
1. What is the main advantage of an
index-only scan in PostgreSQL?easy
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]
Hint: Index-only scans avoid table reads by using index data only [OK]
Common Mistakes:
- Thinking index-only scans update data
- Assuming they lock tables
- Believing they create indexes automatically
2. Which of the following is a correct condition for PostgreSQL to use an index-only scan?
easy
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]
Hint: Index-only scans need all query columns in the index [OK]
Common Mistakes:
- Thinking index-only scans work without indexes
- Assuming aggregates always use index-only scans
- Believing empty tables affect index-only scans
3. Given a table
users(id, name, email) with an index on (id, email), which query can use an index-only scan?medium
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]
Hint: Index-only scan if query columns are subset of index columns [OK]
Common Mistakes:
- Choosing queries requesting columns not in index
- Ignoring WHERE clause columns
- Assuming * always uses index-only scan
4. You have an index on
(username, email) but your query SELECT email FROM users WHERE username = 'bob'; is not using an index-only scan. What could be the reason?medium
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]
Hint: Visibility map must be updated for index-only scans [OK]
Common Mistakes:
- Assuming index missing columns
- Believing WHERE clause blocks index-only scan
- Thinking data type prevents index-only scans
5. You want to optimize a query
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?hard
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]
Hint: Index must cover all selected columns for index-only scan [OK]
Common Mistakes:
- Thinking partial index alone enables index-only scan
- Dropping index reduces performance
- Adding WHERE clause doesn't affect index structure
