Challenge - 5 Problems
Master of Sequential and Index Scans
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Identify the scan type used by this query
Given a table employees with an index on the
department_id column, what type of scan will PostgreSQL most likely use for this query?SELECT * FROM employees WHERE department_id = 5;Attempts:
2 left
💡 Hint
Think about how PostgreSQL uses indexes to find rows matching a condition.
✗ Incorrect
When a query filters on a column with an index, PostgreSQL usually uses an index scan to quickly find matching rows instead of scanning the whole table.
🧠 Conceptual
intermediate2:00remaining
When does PostgreSQL prefer sequential scan over index scan?
Which situation below best explains when PostgreSQL will choose a sequential scan instead of an index scan?
Attempts:
2 left
💡 Hint
Think about the cost of using an index versus reading the whole table.
✗ Incorrect
If the table is small or the query returns a large portion of rows, scanning the whole table sequentially is often faster than using an index.
📝 Syntax
advanced2:00remaining
Identify the error in this EXPLAIN query
Which option shows the correct way to get the query plan including whether a sequential or index scan is used for
SELECT * FROM products WHERE price < 100;?Attempts:
2 left
💡 Hint
One option runs the query and shows actual timing and scan type.
✗ Incorrect
EXPLAIN ANALYZE runs the query and shows the actual execution plan including scan types and timing. Other options are invalid syntax or incomplete.
❓ optimization
advanced2:00remaining
Optimize query to use index scan instead of sequential scan
Given a large table
Which change is most likely to make PostgreSQL use an index scan on
orders with an index on customer_id, this query uses a sequential scan:SELECT * FROM orders WHERE customer_id IN (SELECT id FROM customers WHERE country = 'USA');Which change is most likely to make PostgreSQL use an index scan on
customer_id?Attempts:
2 left
💡 Hint
Sometimes rewriting queries helps the planner choose better plans.
✗ Incorrect
Rewriting the query with a JOIN can help PostgreSQL better use indexes on customer_id by flattening the query and making the filter clearer.
🔧 Debug
expert2:00remaining
Diagnose why index scan is not used
A table
What is the most likely reason PostgreSQL does not use the index?
sales has an index on sale_date. The query below always uses sequential scan:SELECT * FROM sales WHERE sale_date > '2023-01-01';What is the most likely reason PostgreSQL does not use the index?
Attempts:
2 left
💡 Hint
PostgreSQL relies on statistics to decide query plans.
✗ Incorrect
If statistics are outdated, PostgreSQL may wrongly estimate that a sequential scan is cheaper. Running ANALYZE updates statistics and can fix this.