Bird
0
0

You want to speed up queries on a products table filtering only active products with price < 100. Which partial index is best to create?

hard📝 Application Q15 of 15
PostgreSQL - Indexing Strategies
You want to speed up queries on a products table filtering only active products with price < 100. Which partial index is best to create?
ACREATE INDEX idx_active_price ON products (price);
BCREATE INDEX idx_active_price ON products (price) WHERE active = true AND price < 100;
CCREATE INDEX idx_active_price ON products (price) WHERE active = true OR price < 100;
DCREATE INDEX idx_active_price ON products WHERE active = true AND price < 100;
Step-by-Step Solution
Solution:
  1. Step 1: Define index condition matching query filters

    The query filters active = true AND price < 100, so index WHERE must match both.
  2. Step 2: Check syntax and columns

    Index must specify columns (price) and use AND in WHERE clause for correct filtering.
  3. Final Answer:

    CREATE INDEX idx_active_price ON products (price) WHERE active = true AND price < 100; -> Option B
  4. Quick Check:

    Partial index WHERE matches query filters exactly [OK]
Quick Trick: Use AND in WHERE to match all query conditions [OK]
Common Mistakes:
  • Using OR instead of AND in WHERE clause
  • Omitting columns in index definition
  • Trying to create index without columns

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes