gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:11:44 25 4
gpt4 key购买 nike

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

我最近注意到 mongod 的 cpu 利用率为 100% ... 我不确定这是何时/为什么开始发生的。

我在一个 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" : {

}
}

更新:

好的,我现在看到示例查询(它执行了很多次)花费了将近 4 秒。我猜它没有使用任何索引。我需要查找如何添加索引...现在就做。

更新:

所以我做了以下

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

它解决了所有问题。呵呵。

最佳答案

查看我的 OP 线程,但答案是需要添加缺少的索引:

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

关于python - Mongod 占用 100% CPU - 不确定如何诊断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15868001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com