Bird
0
0

You want to optimize a Firestore query that filters by 'category' and orders by 'price' and 'rating'. You also want to paginate results. What is the best index strategy?

hard📝 Application Q9 of 15
GCP - Cloud Firestore and Bigtable
You want to optimize a Firestore query that filters by 'category' and orders by 'price' and 'rating'. You also want to paginate results. What is the best index strategy?
ANo index needed if you limit results to 10 documents.
BCreate a composite index on 'category', 'price', and 'rating' with ascending order.
CCreate a composite index on 'price' and 'rating' only, ignoring 'category'.
DUse single-field indexes on each field and rely on Firestore's automatic indexing.
Step-by-Step Solution
Solution:
  1. Step 1: Understand composite index needs for filtering and ordering

    Filtering by 'category' and ordering by 'price' and 'rating' requires a composite index including all these fields.
  2. Step 2: Consider pagination

    Pagination requires consistent ordering, so composite index must cover all orderBy fields.
  3. Final Answer:

    Create a composite index on 'category', 'price', and 'rating' with ascending order. -> Option B
  4. Quick Check:

    Filter + multiple orderBy + pagination = composite index on all fields [OK]
Quick Trick: Include all filter and orderBy fields in composite index for pagination [OK]
Common Mistakes:
  • Ignoring filter field in composite index
  • Relying only on single-field indexes
  • Assuming limit removes index need

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More GCP Quizzes