Mongodb索引和explain分析查詢速度

Mongodb索引和explain分析查詢速度

window

cmd

mongo
MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { “id” : UUID(“7caa8fcf-c4d4-476d-90d9-884e2d27b38b”) }
MongoDB server version: 4.2.1
WARNING: shell and server versions do not match
Server has startup warnings:
2019-10-31T16:12:54.220+0800 I CONTROL [initandlisten]
2019-10-31T16:12:54.221+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-10-31T16:12:54.221+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-10-31T16:12:54.221+0800 I CONTROL [initandlisten]
Enable MongoDB’s free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()

查看索引

show dbs;

admin 0.000GB
comment 0.000GB
config 0.000GB
itying 0.000GB
local 0.000GB
test 0.000GB

use itying

switched to db itying

show collections;

admin
blog
user

db.blog.find();

{ “_id” : ObjectId(“5dc2d71dd6f4c884d9bb6812”), “title” : “標題” }
{ “_id” : ObjectId(“5dc2d737d6f4c884d9bb6813”), “name” : “哈哈” }
{ “_id” : ObjectId(“5dc2d771d6f4c884d9bb6814”), “name” : “哈哈”, “title” : “望” }

db.blog.getIndexes();

[
{
“v” : 2,
“key” : {
“_id” : 1
},
“name” : “id”,
“ns” : “itying.blog”
}
]

增加索引

db.表名.ensureIndex({“字段”:1});

db.blog.ensureIndex({“title”:1});

{
“createdCollectionAutomatically” : false,
“numIndexesBefore” : 1,
“numIndexesAfter” : 2,
“ok” : 1
}

db.blog.getIndexes();

[
{
“v” : 2,
“key” : {
“_id” : 1
},
“name” : “id”,
“ns” : “itying.blog”
},
{
“v” : 2,
“key” : {
“title” : 1
},
“name” : “title_1”,
“ns” : “itying.blog”
}
]

唯一索引

db.表名.ensureIndex({“字段”:1},{“unique”:true});

db.user.find();

{ “_id” : ObjectId(“5dc2d61bd6f4c884d9bb6810”), “name” : “zhang”, “age” : 30 }

db.user.ensureIndex({“name”:1},{“unique”:true});

{
“createdCollectionAutomatically” : false,
“numIndexesBefore” : 1,
“numIndexesAfter” : 2,
“ok” : 1
}

db.user.insert({“name”:“zhang”});

WriteResult({
“nInserted” : 0,
“writeError” : {
“code” : 11000,
“errmsg” : “E11000 duplicate key error collection: itying.user index: name_1 dup key: { name: “zhang” }”
}
})

db.user.insert({“name”:“zhang1”});

WriteResult({ “nInserted” : 1 })

db.user.find();

{ “_id” : ObjectId(“5dc2d61bd6f4c884d9bb6810”), “name” : “zhang”, “age” : 30 }
{ “_id” : ObjectId(“5dc435a837f8fe85132e5dcf”), “name” : “zhang1” }

刪除索引

db.表名.dropIndex({“字段”:1});

db.blog.dropIndex({“title”:1});

{ “nIndexesWas” : 2, “ok” : 1 }

db.blog.getIndexes();

[
{
“v” : 2,
“key” : {
“_id” : 1
},
“name” : “id”,
“ns” : “itying.blog”
}
]

命令行循環添加數據

for (let i = 0; i < 10000; i++) {
… db.blog.insert({“title”:“title”+i,“name”:“name”+i,“content”:“content”+i})}

WriteResult({ “nInserted” : 1 })

db.blog.find().count();

10003

db.blog.find().limit(10);

{ “_id” : ObjectId(“5dc2d71dd6f4c884d9bb6812”), “title” : “標題” }
{ “_id” : ObjectId(“5dc2d737d6f4c884d9bb6813”), “name” : “哈哈” }
{ “_id” : ObjectId(“5dc2d771d6f4c884d9bb6814”), “name” : “哈哈”, “title” : “望” }
{ “_id” : ObjectId(“5dc42bdf37f8fe85131f1b90”), “title” : “title0”, “name” : “name0”, “content” : “content0” }
{ “_id” : ObjectId(“5dc42bdf37f8fe85131f1b91”), “title” : “title1”, “name” : “name1”, “content” : “content1” }
{ “_id” : ObjectId(“5dc42bdf37f8fe85131f1b92”), “title” : “title2”, “name” : “name2”, “content” : “content2” }
{ “_id” : ObjectId(“5dc42bdf37f8fe85131f1b93”), “title” : “title3”, “name” : “name3”, “content” : “content3” }
{ “_id” : ObjectId(“5dc42bdf37f8fe85131f1b94”), “title” : “title4”, “name” : “name4”, “content” : “content4” }
{ “_id” : ObjectId(“5dc42bdf37f8fe85131f1b95”), “title” : “title5”, “name” : “name5”, “content” : “content5” }
{ “_id” : ObjectId(“5dc42bdf37f8fe85131f1b96”), “title” : “title6”, “name” : “name6”, “content” : “content6” }

for (let i = 100001; i < 1000000; i++) {
db.blog.insert({“title”:“title”+i,“name”:“name”+i,“content”:“content”+i})}

WriteResult({ “nInserted” : 1 })

db.blog.find().count();

1000001

複合索引

db.表名.ensureIndex(“字段1”:1,“字段2”:-1);1表示索引按升序存儲,-1表示索引按照降序存儲。
複合索引中條件使用字段1或字段1和字段2會使用到索引;條件使用字段2不會使用到索引。

explain使用

db.blog.find({“title”:“title888”}).explain(“executionStats”);

{
“queryPlanner” : {
“plannerVersion” : 1,
“namespace” : “itying.blog”,
“indexFilterSet” : false,
“parsedQuery” : {
“title” : {
KaTeX parse error: Expected 'EOF', got '}' at position 42: … }̲ …eq” : “title888”
}
},
“direction” : “forward”
},
“rejectedPlans” : [ ]
},
“executionStats” : {
“executionSuccess” : true,
“nReturned” : 1,
“executionTimeMillis” : 5,
“totalKeysExamined” : 0,
“totalDocsExamined” : 10003,
“executionStages” : {
“stage” : “COLLSCAN”,
“filter” : {
“title” : {
“$eq” : “title888”
}
},
“nReturned” : 1,
“executionTimeMillisEstimate” : 0,
“works” : 10005,
“advanced” : 1,
“needTime” : 10003,
“needYield” : 0,
“saveState” : 78,
“restoreState” : 78,
“isEOF” : 1,
“direction” : “forward”,
“docsExamined” : 10003
}
},
“serverInfo” : {
“host” : “aha”,
“port” : 27017,
“version” : “4.2.1”,
“gitVersion” : “edf6d45851c0b9ee15548f0f847df141764a317e”
},
“ok” : 1
}

沒有索引,數據增到100萬

db.blog.find({“title”:“title8888”}).explain(“executionStats”);

{
“queryPlanner” : {
“plannerVersion” : 1,
“namespace” : “itying.blog”,
“indexFilterSet” : false,
“parsedQuery” : {
“title” : {
KaTeX parse error: Expected 'EOF', got '}' at position 43: … }̲ …eq” : “title8888”
}
},
“direction” : “forward”
},
“rejectedPlans” : [ ]
},
“executionStats” : {
“executionSuccess” : true,
“nReturned” : 1,
“executionTimeMillis” : 460,
“totalKeysExamined” : 0,
“totalDocsExamined” : 1000001,
“executionStages” : {
“stage” : “COLLSCAN”,
“filter” : {
“title” : {
“$eq” : “title8888”
}
},
“nReturned” : 1,
“executionTimeMillisEstimate” : 3,
“works” : 1000003,
“advanced” : 1,
“needTime” : 1000001,
“needYield” : 0,
“saveState” : 7812,
“restoreState” : 7812,
“isEOF” : 1,
“direction” : “forward”,
“docsExamined” : 1000001
}
},
“serverInfo” : {
“host” : “aha”,
“port” : 27017,
“version” : “4.2.1”,
“gitVersion” : “edf6d45851c0b9ee15548f0f847df141764a317e”
},
“ok” : 1
}

增加索引後

db.blog.find({“title”:“title8889”}).explain(“executionStats”);

{
“queryPlanner” : {
“plannerVersion” : 1,
“namespace” : “itying.blog”,
“indexFilterSet” : false,
“parsedQuery” : {
“title” : {
“$eq” : “title8889”
}
},
“winningPlan” : {
“stage” : “FETCH”,
“inputStage” : {
“stage” : “IXSCAN”,
“keyPattern” : {
“title” : 1
},
“indexName” : “title_1”,
“isMultiKey” : false,
“multiKeyPaths” : {
“title” : [ ]
},
“isUnique” : false,
“isSparse” : false,
“isPartial” : false,
“indexVersion” : 2,
“direction” : “forward”,
“indexBounds” : {
“title” : [
“[“title8889”, “title8889”]”
]
}
}
},
“rejectedPlans” : [ ]
},
“executionStats” : {
“executionSuccess” : true,
“nReturned” : 1,
“executionTimeMillis” : 6,
“totalKeysExamined” : 1,
“totalDocsExamined” : 1,
“executionStages” : {
“stage” : “FETCH”,
“nReturned” : 1,
“executionTimeMillisEstimate” : 0,
“works” : 2,
“advanced” : 1,
“needTime” : 0,
“needYield” : 0,
“saveState” : 0,
“restoreState” : 0,
“isEOF” : 1,
“docsExamined” : 1,
“alreadyHasObj” : 0,
“inputStage” : {
“stage” : “IXSCAN”,
“nReturned” : 1,
“executionTimeMillisEstimate” : 0,
“works” : 2,
“advanced” : 1,
“needTime” : 0,
“needYield” : 0,
“saveState” : 0,
“restoreState” : 0,
“isEOF” : 1,
“keyPattern” : {
“title” : 1
},
“indexName” : “title_1”,
“isMultiKey” : false,
“multiKeyPaths” : {
“title” : [ ]
},
“isUnique” : false,
“isSparse” : false,
“isPartial” : false,
“indexVersion” : 2,
“direction” : “forward”,
“indexBounds” : {
“title” : [
“[“title8889”, “title8889”]”
]
},
“keysExamined” : 1,
“seeks” : 1,
“dupsTested” : 0,
“dupsDropped” : 0
}
}
},
“serverInfo” : {
“host” : “aha”,
“port” : 27017,
“version” : “4.2.1”,
“gitVersion” : “edf6d45851c0b9ee15548f0f847df141764a317e”
},
“ok” : 1
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章