摘要
上篇文章介紹了mapReduce這個聚合操作。本篇將繼續學習,db有了,collection和document也有,基本上夠用了,但是隨着項目上線后,發現業務數據越來越多,查詢效率越來越慢,這時就需要分析慢查詢記錄了。如何開啟慢查詢記錄?就是本篇文章介紹的內容了。
相關文章
[MongoDB]索引
Profiling
首先添加測試數據,添加100w吧。
插入時間比較長,你可以通過服務端,查看日志
時間比較久,就插入這么多吧,能說明問題就行
首先需要分析是否需要建立索引,之前的版本可以通過expalin函數進行查看,不過當前使用的版本,通過該函數給出的結果是下面的情況。通過下圖的indexFilterSet只能看到沒有使用索引,其他的信息並不能幫到我們。
下面這張是@一線碼農 園友的圖片 可以對比一下 原文:http://www.cnblogs.com/huangxincheng/archive/2012/02/29/2372699.html
不過還有另外的一種方式,通過Profiling ,可以在服務端啟動的時候加上該參數,–profile=級別。
也可以通過客戶端db.setProfilingLevel(級別) 命令來實時配置。可以通過db.getProfilingLevel()命令來獲取當前的Profile級別。
level有三種級別
0 – 不開啟
1 – 記錄慢命令 (默認為>100ms)
2 – 記錄所有命令
參數為1的時候,默認的慢命令是大於100ms,當然也可以進行設置
db.setProfilingLevel( level , slowms )
db.setProfilingLevel( 1 , 120 );
Mongodb Profile 記錄是直接存在系統db里的,記錄位置 system.profile ,我們只要查詢這個Collection的記錄就可以獲取到我們的 Profile 記錄了。
執行查詢,然后執行profile
> db.users.find({"name":"wolfy"+66666}) { "_id" : ObjectId("5752486fc74b6bdc94876d95"), "name" : "wolfy66666", "age" : 13471 } > db.system.profile.find()
分析結果
{ "op": "query", "ns": "test.system.profile", "query": { "find": "system.profile", "filter": { } }, "keysExamined": 0, "docsExamined": 0, "cursorExhausted": true, "keyUpdates": 0, "writeConflicts": 0, "numYield": 0, "locks": { "Global": { "acquireCount": { "r": NumberLong(2) } }, "Database": { "acquireCount": { "r": NumberLong(1) } }, "Collection": { "acquireCount": { "r": NumberLong(1) } } }, "nreturned": 0, "responseLength": 110, "protocol": "op_command", "millis": 0, "execStats": { "stage": "COLLSCAN", "filter": { "$and": [ ] }, "nReturned": 0, "executionTimeMillisEstimate": 0, "works": 2, "advanced": 0, "needTime": 1, "needYield": 0, "saveState": 0, "restoreState": 0, "isEOF": 1, "invalidates": 0, "direction": "forward", "docsExamined": 0 }, "ts": ISODate("2016-06-04T03:56:35.706Z"), "client": "127.0.0.1", "allUsers": [ ], "user": "" }{ "op": "query", "ns": "test.users", "query": { "find": "users", "filter": { "name": "wolfy66666" } }, "keysExamined": 0, "docsExamined": 866283, "cursorExhausted": true, "keyUpdates": 0, "writeConflicts": 0, "numYield": 6767, "locks": { "Global": { "acquireCount": { "r": NumberLong(13536) } }, "Database": { "acquireCount": { "r": NumberLong(6768) } }, "Collection": { "acquireCount": { "r": NumberLong(6768) } } }, "nreturned": 1, "responseLength": 160, "protocol": "op_command", "millis": 339, "execStats": { "stage": "COLLSCAN", "filter": { "name": { "$eq": "wolfy66666" } }, "nReturned": 1, "executionTimeMillisEstimate": 310, "works": 866285, "advanced": 1, "needTime": 866283, "needYield": 0, "saveState": 6767, "restoreState": 6767, "isEOF": 1, "invalidates": 0, "direction": "forward", "docsExamined": 866283 }, "ts": ISODate("2016-06-04T03:57:10.206Z"), "client": "127.0.0.1", "allUsers": [ ], "user": "" }
通過下面的命令可以查看最新的記錄
db.system.profile.find().sort({$natural:-1})
還有一種更簡潔的查看方式
show profile
該命令可以查看最近的5條記錄
profile提供的信息內容解釋
ts:該命令在何時執行。
millis:執行耗時,以毫秒為單位。
op:什么操作。
query:設置的查詢條件。
nReturned:返回的條數。
docsExamined:文檔掃描條數。
總結
上面列舉了profile的使用方法,以及常用的幾個分析結果進行解釋。感興趣的可以使用增刪改查練練手,嘗試一下。