筆記 mongo查詢慢日誌,建立索引
1.檢視mongodb慢日誌是否開起
use LogDB;
db.getProfilingStatus();
2.開啟慢日誌,設定超過100毫秒的操作為慢操作
db.setProfilingLevel(1,100);
3.檢視慢日誌內容
db.system.profile.find().sort({$natural:-1})
日誌,
shard3:PRIMARY> db.system.profile.find().sort({$natural:-1})
{ "op" : "query", "ns" : "LogDB.Flashxxx", "command" : { "find" : "Flashxxx", "filter" : { "UserId" : { "$in" : [ "1111111" ] } }, "shardVersion" : [ Timestamp(0, 0), ObjectId("000000000000000000000000") ], "lsid" : { "id" : UUID("d08179ba-ecbd-4f9d-92e7-cd491e5a1d8a"), "uid" : BinData(0,"iwsImy7Wfk2mAp4o/uuD+I9mpETQTxb5PXo26pIQkS4=") }, "$clusterTime" : { "clusterTime" : Timestamp(1558696199, 3), "signature" : { "hash" : BinData(0,"Ht7EWhYpVLiy+DbH8Cu1Ijs6PNk="), "keyId" : NumberLong("6647500774429425691") } }, "$client" : { "driver" : { "name" : "mongo-csharp-driver", "version" : "2.7.0.0" }, "os" : { "type" : "Windows", "name" : "Microsoft Windows 6.3.9600", "architecture" : "x86_64", "version" : "6.3.9600" }, "platform" : ".NET Framework 4.0.0.0", "mongos" : { "host" : "new-mongo01:20000", "client" : "10.205.33.32:59518", "version" : "3.6.9" } }, "$configServerState" : { "opTime" : { "ts" : Timestamp(1558696196, 12), "t" : NumberLong(3) } }, "$db" : "LogDB" }, "keysExamined" : 0, "docsExamined" : 37428, "cursorExhausted" : true, "numYield" : 292, "locks" : { "Global" : { "acquireCount" : { "r" : NumberLong(586) } }, "Database" : { "acquireCount" : { "r" : NumberLong(293) } }, "Collection" : { "acquireCount" : { "r" : NumberLong(293) } } }, "nreturned" : 1, "responseLength" : 673, "protocol" : "op_msg", "millis" : 104, "planSummary" : "COLLSCAN", "execStats" : { "stage" : "COLLSCAN", "filter" : { "UserId" : { "$eq" : "11111111115" } }, "nReturned" : 1, "executionTimeMillisEstimate" : 100, "works" : 37430, "advanced" : 1, "needTime" : 37428, "needYield" : 0, "saveState" : 292, "restoreState" : 292, "isEOF" : 1, "invalidates" : 0, "direction" : "forward", "docsExamined" : 37428 }, "ts" : ISODate("2019-05-24T11:09:59.346Z"), "client" : "10.205.34.91", "allUsers" : [ { "user" : "__system", "db" : "local" } ], "user" : "__system@local" }
注意COLLSCAN 是全表掃描。
檢視執行計劃:
db.FlashClientData.find({UserId: 1000493111}).explain()
shard3:PRIMARY> db.FlashClientData.find({UserId: 100049111}).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "LogDB.Flashxxx",
"indexFilterSet" : false,
"parsedQuery" : {
"UserId" : {
"$eq" : 10x0493 111
}
},
"winningPlan" : {
"stage" : "COLLSCAN",<<<<<<<<----全表掃描
"filter" : {
"UserId" : {
"$eq" : 10x0493111
}
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
建立索引:
db.Flashxxx.createIndex( {UserId: 1} ) // 按age欄位建立升序索引
db.Flashxxx.getIndexes() // 查詢集合的索引資訊
shard3:PRIMARY> db.FlashClientData.find({UserId: 100049111}).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "LogDB.FlashClientData",
"indexFilterSet" : false,
"parsedQuery" : {
"UserId" : {
"$eq" : 100049111
}
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",,<<<<<<<<
"keyPattern" : {
"UserId" : 1
},
"indexName" : "UserId_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"UserId" : [ ]
},
"isUnique" : false,
刪除指定的索引dropIndex()
db.COLLECTION_NAME.dropIndex("INDEX-NAME")
如,刪除集合sites中名為"name_1_domain_-1"的索引:
單欄位索引 (Single Field Index)
db.person.createIndex( {age: 1} )
{age: 1} 代表升序索引,也可以透過{age: -1}來指定降序索引,對於單欄位索引,升序/降序效果是一樣的。
單索引建立唯一索引,如:
db.persons.createIndex({name:1},{unique:true})
複合索引 (Compound Index)
db.person.createIndex( {age: 1, name: 1} )
多key索引 (Multikey Index)
{"name" : "jack", "age" : 19, habbit: ["football, runnning"]}
db.person.createIndex( {habbit: 1} ) // 自動建立多key索引
db.person.find( {habbit: "football"} )
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29863023/viewspace-2642327/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL:慢查詢日誌MySql
- 使用慢查詢日誌
- mysql慢查詢日誌MySql
- 慢查詢日誌的管理
- 關於MySQL 通用查詢日誌和慢查詢日誌分析MySql
- PHP慢指令碼日誌和Mysql的慢查詢日誌PHP指令碼MySql
- 開啟查詢慢查詢日誌引數
- 慢查詢日誌開啟分析
- 【MySQL】慢查詢日誌不列印MySql
- mysqlsla 分析mysql慢查詢日誌MySql
- MySQL 慢查詢日誌——讓“慢”無所遁形MySql
- Linux下mysql配置慢日誌查詢,把查詢慢的sql記錄下來LinuxMySql
- Redis慢查詢日誌學習功能Redis
- mysql之 slow log 慢查詢日誌MySql
- MySQL Slow Query log(慢查詢日誌)MySql
- mysql慢查詢日誌分析工具使用MySql
- Mysql慢查詢日誌分析工具mysqlslaMySql
- 【MySql】 慢日誌查詢工具之mysqlslaMySql
- MySQL慢查詢日誌相關設定MySql
- 【轉載】MySQL慢查詢日誌總結MySql
- mysql慢查詢和錯誤日誌分析MySql
- 線上安全清空慢查詢日誌slowlog
- MYSQL開啟慢查詢日誌實施MySql
- mysql開啟檢視慢查詢日誌MySql
- 用命令過濾MySQL慢查詢日誌MySql
- Mysql慢查詢日誌檔案轉ExcelMySqlExcel
- laravelS 記錄accesslog日誌,服務慢查詢預警Laravel
- MySQLSlowlog慢查詢日誌的配置以及清空日誌內容MySql
- MongoDB慢查詢與索引MongoDB索引
- 記錄 sql 查詢日誌SQL
- 對 MySQL 慢查詢日誌的簡單分析MySql
- 如何在MySQL中開啟慢查詢日誌?MySql
- mysql分析慢查詢日誌工具mysqlsla安裝MySql
- Mysql 啟動慢查詢日誌 (不用重啟)MySql
- 分析mysql慢查詢日誌的好工具--mysqlslaMySql
- 【MySql】mysql 慢日誌查詢工具之mysqldumpslowMySql
- MySQL資料庫中的日誌檔案---(3)慢查詢日誌MySql資料庫
- 資料庫MySQL一般查詢日誌或者慢查詢日誌歷史資料的清理資料庫MySql