Profiler for slow queries in MongoDB - Time & Space Complexity
When we use MongoDB's profiler to find slow queries, we want to understand how the time to run these queries grows as the data gets bigger.
We ask: How does the work done by the database increase when the data size grows?
Analyze the time complexity of enabling and using the MongoDB profiler to find slow queries.
// Enable profiler for slow queries over 100ms
db.setProfilingLevel(1, { slowms: 100 });
// Run some queries
db.collection.find({ age: { $gt: 30 } }).toArray();
// Check profiler data
db.system.profile.find({ millis: { $gt: 100 } }).sort({ ts: -1 });
This code sets the profiler to log queries slower than 100 milliseconds, runs a query, and then retrieves slow query logs.
Look at what repeats when profiling slow queries.
- Primary operation: Each query execution is recorded if it exceeds the slow time threshold.
- How many times: Once per query that runs slower than the threshold, so it depends on how many slow queries happen.
As the database size grows, queries may take longer, so more queries might be logged by the profiler.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 documents | Few slow queries, few profiler entries |
| 1000 documents | More slow queries possible, more profiler entries |
| 100,000 documents | Many slow queries logged, profiler data grows |
Pattern observation: The profiler work grows with the number of slow queries, which often grows as data size and query complexity increase.
Time Complexity: O(k)
This means the profiler's work grows linearly with the number of slow queries it records.
[X] Wrong: "The profiler slows down all queries equally regardless of speed."
[OK] Correct: The profiler mainly records queries that exceed the slow threshold, so fast queries are not logged and have minimal impact.
Understanding how profiling affects performance helps you explain how to monitor and improve database speed in real projects.
"What if we lowered the slowms threshold to 1ms? How would the time complexity of profiling change?"
Practice
Solution
Step 1: Understand profiler's role
The profiler records queries that exceed a time threshold to help identify slow operations.Step 2: Identify what profiler does not do
It does not fix or delete queries automatically; it only logs them for review.Final Answer:
To record queries that take longer than a specified time to help optimize performance -> Option BQuick Check:
Profiler logs slow queries = To record queries that take longer than a specified time to help optimize performance [OK]
- Thinking profiler fixes queries automatically
- Confusing profiler with query caching
- Assuming profiler deletes slow queries
Solution
Step 1: Recall profiling levels
Level 1 logs slow queries exceeding the slowms threshold.Step 2: Check correct syntax
The correct syntax uses level and options object with slowms key, so db.setProfilingLevel(1, { slowms: 100 }) is valid.Final Answer:
db.setProfilingLevel(1, { slowms: 100 }) -> Option CQuick Check:
Level 1 + slowms option = db.setProfilingLevel(1, { slowms: 100 }) [OK]
- Using level 2 to log only slow queries (it logs all queries)
- Passing slowms as second argument without object
- Setting level 0 which disables profiling
db.system.profile.find({ millis: { $gt: 50 } })What does this query return?
Solution
Step 1: Understand the query filter
The filter { millis: { $gt: 50 } } selects documents where the millis field is greater than 50.Step 2: Interpret millis field meaning
Millis records query execution time in milliseconds, so this finds queries slower than 50ms.Final Answer:
All queries that took more than 50 milliseconds -> Option AQuick Check:
Filter $gt 50 means queries > 50ms = All queries that took more than 50 milliseconds [OK]
- Confusing $gt with $lt (greater than vs less than)
- Thinking it returns queries equal to 50ms
- Assuming it returns all queries
db.setProfilingLevel(1, { slowms: '100' }) but no slow queries are logged. What is the problem?Solution
Step 1: Check slowms parameter type
slowms expects a number, but '100' is a string, causing it to ignore the threshold.Step 2: Understand profiling behavior
Profiling level 1 logs slow queries, no restart needed, and default slowms is 100ms if not set.Final Answer:
The slowms value should be a number, not a string -> Option DQuick Check:
slowms must be numeric, not string = The slowms value should be a number, not a string [OK]
- Passing slowms as string instead of number
- Thinking profiling level 1 disables logging
- Believing MongoDB needs restart after setting profiler
Solution
Step 1: Sort queries by execution time descending
Sorting by millis: -1 orders queries from slowest to fastest.Step 2: Limit results to top 3
Using limit(3) returns only the first three documents after sorting.Final Answer:
db.system.profile.find().sort({ millis: -1 }).limit(3) -> Option AQuick Check:
Sort descending + limit 3 = db.system.profile.find().sort({ millis: -1 }).limit(3) [OK]
- Sorting millis ascending returns fastest queries
- Filtering millis less than 3 returns wrong data
- Not sorting before limiting results
Ready to prove it?
Pass the test and this pattern moves unknown
