Bird
0
0

Which index strategy best enables index-only scans for queries like:

hard📝 Application Q9 of 15
PostgreSQL - Indexing Strategies
You have a table logs(id, event_type, created_at, details) and want to speed up queries filtering by event_type and selecting created_at. Which index strategy best enables index-only scans for queries like:

SELECT created_at FROM logs WHERE event_type = 'error';
ACREATE INDEX idx_logs_event_created ON logs (event_type, created_at);
BCREATE INDEX idx_logs_created ON logs (created_at);
CCREATE INDEX idx_logs_event ON logs (event_type) INCLUDE (created_at);
DCREATE INDEX idx_logs_details ON logs (details);
Step-by-Step Solution
Solution:
  1. Step 1: Analyze query columns

    The query filters on 'event_type' and selects 'created_at'.
  2. Step 2: Choose index covering filter and selected columns

    CREATE INDEX idx_logs_event ON logs (event_type) INCLUDE (created_at); indexes 'event_type' and includes 'created_at', covering all needed columns for an index-only scan.
  3. Step 3: Compare other options

    CREATE INDEX idx_logs_event_created ON logs (event_type, created_at); indexes both columns but does not use INCLUDE, which is fine but may be larger. CREATE INDEX idx_logs_created ON logs (created_at); and CREATE INDEX idx_logs_details ON logs (details); do not cover filter and select columns properly.
  4. Final Answer:

    CREATE INDEX idx_logs_event ON logs (event_type) INCLUDE (created_at); -> Option C
  5. Quick Check:

    Cover filter and select columns with INCLUDE = C [OK]
Quick Trick: Include selected columns, index filter column first [OK]
Common Mistakes:
  • Choosing index without filter column first
  • Not including selected columns in index
  • Indexing irrelevant columns

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes