Working with Database Profiler
1. Enabling Database Profiler
db.setProfilingLevel(1, { slowms: 100 });
2. Setting Profiling Level
| Level | Effect |
|---|---|
| 0 | Disabled |
| 1 | Slow ops only |
| 2 | All ops (high overhead) |
3. Setting Slow Operation Threshold
| Setting | Detail |
|---|---|
| slowms | Threshold in ms (default 100) |
| sampleRate | 0.0–1.0 (sample subset) |
4. Querying system.profile Collection
db.system.profile.find({millis:{$gt:200}}).sort({ts:-1}).limit(20);
5. Analyzing Slow Queries
| Field | Use |
|---|---|
| millis | Execution time |
| planSummary | COLLSCAN / IXSCAN + index |
| docsExamined / keysExamined | Efficiency |
| nreturned | Result size |
6. Understanding Profile Document Structure
| Field | Detail |
|---|---|
| op | command / query / insert / update / remove |
| ns | Namespace |
| command | Original command document |
| locks | Lock acquisition times |
| storage | Cache + bytes read |
7. Using Profile for Index Optimization
| Signal | Action |
|---|---|
| High docsExamined / nreturned ratio | Add or refine index |
| COLLSCAN | Create index |
| SORT stage | Add sort-friendly index |
8. Filtering Profile Results
| Filter | Detail |
|---|---|
| filter (4.4+) | Persistent filter for what gets profiled |
| ad-hoc | find on system.profile |
9. Disabling Profiler
db.setProfilingLevel(0);
10. Managing Profile Collection Size
| Aspect | Detail |
|---|---|
| Capped | Default 1MB; system.profile |
| Resize | Disable profiler, drop, recreate as capped with desired size |