Bird
0
0

You have a table products with columns id, category, and price. You often run this query:

hard📝 optimization Q15 of 15
PostgreSQL - Indexing Strategies
You have a table products with columns id, category, and price. You often run this query:
SELECT * FROM products WHERE category = 'books' AND price < 20;
Which indexing strategy will most improve query speed without slowing inserts too much?
ACreate no indexes to keep inserts fast.
BCreate separate indexes on category and price.
CCreate a composite index on (category, price).
DCreate an index only on price.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze query filter conditions

    The query filters on both category and price together, so a composite index on both columns helps the database find matching rows efficiently.
  2. Step 2: Compare indexing options

    Separate indexes may be less efficient because PostgreSQL might not combine them well; no index slows queries; indexing only price misses category filtering.
  3. Final Answer:

    Create a composite index on (category, price). -> Option C
  4. Quick Check:

    Composite index matches multi-column filters [OK]
Quick Trick: Use composite index for multi-column WHERE filters [OK]
Common Mistakes:
  • Creating separate indexes expecting same speed
  • Indexing only one column in multi-filter queries
  • Avoiding indexes to keep inserts fast but hurting queries

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes