您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)mongo如何查詢慢日志以及創(chuàng)建索引的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
1.查看mongodb慢日志是否開(kāi)起
use LogDB;
db.getProfilingStatus();
2.開(kāi)啟慢日志,設(shè)置超過(guò)100毫秒的操作為慢操作
db.setProfilingLevel(1,100);
3.查看慢日志內(nèi)容
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 是全表掃描。
查看執(zhí)行計(jì)劃:
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" : 10x0493111
}
},
"winningPlan" : {
"stage" : "COLLSCAN",<<<<<<<<----全表掃描
"filter" : {
"UserId" : {
"$eq" : 10x0493111
}
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
創(chuàng)建索引:
db.Flashxxx.createIndex( {UserId: 1} ) // 按age字段創(chuàng)建升序索引
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} 代表升序索引,也可以通過(guò){age: -1}來(lái)指定降序索引,對(duì)于單字段索引,升序/降序效果是一樣的。
單索引創(chuàng)建唯一索引,如:
db.persons.createIndex({name:1},{unique:true})
復(fù)合索引 (Compound Index)
db.person.createIndex( {age: 1, name: 1} )
多key索引 (Multikey Index)
{"name" : "jack", "age" : 19, habbit: ["football, runnning"]}
db.person.createIndex( {habbit: 1} ) // 自動(dòng)創(chuàng)建多key索引
db.person.find( {habbit: "football"} )
感謝各位的閱讀!關(guān)于“mongo如何查詢慢日志以及創(chuàng)建索引”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。