Composite index requirements in Firebase - Time & Space Complexity
When using composite indexes in Firebase, it is important to understand how the number of fields in the index affects query speed.
We want to know how query time changes as we add more fields to the composite index.
Analyze the time complexity of this Firebase query using a composite index.
const query = firestore.collection('orders')
.where('status', '==', 'shipped')
.where('customerId', '==', '12345')
.orderBy('orderDate', 'desc')
.limit(20);
const snapshot = await query.get();
This code queries orders filtered by status and customerId, ordered by orderDate, using a composite index.
Look at what repeats when the query runs.
- Primary operation: Searching the composite index entries matching the filters.
- How many times: The database scans entries matching the combined fields in the index.
The query uses the composite index to quickly find matching entries.
| Input Size (k) | Approx. Operations |
|---|---|
| 10 | About 10 or fewer index lookups |
| 100 | About 100 or fewer index lookups |
| 1000 | About 1000 or fewer index lookups |
Pattern observation: The number of operations grows roughly in proportion to the number of matching entries, not the total data size.
Time Complexity: O(k)
This means the query time grows linearly with the number of matching entries k, thanks to the composite index.
[X] Wrong: "Adding more fields to the composite index always makes queries slower."
[OK] Correct: Composite indexes help narrow down results faster, so adding relevant fields can make queries faster, not slower.
Understanding how composite indexes affect query speed shows you know how databases handle filtering and sorting efficiently.
"What if we removed the composite index and only had single-field indexes? How would the time complexity change?"
Practice
Solution
Step 1: Understand composite index purpose
Composite indexes allow Firebase to quickly search using more than one field at the same time.Step 2: Compare options to purpose
Only To efficiently query data using multiple fields together matches this purpose; others describe unrelated features.Final Answer:
To efficiently query data using multiple fields together -> Option DQuick Check:
Composite index = multi-field query speed [OK]
- Confusing composite indexes with data backup
- Thinking composite indexes encrypt data
- Believing composite indexes store data in multiple places
Solution
Step 1: Check JSON structure for composite index
Correct composite index JSON includes collectionGroup, queryScope, and fields with fieldPath and order.Step 2: Validate each option
Only "indexes": [{"collectionGroup": "users", "queryScope": "COLLECTION", "fields": [{"fieldPath": "age", "order": "ASCENDING"}, {"fieldPath": "name", "order": "DESCENDING"}]}] has all required keys and correct field definitions with order specified.Final Answer:
"indexes": [{"collectionGroup": "users", "queryScope": "COLLECTION", "fields": [{"fieldPath": "age", "order": "ASCENDING"}, {"fieldPath": "name", "order": "DESCENDING"}]}] -> Option AQuick Check:
Composite index JSON needs fields with order [OK]
- Omitting order in fields
- Using 'collection' instead of 'collectionGroup'
- Listing fields as simple strings without objects
db.collection('products').where('category', '==', 'books').orderBy('price', 'asc').orderBy('rating', 'desc'), what composite index is required?Solution
Step 1: Identify query filters and orderBy fields
The query filters on 'category' and orders by 'price' ascending and 'rating' descending.Step 2: Determine composite index fields and order
Composite index must include 'category' (filter), then 'price' (asc), then 'rating' (desc) in that order.Final Answer:
Index on fields: category (ASC), price (ASC), rating (DESC) -> Option CQuick Check:
Filter + orderBy fields = composite index fields [OK]
- Ignoring filter field in index
- Wrong order or direction of fields
- Assuming no index needed for multiple orderBy
db.collection('orders').where('status', '==', 'shipped').orderBy('date', 'desc'). Which fix is correct?Solution
Step 1: Analyze query filter and orderBy
The query filters on 'status' and orders by 'date' descending.Step 2: Determine correct composite index order
Composite index must start with filter field 'status' (ASC by default), then 'date' (DESC) as orderBy.Final Answer:
Create a composite index on fields: status (ASC), date (DESC) -> Option AQuick Check:
Filter field first, then orderBy fields in index [OK]
- Reversing field order in index
- Creating single-field index only
- Ignoring error and changing query syntax
Solution
Step 1: Identify query filter and sort fields
The query filters on 'location' and 'type', and sorts by 'date' ascending.Step 2: Determine composite index order
Composite index must list filter fields first in any order, then the sort field last with ascending order.Step 3: Validate options
Index on fields: location (ASC), type (ASC), date (ASC) matches filter fields first (location, type) then date ascending. Index on fields: date (ASC), location (ASC), type (ASC) starts with date, which is incorrect. Index on fields: location (DESC), type (DESC), date (ASC) uses descending for filters, which is wrong. No composite index needed if only filtering is incorrect because sorting requires composite index.Final Answer:
Index on fields: location (ASC), type (ASC), date (ASC) -> Option BQuick Check:
Filters first, then sort field in index [OK]
- Placing sort field before filters in index
- Using descending order for filter fields
- Assuming no index needed when sorting
Ready to prove it?
Pass the test and this pattern moves unknown
