0
0
PostgreSQLquery~20 mins

Sequential scan vs index scan in PostgreSQL - Practice Questions

Choose your learning style9 modes available
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
intermediate
2: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;
ASequential scan
BHash scan
CBitmap heap scan
DIndex scan
Attempts:
2 left
💡 Hint
Think about how PostgreSQL uses indexes to find rows matching a condition.
🧠 Conceptual
intermediate
2: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?
AWhen the query filters on a unique column
BWhen the table has an index on the filtered column
CWhen the table is very small or the query returns most rows
DWhen the query uses an ORDER BY clause
Attempts:
2 left
💡 Hint
Think about the cost of using an index versus reading the whole table.
📝 Syntax
advanced
2: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;?
AEXPLAIN SELECT * FROM products WHERE price < 100;
BEXPLAIN ANALYZE SELECT * FROM products WHERE price < 100;
CEXPLAIN PLAN FOR SELECT * FROM products WHERE price < 100;
DEXPLAIN QUERY SELECT * FROM products WHERE price < 100;
Attempts:
2 left
💡 Hint
One option runs the query and shows actual timing and scan type.
optimization
advanced
2:00remaining
Optimize query to use index scan instead of sequential scan
Given a large table 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?
ARewrite the query to use a JOIN instead of a subquery
BAdd an index on the <code>country</code> column in <code>customers</code>
CIncrease work_mem setting in PostgreSQL
DUse a sequential scan hint in the query
Attempts:
2 left
💡 Hint
Sometimes rewriting queries helps the planner choose better plans.
🔧 Debug
expert
2:00remaining
Diagnose why index scan is not used
A table 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?
AThe statistics for the table are outdated and need to be refreshed
BThe sale_date column is not indexed
CThe query uses an unsupported operator for the index
DThe table is empty
Attempts:
2 left
💡 Hint
PostgreSQL relies on statistics to decide query plans.