dataiotech Labs
dbaguidedbaguideDBAdataxtoolsdataopsphere
Scripts Library/mongodb/#85
Script #85
mongodb
PerformanceProfiler'ı açmak hafif yazma etkisi yaratır; okuma salt okunurMin Version: 4.0

Analyze Slow MongoDB Queries with the Profiler

Groups system.profile data to reveal the slowest query patterns and COLLSCANs. Ideal for finding missing indexes.

#profiler#slow-query#system.profile
mongodb · javascript
// Profiler'ı aç (100 ms üzeri): db.setProfilingLevel(1, { slowms: 100 })
db.system.profile.aggregate([
  { $match: { op: { $in: ["query", "command", "update", "remove"] }, ns: { $not: /system\.profile/ } } },
  { $group: { _id: { ns: "$ns", planSummary: "$planSummary" },
              count: { $sum: 1 }, avg_ms: { $avg: "$millis" }, max_ms: { $max: "$millis" },
              avg_docs_examined: { $avg: "$docsExamined" }, avg_returned: { $avg: "$nreturned" } } },
  { $sort: { avg_ms: -1 } },
  { $limit: 20 }
]);
Operational Notes & Remediation Guidance

Verify in a staging environment prior to running in production. This is a read-only diagnostic query; it does not acquire exclusive locks.

Production Safety & Execution Standards
  • This script is designed for read-only diagnostics; it does not alter schema or modify data directly.
  • Prior to executing on production systems under high load, verify the target database version (Min. Version: 4.0).
  • Exercise caution with mutating actions (e.g., terminating sessions or dropping indexes) referenced in the notes section.