Bird
0
0

Consider the table orders(id INT, total NUMERIC) and the index:

medium📝 query result Q5 of 15
PostgreSQL - Indexing Strategies
Consider the table orders(id INT, total NUMERIC) and the index:
CREATE INDEX idx_total_rounded ON orders ((ROUND(total)));
What will the query SELECT * FROM orders WHERE ROUND(total) = 100; do?
AThrow a syntax error due to incorrect index usage
BUse the idx_total_rounded expression index to speed up the query
CPerform a sequential scan ignoring the index
DUse a default index on total column
Step-by-Step Solution
Solution:
  1. Step 1: Match query expression with index expression

    The index is created on ROUND(total), and the query filters by ROUND(total) = 100, so they match.
  2. Step 2: Determine query plan behavior

    PostgreSQL will use the expression index idx_total_rounded to speed up the query.
  3. Final Answer:

    Use the idx_total_rounded expression index to speed up the query -> Option B
  4. Quick Check:

    Matching expressions in query and index enable index usage [OK]
Quick Trick: Expression index used only if query expression matches exactly [OK]
Common Mistakes:
  • Assuming expression indexes cause errors
  • Thinking default column indexes are used instead
  • Believing expression indexes are ignored in queries

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes