MongoDB: 6. Optimization
來(lái)源:程序員人生 發(fā)布時(shí)間:2014-01-17 08:31:33 閱讀次數(shù):3530次
1. Profiler
MongoDB 自帶 Profiler,可以非常方便地記錄下所有耗時(shí)過(guò)長(zhǎng)操作,以便于調(diào)優(yōu)。
> db.setProfilingLevel(n)
n:
0: Off;
1: Log Slow Operations;
2: Log All Operations.
通常我們只關(guān)心 Slow Operation,Level 1 默認(rèn)記錄 >100ms 的操作,當(dāng)然我們也可以自己調(diào)整 "db.setProfilingLevel(2, 300)"。
Profiler 信息保存在 system.profile (Capped Collection) 中。
準(zhǔn)備 1000000 條數(shù)據(jù)測(cè)試一下。
>>> from pymongo import *
>>> from random import randint
>>> conn = Connection()
>>> db = conn.blog
>>> for i in xrange(1000000):
u = dict(name = "user" + str(i), age = randint(10, 90))
db.users.insert(u)
開(kāi)始調(diào)優(yōu)操作。
> db.setProfilingLevel(1)
{ "was" : 0, "ok" : 1 }
> db.users.find().sort({age:-1}).limit(10000)
{ "_id" : ObjectId("4c50dc07499b1404c60f42e5"), "age" : 90, "name" : "user165" }
{ "_id" : ObjectId("4c50dc07499b1404c60f42e8"), "age" : 90, "name" : "user168" }
{ "_id" : ObjectId("4c50dc07499b1404c60f4350"), "age" : 90, "name" : "user272" }
{ "_id" : ObjectId("4c50dc07499b1404c60f4358"), "age" : 90, "name" : "user280" }
{ "_id" : ObjectId("4c50dc07499b1404c60f4375"), "age" : 90, "name" : "user309" }
{ "_id" : ObjectId("4c50dc07499b1404c60f4433"), "age" : 90, "name" : "user499" }
{ "_id" : ObjectId("4c50dc07499b1404c60f4480"), "age" : 90, "name" : "user576" }
{ "_id" : ObjectId("4c50dc07499b1404c60f4484"), "age" : 90, "name" : "user580" }
{ "_id" : ObjectId("4c50dc07499b1404c60f44cf"), "age" : 90, "name" : "user655" }
{ "_id" : ObjectId("4c50dc07499b1404c60f44fb"), "age" : 90, "name" : "user699" }
{ "_id" : ObjectId("4c50dc07499b1404c60f4517"), "age" : 90, "name" : "user727" }
{ "_id" : ObjectId("4c50dc07499b1404c60f4688"), "age" : 90, "name" : "user1096" }
{ "_id" : ObjectId("4c50dc07499b1404c60f46a8"), "age" : 90, "name" : "user1128" }
{ "_id" : ObjectId("4c50dc07499b1404c60f46ae"), "age" : 90, "name" : "user1134" }
{ "_id" : ObjectId("4c50dc07499b1404c60f4740"), "age" : 90, "name" : "user1280" }
{ "_id" : ObjectId("4c50dc07499b1404c60f479b"), "age" : 90, "name" : "user1371" }
{ "_id" : ObjectId("4c50dc07499b1404c60f479d"), "age" : 90, "name" : "user1373" }
{ "_id" : ObjectId("4c50dc07499b1404c60f480f"), "age" : 90, "name" : "user1487" }
{ "_id" : ObjectId("4c50dc07499b1404c60f4842"), "age" : 90, "name" : "user1538" }
{ "_id" : ObjectId("4c50dc07499b1404c60f4844"), "age" : 90, "name" : "user1540" }
has more
> db.system.profile.find()
{
"ts" : "Thu Jul 29 2010 09:47:47 GMT+0800 (CST)",
"info" : "query blog.users
ntoreturn:10000 scanAndOrder
reslen:518677
nscanned:1000000
query: { query: {}, orderby: { age: -1.0 } }
nreturned:10000 1443ms",
"millis" : 1443
}
system.profile 中記錄下一條耗時(shí)過(guò)長(zhǎng)的操作。
ts: 操作執(zhí)行時(shí)間。
info: 操作詳細(xì)信息。
info.query: 查詢目標(biāo)(數(shù)據(jù)庫(kù).集合)。
info.ntoreturn: 客戶端期望返回的文檔數(shù)量。
info.nscanned: 服務(wù)器實(shí)際掃描的文檔數(shù)量。
info.reslen: 查詢結(jié)果字節(jié)長(zhǎng)度。
info.nreturnned: 查詢返回文檔數(shù)。
millis: 操作耗時(shí)(毫秒)。
很顯然,該操作掃描的文檔過(guò)多(info.nscanned),通常是沒(méi)有使用索引造成的。我們用 explain() 看看服務(wù)器如何執(zhí)行執(zhí)行該命令。
> db.users.find().sort({age:-1}).limit(10000).explain()
{
"cursor" : "BasicCursor",
"nscanned" : 1000000,
"nscannedObjects" : 1000000,
"n" : 10000,
"scanAndOrder" : true,
"millis" : 1412,
"indexBounds" : {
}
}
沒(méi)有索引自然很慢了,建個(gè)索引看看效果。
> db.users.ensureIndex({age:-1})
> db.users.find().sort({age:-1}).limit(10000).explain()
{
"cursor" : "BtreeCursor age_-1",
"nscanned" : 10000,
"nscannedObjects" : 10000,
"n" : 10000,
"millis" : 211,
"indexBounds" : {
"age" : [
[
{
"$maxElement" : 1
},
{
"$minElement" : 1
}
]
]
}
}
速度提升非常明顯。最后別忘了 Profiler 本身也會(huì)影響服務(wù)器性能,不用的時(shí)候要關(guān)掉。
> db.setProfilingLevel(0)
{ "was" : 1, "ok" : 1 }
除了使用 setProfilingLevel 命令外,也可以在 mongod 參數(shù)中啟用 profiler,不推薦。
--profile arg 0=off 1=slow, 2=all
--slowms arg (=100) value of slow for profile and console log
2. Optimization
優(yōu)化建議:
如果 nscanned 遠(yuǎn)大于 nreturned,那么需要使用索引。
如果 reslen 返回字節(jié)非常大,那么考慮只獲取所需的字段。
執(zhí)行 update 操作時(shí)同樣檢查一下 nscanned,并使用索引減少文檔掃描數(shù)量。
使用 db.eval() 在服務(wù)端執(zhí)行某些統(tǒng)計(jì)操作。
減少返回文檔數(shù)量,使用 skip & limit 分頁(yè)。
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)