【MongoDB】MongoDB之最佳化器Profiler,mongodbprofiler

來源:互聯網
上載者:User

【MongoDB】MongoDB之最佳化器Profiler,mongodbprofiler

在mysql資料庫中,慢查詢日誌經常作為最佳化資料庫的依據, mongodb中依然有類似的功能。Mongodb內建的profiler,可以方便地記錄所有耗時的操作,以便於調優;

一、開始profiler功能

開啟profier功能有兩種:

第一種就是直接在啟動參數裡面進行設定,就在茄冬mongodb時候添加-profile=層級

第二種就是在用戶端執行“db.setProfilingLevel(層級)”命令

profiler資訊儲存在system.profile表中,可以通過執行“db.getProfilingLevel()”命令來擷取當前profiler層級來:




在可以看到,level總共有三個參數,0是關閉,1是慢查詢,2是所有的。如果設定為2表示所有的語句都會記錄到log中。慢查詢的預設時間是100ms,當然也可以通過參數slowsms進行設定。

二、查詢profiler記錄

mysql慢查詢日誌是儲存在磁碟上,而mongodb profiler記錄直接存在系統的db中。記錄到system.profile中。profile就是前面講過的capped collection集合。所以只要查詢這個collection的記錄就可以擷取profiler記錄的日誌,可以使用db.system.profile.find()的命令直接尋找執行時間大於某一限度的(如5ms)的profiler日誌;


開啟設定為100ms

<pre name="code" class="html">> db.students.find({'comment.aihao':'reading'}).limit(1){ "_id" : ObjectId("5485c80bdf41c6670aa8d51c"), "name" : "albert", "age" : 12, "comment" : { "aihao" : [ "basket", "reading" ], "relatives" : [ "parent", "brother" ] } }> db.system.profile.find().sort({$natural:-1}).limit(1){ "op" : "query", "ns" : "test.system.indexes", "query" : { "expireAfterSeconds" : { "$exists" : true } }, "ntoreturn" : 0, "ntoskip" : 0, "nscanned" : 7, "nscannedObjects" : 7, "keyUpdates" : 0, "numYield" : 0, "lockStats" : { "timeLockedMicros" : { "r" : NumberLong(159), "w" : NumberLong(0) }, "timeAcquiringMicros" : { "r" : NumberLong(4), "w" : NumberLong(6) } }, "nreturned" : 0, "responseLength" : 20, "millis" : 0, "execStats" : { "type" : "COLLSCAN", "works" : 9, "yields" : 0, "unyields" : 0, "invalidates" : 0, "advanced" : 0, "needTime" : 8, "needFetch" : 0, "isEOF" : 1, "docsTested" : 7, "children" : [ ] }, "ts" : ISODate("2014-12-08T15:54:47.377Z"), "client" : "0.0.0.0", "allUsers" : [ { "user" : "__system", "db" : "local" } ], "user" : "__system@local" }


通過執行上面語句,可以看出在system.profile中記錄了詳細的查詢資訊。主要欄位說明:

1: ts 命令在何時執行

2: info 命令的詳細資料

3:reslen: 返回結果集的大小

4: nscanned:本次查詢掃描的記錄數

5: nreturned: 本次查詢實際返回的結果集

6: mills:該命令的執行耗時(單位:毫秒)


可以利用命令查詢特定條件的值:例如:查詢執行時間大於4ms的查詢語句:





相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.