0
0
PostgreSQLquery~20 mins

Index-only scans mental model in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Index-only Scan Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of an index-only scan query

Consider a PostgreSQL table employees with columns id, name, and department. An index exists on department and name. Which query will most likely produce an index-only scan result?

PostgreSQL
SELECT name FROM employees WHERE department = 'Sales';
ASELECT name FROM employees WHERE department = 'Sales';
BSELECT id FROM employees WHERE department = 'Sales';
CSELECT * FROM employees WHERE department = 'Sales';
DSELECT name FROM employees WHERE id = 10;
Attempts:
2 left
💡 Hint

Index-only scans work best when the query can be answered using only the indexed columns.

🧠 Conceptual
intermediate
2:00remaining
Understanding visibility map role in index-only scans

What role does the visibility map play in PostgreSQL's index-only scans?

AIt tracks which table pages are all-visible to avoid heap access during index-only scans.
BIt stores the index data for faster lookups.
CIt manages user permissions for index access.
DIt logs query execution times for optimization.
Attempts:
2 left
💡 Hint

Think about how PostgreSQL avoids reading the main table during index-only scans.

📝 Syntax
advanced
2:00remaining
Identify the query that forces a heap access despite index-only scan

Given a table orders with an index on customer_id and order_date, which query will NOT use an index-only scan and will force heap access?

ASELECT customer_id FROM orders WHERE order_date > '2023-01-01';
BSELECT customer_id FROM orders WHERE customer_id = 123;
CSELECT customer_id, total_amount FROM orders WHERE order_date > '2023-01-01';
DSELECT order_date FROM orders WHERE customer_id = 123;
Attempts:
2 left
💡 Hint

Index-only scans require all selected columns to be in the index.

optimization
advanced
2:00remaining
Improving index-only scan effectiveness

You notice that your index-only scan queries still access the heap frequently. What is the best way to reduce heap access and improve index-only scan performance?

AAdd more columns to the table to reduce index size.
BRun <code>VACUUM</code> to update the visibility map and mark pages as all-visible.
CDrop and recreate the index frequently.
DDisable autovacuum to prevent interference.
Attempts:
2 left
💡 Hint

Think about how PostgreSQL knows which pages are safe to skip.

🔧 Debug
expert
2:00remaining
Diagnosing why an index-only scan is not used

You created a multi-column index on products(category, price). You run this query:

SELECT category FROM products WHERE price < 100;

But PostgreSQL does not use an index-only scan. What is the most likely reason?

AThe index columns are in the wrong order for this query to use an index-only scan.
BThe query selects only <code>category</code>, which is not in the index.
CPostgreSQL does not support index-only scans on numeric columns.
DThe visibility map is not updated, so heap access is needed.
Attempts:
2 left
💡 Hint

Consider what PostgreSQL needs to skip heap access.