Bird
0
0

Given the table orders(order_id, customer_id, status, total_amount), which index definition best enables an index-only scan for the query:

hard📝 Application Q8 of 15
PostgreSQL - Indexing Strategies
Given the table orders(order_id, customer_id, status, total_amount), which index definition best enables an index-only scan for the query:

SELECT customer_id, total_amount FROM orders WHERE status = 'completed';
ACREATE INDEX idx_total ON orders(total_amount);
BCREATE INDEX idx_customer ON orders(customer_id);
CCREATE INDEX idx_status ON orders(status);
DCREATE INDEX idx_status_customer_total ON orders(status, customer_id, total_amount);
Step-by-Step Solution
Solution:
  1. Step 1: Analyze query columns

    The query filters by status and selects customer_id and total_amount.
  2. Step 2: Determine index coverage

    To enable an index-only scan, the index must cover all these columns.
  3. Step 3: Evaluate options

    CREATE INDEX idx_status_customer_total ON orders(status, customer_id, total_amount); includes status, customer_id, and total_amount in the index, fully covering the query.
  4. Final Answer:

    CREATE INDEX idx_status_customer_total ON orders(status, customer_id, total_amount); -> Option D
  5. Quick Check:

    Index covers WHERE and SELECT columns [OK]
Quick Trick: Index must cover WHERE and SELECT columns for index-only scan [OK]
Common Mistakes:
  • Creating index on only one column
  • Ignoring columns in SELECT clause
  • Not including filter column in index

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes