Sparse indexes in MongoDB - Time & Space Complexity
When using sparse indexes in MongoDB, it's important to understand how the time to find data changes as the amount of data grows.
We want to see how the index helps speed up searches and how its cost changes with more data.
Analyze the time complexity of the following MongoDB query using a sparse index.
// Create a sparse index on the field "email"
db.users.createIndex({ email: 1 }, { sparse: true })
// Query to find users with a specific email
const result = db.users.find({ email: "user@example.com" })
This code creates a sparse index on the "email" field, which only indexes documents where "email" exists. Then it queries users by email.
Look at what repeats when the query runs:
- Primary operation: The database looks up the email value in the sparse index.
- How many times: It does this once per query, but the index structure may check multiple nodes internally.
As the number of users grows, the index helps keep searches fast by skipping documents without emails.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 3-4 steps to find the email |
| 100 | About 6-7 steps |
| 1000 | About 10 steps |
Pattern observation: The number of steps grows slowly, roughly with the height of the index tree, not the total data size.
Time Complexity: O(log n)
This means the search time grows slowly as the data grows, thanks to the sparse index structure.
[X] Wrong: "Sparse indexes make the query run in constant time no matter how big the data is."
[OK] Correct: Even with sparse indexes, the database still needs to traverse the index tree, which grows slowly with data size, so it is not constant time.
Understanding how sparse indexes affect query speed shows you know how databases handle missing data efficiently, a useful skill in real projects.
"What if we changed the sparse index to a regular index? How would the time complexity change when querying documents missing the indexed field?"