mongodb可以通過(guò)profile來(lái)監(jiān)控?cái)?shù)據(jù),進(jìn)行優(yōu)化。
查看當(dāng)前是否開(kāi)啟profile功能用命令
db.getProfilingLevel() ?返回level等級(jí),值為0|1|2,分別代表意思:0代表關(guān)閉,1代表記錄慢命令,2代表全部
開(kāi)始profile功能為
db.setProfilingLevel(level); ?#level等級(jí),值同上
level為1的時(shí)候,慢命令默認(rèn)值為100ms,更改為db.setProfilingLevel(level,slowms)如db.setProfilingLevel(1,50)這樣就更改為50毫秒
通過(guò)db.system.profile.find() 查看當(dāng)前的監(jiān)控日志。
如:
1
|
> db.system.profile.
find
({millis:{$
gt
:500}})
|
2
|
{
"ts"
: ISODate(
"2011-07-23T02:50:13.941Z"
),
"info"
:
"query order.order reslen:11022 nscanned:672230? \nquery: { status: 1.0 } nreturned:101 bytes:11006 640ms"
,
"millis"
: 640 }
|
3
|
{
"ts"
: ISODate(
"2011-07-23T02:51:00.096Z"
),
"info"
:
"query order.order reslen:11146 nscanned:672302? \nquery: { status: 1.0, user.uid: { $gt: 1663199.0 } }? nreturned:101 bytes:11130 647ms"
,
"millis"
: 647 }
|
?
?
?這里值的含義是
? ts:命令執(zhí)行時(shí)間
info:命令的內(nèi)容
query:代表查詢
order.order: 代表查詢的庫(kù)與集合
reslen:返回的結(jié)果集大小,byte數(shù)
nscanned:掃描記錄數(shù)量
nquery:后面是查詢條件
nreturned:返回記錄數(shù)及用時(shí)
millis:所花時(shí)間
?
如果發(fā)現(xiàn)時(shí)間比較長(zhǎng),那么就需要作優(yōu)化。
比如nscanned數(shù)很大,或者接近記錄總數(shù),那么可能沒(méi)有用到索引查詢。
reslen很大,有可能返回沒(méi)必要的字段。
nreturned很大,那么有可能查詢的時(shí)候沒(méi)有加限制。
mongo可以通過(guò)db.serverStatus()查看mongod的運(yùn)行狀態(tài)
?
?
01
|
> db.serverStatus()
|
02
|
{
|
03
|
????
"host"
:
"baobao-laptop"
,
#主機(jī)名
|
04
|
????
"version"
:
"1.8.2"
,
#版本號(hào)
|
05
|
????
"process"
:
"mongod"
,
#進(jìn)程名
|
06
|
????
"uptime"
: 15549,
#運(yùn)行時(shí)間
|
07
|
????
"uptimeEstimate"
: 15351,
|
08
|
????
"localTime"
: ISODate(
"2011-07-23T06:07:31.220Z"
),當(dāng)前時(shí)間
|
09
|
????
"globalLock"
: {
|
10
|
????????
"totalTime"
: 15548525410,
#總運(yùn)行時(shí)間(ns)
|
11
|
????????
"lockTime"
: 89206633,?
#總的鎖時(shí)間(ns)
|
12
|
????????
"ratio"
: 0.005737305027178137,
#鎖比值
|
13
|
????????
"currentQueue"
: {
|
14
|
????????????
"total"
: 0,
#當(dāng)前需要執(zhí)行的隊(duì)列
|
15
|
????????????
"readers"
: 0,
#讀隊(duì)列
|
16
|
????????????
"writers"
: 0
#寫隊(duì)列
|
17
|
????????
},
|
18
|
????????
"activeClients"
: {
|
19
|
????????????
"total"
: 0,
#當(dāng)前客戶端執(zhí)行的鏈接數(shù)
|
20
|
????????????
"readers"
: 0,
#讀鏈接數(shù)
|
21
|
????????????
"writers"
: 0
#寫鏈接數(shù)
|
22
|
????????
}
|
23
|
????
},
|
24
|
????
"mem"
: {
#內(nèi)存情況
|
25
|
????????
"bits"
: 32,
#32位系統(tǒng)
|
26
|
????????
"resident"
: 337,
#占有物理內(nèi)存數(shù)
|
27
|
????????
"virtual"
: 599,
#占有虛擬內(nèi)存
|
28
|
????????
"supported"
:
true
,
#是否支持?jǐn)U展內(nèi)存
|
29
|
????????
"mapped"
: 512
|
30
|
????
},
|
31
|
????
"connections"
: {
|
32
|
????????
"current"
: 2,
#當(dāng)前鏈接數(shù)
|
33
|
????????
"available"
: 817
#可用鏈接數(shù)
|
34
|
????
},
|
35
|
????
"extra_info"
: {
|
36
|
????????
"note"
:
"fields vary by platform"
,
|
37
|
????????
"heap_usage_bytes"
: 159008,
#堆使用情況字節(jié)
|
38
|
????????
"page_faults"
: 907
#頁(yè)面故作
|
39
|
????
},
|
40
|
????
"indexCounters"
: {
|
41
|
????????
"btree"
: {
|
42
|
????????????
"accesses"
: 59963,
#索引被訪問(wèn)數(shù)
|
43
|
????????????
"hits"
: 59963,
#所以命中數(shù)
|
44
|
????????????
"misses"
: 0,
#索引偏差數(shù)
|
45
|
????????????
"resets"
: 0,
#復(fù)位數(shù)
|
46
|
????????????
"missRatio"
: 0
#未命中率
|
47
|
????????
}
|
48
|
????
},
|
49
|
????
"backgroundFlushing"
: {???
|
50
|
????????
"flushes"
: 259,?
#刷新次數(shù)
|
51
|
????????
"total_ms"
: 3395,
#刷新總花費(fèi)時(shí)長(zhǎng)
|
52
|
????????
"average_ms"
: 13.108108108108109,
#平均時(shí)長(zhǎng)
|
53
|
????????
"last_ms"
: 1,
#最后一次時(shí)長(zhǎng)
|
54
|
????????
"last_finished"
: ISODate(
"2011-07-23T06:07:22.725Z"
)
#最后刷新時(shí)間
|
55
|
????
},
|
56
|
????
"cursors"
: {
|
57
|
????????
"totalOpen"
: 0,
#打開(kāi)游標(biāo)數(shù)
|
58
|
????????
"clientCursors_size"
: 0,
#客戶端游標(biāo)大小
|
59
|
????????
"timedOut"
: 16
#超時(shí)時(shí)間
|
60
|
????
},
|
61
|
????
"network"
: {
|
62
|
????????
"bytesIn"
: 285676177,
#輸入數(shù)據(jù)(byte)
|
63
|
????????
"bytesOut"
: 286564,
#輸出數(shù)據(jù)(byte)
|
64
|
????????
"numRequests"
: 2012348
#請(qǐng)求數(shù)
|
65
|
????
},
|
66
|
????
"opcounters"
: {
|
67
|
????????
"insert"
: 2010000,
#插入操作數(shù)
|
68
|
????????
"query"
: 51,
#查詢操作數(shù)
|
69
|
????????
"update"
: 5,
#更新操作數(shù)
|
70
|
????????
"delete"
: 0,
#刪除操作數(shù)
|
71
|
????????
"getmore"
: 0,
#獲取更多的操作數(shù)
|
72
|
????????
"command"
: 148
#其他命令操作數(shù)
|
73
|
????
},
|
74
|
????
"asserts"
: {
#各個(gè)斷言的數(shù)量
|
75
|
????????
"regular"
: 0,
|
76
|
????????
"warning"
: 0,
|
77
|
????????
"msg"
: 0,
|
78
|
????????
"user"
: 2131,
|
79
|
????????
"rollovers"
: 0
|
80
|
????
},
|
81
|
????
"writeBacksQueued"
:
false
,
|
82
|
????
"ok"
: 1
|
83
|
}
|
?
db.stats()查看某一個(gè)庫(kù)的原先狀況
?
01
|
> db.stats()
|
02
|
{
|
03
|
????
"db"
:
"order"
,
#庫(kù)名
|
04
|
????
"collections"
: 4,
#集合數(shù)
|
05
|
????
"objects"
: 2011622,
#記錄數(shù)
|
06
|
????
"avgObjSize"
: 111.92214441878245,
#每條記錄的平均值
|
07
|
????
"dataSize"
: 225145048,
#記錄的總大小
|
08
|
????
"storageSize"
: 307323392,
#預(yù)分配的存儲(chǔ)空間
|
09
|
????
"numExtents"
: 21,
#事件數(shù)
|
10
|
????
"indexes"
: 1,
#索引數(shù)
|
11
|
????
"indexSize"
: 74187744,
#所以大小
|
12
|
????
"fileSize"
: 1056702464,
#文件大小
|
13
|
????
"ok"
: 1
|
14
|
}
|
查看集合記錄用
?
01
|
> db.order.stats()
|
02
|
{
|
03
|
????
"ns"
:
"order.order"
,
#命名空間
|
04
|
????
"count"
: 2010000,
#記錄數(shù)
|
05
|
????
"size"
: 225039600,
#大小
|
06
|
????
"avgObjSize"
: 111.96,
|
07
|
????
"storageSize"
: 307186944,
|
08
|
????
"numExtents"
: 18,
|
09
|
????
"nindexes"
: 1,
|
10
|
????
"lastExtentSize"
: 56089856,
|
11
|
????
"paddingFactor"
: 1,
|
12
|
????
"flags"
: 1,
|
13
|
????
"totalIndexSize"
: 74187744,
|
14
|
????
"indexSizes"
: {
|
15
|
????????
"_id_"
: 74187744
#索引為_(kāi)id_的索引大小
|
16
|
????
},
|
17
|
????
"ok"
: 1
|
18
|
}
|
mongostat命令查看運(yùn)行中的實(shí)時(shí)統(tǒng)計(jì),表示每秒實(shí)時(shí)執(zhí)行的次數(shù)
mongodb還提供了一個(gè)機(jī)遇http的監(jiān)控頁(yè)面,可以訪問(wèn)http://ip:28017來(lái)查看,這個(gè)頁(yè)面基本上是對(duì)上面的這些命令做了一下綜合,所以這里不細(xì)述了。
二.mongodb的優(yōu)化
根據(jù)上面這些監(jiān)控手段,找到問(wèn)題后,我們可以進(jìn)行優(yōu)化
上面找到了某一下慢的命令,現(xiàn)在我們可以通過(guò)執(zhí)行計(jì)劃跟蹤一下,如
01
|
> db.order.
find
({
"status"
: 1.0,
"user.uid"
: { $
gt
: 2663199.0 } }).explain()
|
02
|
{
|
03
|
????
"cursor"
:
"BasicCursor"
,
#游標(biāo)類型
|
04
|
????
"nscanned"
: 2010000,
#掃描數(shù)量
|
05
|
????
"nscannedObjects"
: 2010000,
#掃描對(duì)象
|
06
|
????
"n"
: 337800,
#返回?cái)?shù)據(jù)
|
07
|
????
"millis"
: 2838,
#耗時(shí)
|
08
|
????
"nYields"
: 0,
|
09
|
????
"nChunkSkips"
: 0,
|
10
|
????
"isMultiKey"
:
false
,
|
11
|
????
"indexOnly"
:
false
,
|
12
|
????
"indexBounds"
: {
#使用索引(這里沒(méi)有)
|
13
|
????????
?
|
14
|
????
}
|
15
|
}
|
對(duì)于這樣的,我們可以創(chuàng)建索引
可以通過(guò) ?db.collection.ensureIndex({"字段名":1}) 來(lái)創(chuàng)建索引,1為升序,-1為降序,在已經(jīng)有多數(shù)據(jù)的情況下,可用后臺(tái)來(lái)執(zhí)行,語(yǔ)句db.collection.ensureIndex({"字段名":1} , {backgroud:true})?
獲取索引用db.collection.getIndexes() 查看
這里我們創(chuàng)建一個(gè)user.uid的索引 >db.order.ensureIndex({"user.uid":1})
創(chuàng)建后重新執(zhí)行
01
|
db.order.
find
({
"status"
: 1.0,
"user.uid"
: { $
gt
: 2663199.0 } }).explain()
|
02
|
{
|
03
|
????
"cursor"
:
"BtreeCursor user.uid_1"
,
|
04
|
????
"nscanned"
: 337800,
|
05
|
????
"nscannedObjects"
: 337800,
|
06
|
????
"n"
: 337800,
|
07
|
????
"millis"
: 1371,
|
08
|
????
"nYields"
: 0,
|
09
|
????
"nChunkSkips"
: 0,
|
10
|
????
"isMultiKey"
:
false
,
|
11
|
????
"indexOnly"
:
false
,
|
12
|
????
"indexBounds"
: {
|
13
|
????????
"user.uid"
: [
|
14
|
????????????
[
|
15
|
????????????????
2663199,
|
16
|
????????????????
1.7976931348623157e+308
|
17
|
????????????
]
|
18
|
????????
]
|
19
|
????
}
|
20
|
}
|
掃描數(shù)量減少,速度提高。mongodb的索引設(shè)計(jì)類似與關(guān)系數(shù)據(jù)庫(kù),按索引查找加快書(shū)讀,但是多了會(huì)對(duì)寫有壓力,所以這里就不再敘述了。
?
2.其他優(yōu)化可以用hint強(qiáng)制索引查找,返回只是需要的數(shù)據(jù),對(duì)數(shù)據(jù)分頁(yè)等。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
