profiler 데이터에서 가장 느린 쿼리와 query shape 추출
MongoDB성능명령어
// Recent slow operations (in the db where profiling is on; see set-profiling):
db.system.profile.find({ millis: { $gte: 100 } }, { ts: 1, ns: 1, op: 1, millis: 1, planSummary: 1, keysExamined: 1, docsExamined: 1, nreturned: 1, appName: 1 }).sort({ ts: -1 }).limit(20)
// Total time per query shape (those consuming the most time):
db.system.profile.aggregate([
{ $group: { _id: { ns: "$ns", plan: "$planSummary", shape: "$queryHash" }, n: { $sum: 1 }, totalMs: { $sum: "$millis" }, maxMs: { $max: "$millis" }, avgDocs: { $avg: "$docsExamined" } } },
{ $sort: { totalMs: -1 } },
{ $limit: 10 }
])
// Queries not using an index (COLLSCAN):
db.system.profile.find({ planSummary: "COLLSCAN" }, { ns: 1, millis: 1, command: 1 }).sort({ millis: -1 }).limit(10)하는 일
system.profile 컬렉션의 기록에서 최근 느린 작업, query shape(queryHash)별 총 시간, 인덱스를 사용하지 않는 COLLSCAN 쿼리를 나열한다.
사용 시점
성능 저하 불만 시, 어떤 쿼리가 서버 시간을 소비하는지 찾고 인덱스 필요성을 판단하기 위해.
주의
system.profile은 기본적으로 1 MB capped collection이며 데이터베이스별로 존재한다. 바쁜 시스템에서는 몇 분 만에 기록이 순환되므로 profiling이 켜져 있는 동안이나 직후에 분석한다. profiling level 2 또는 slowms 0은 모든 작업을 기록하여 상당한 오버헤드를 추가한다. 기록은 실행된 멤버에만 보관되며 복제되지 않는다.