Mongod 100%CPU - 不确定如何诊断?

时间:2013-04-07 21:23:33

标签: python performance mongodb amazon-ec2

我有一个python(python& mongo newbie)应用程序,每小时通过cron运行以获取数据,清理它并插入mongo。在执行期间,应用程序将查询mongo以检查重复项,并在文档是新的时插入。

我最近注意到mongod处于100%cpu利用率......而且我不确定何时/为何开始发生这种情况。

我正在运行EC2微型实例,该实例具有用于mongo的专用EBS卷,其大小约为2.2GB。

我不确定从哪里开始诊断问题。这是系统上stats()和systemStatus()的输出:

> db.myApp.stats()
{
"ns" : "myApp.myApp",
"count" : 138096,
"size" : 106576816,
"avgObjSize" : 771.7588923647318,
"storageSize" : 133079040,
"numExtents" : 13,
"nindexes" : 1,
"lastExtentSize" : 27090944,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 4496800,
"indexSizes" : {
    "_id_" : 4496800
},
"ok" : 1
}
> db.serverStatus()
{
"host" : "kar",
"version" : "2.0.4",
"process" : "mongod",
"uptime" : 4146089,
"uptimeEstimate" : 3583433,
"localTime" : ISODate("2013-04-07T21:18:05.466Z"),
"globalLock" : {
    "totalTime" : 4146088784941,
    "lockTime" : 1483742858,
    "ratio" : 0.0003578656741237909,
    "currentQueue" : {
        "total" : 0,
        "readers" : 0,
        "writers" : 0
    },
    "activeClients" : {
        "total" : 2,
        "readers" : 2,
        "writers" : 0
    }
},
"mem" : {
    "bits" : 64,
    "resident" : 139,
    "virtual" : 1087,
    "supported" : true,
    "mapped" : 208,
    "mappedWithJournal" : 416
},
"connections" : {
    "current" : 7,
    "available" : 812
},
"extra_info" : {
    "note" : "fields vary by platform",
    "heap_usage_bytes" : 359456,
    "page_faults" : 634
},
"indexCounters" : {
    "btree" : {
        "accesses" : 3431,
        "hits" : 3431,
        "misses" : 0,
        "resets" : 0,
        "missRatio" : 0
    }
},
"backgroundFlushing" : {
    "flushes" : 69092,
    "total_ms" : 448897,
    "average_ms" : 6.497090835407862,
    "last_ms" : 0,
    "last_finished" : ISODate("2013-04-07T21:17:15.620Z")
},
"cursors" : {
    "totalOpen" : 0,
    "clientCursors_size" : 0,
    "timedOut" : 1
},
"network" : {
    "bytesIn" : 297154435,
    "bytesOut" : 222773714,
    "numRequests" : 1721768
},
"opcounters" : {
    "insert" : 138004,
    "query" : 359,
    "update" : 0,
    "delete" : 0,
    "getmore" : 0,
    "command" : 1583416
},
"asserts" : {
    "regular" : 0,
    "warning" : 0,
    "msg" : 0,
    "user" : 0,
    "rollovers" : 0
},
"writeBacksQueued" : false,
"dur" : {
    "commits" : 9,
    "journaledMB" : 0,
    "writeToDataFilesMB" : 0,
    "compression" : 0,
    "commitsInWriteLock" : 0,
    "earlyCommits" : 0,
    "timeMs" : {
        "dt" : 3180,
        "prepLogBuffer" : 0,
        "writeToJournal" : 0,
        "writeToDataFiles" : 0,
        "remapPrivateView" : 0
    }
},
"ok" : 1
}

最高输出:

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+ COMMAND
18477 mongodb   20   0 1087m 139m 122m R 99.9 23.7  10729:36 mongod 

我很好奇如何调试mongo来确定这种可怕的性能发生的位置/原因/原因。

更新:

我了解到我可以使用explain()获取详细信息,但我不确定如何解释结果

> db.myApp.find({'id':'320969221423124481'}).explain()
{
"cursor" : "BasicCursor",
"nscanned" : 138124,
"nscannedObjects" : 138124,
"n" : 0,
"millis" : 3949,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {

}
}

更新:

好的,我现在看到示例查询(它执行的BUNCH次数)接近4秒。我猜它不是使用任何索引。我需要查找如何添加索引......现在就这样做。

更新:

所以我做了以下

db.myApp.ensureIndex({'id':1})

它修复了一切。 HEH。

1 个答案:

答案 0 :(得分:6)

请参阅我的OP主题,但答案是需要添加的缺失索引:

db.myApp.ensureIndex({'id':1})