- MongoDB Administrator’s Guide
- Cyrus Dasadia
- 415字
- 2021-07-02 15:47:49
How to do it...
- Assuming you have already created an index on the city field, create one by executing the command db.mockdata.createIndex({'city': 1}) again.
- Run a find() query:
> plan = db.mockdata.find({city:'Boston', first_name: 'Sara'}).explain("executionStats")
- Examine the executionStats:
> plan['executionStats']
You should see the following result:
{
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 0,
"totalKeysExamined" : 9,
"totalDocsExamined" : 9,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"first_name" : {
"$eq" : "Sara"
}
},
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 10,
"advanced" : 1,
"needTime" : 8,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 9,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 9,
"executionTimeMillisEstimate" : 0,
"works" : 10,
"advanced" : 9,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"city" : 1
},
"indexName" : "city_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"city" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"city" : [
"[\"Boston\", \"Boston\"]"
]
},
"keysExamined" : 9,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
}
- Now drop this index:
> db.mockdata.dropIndex('city_1')
You should see an output similar to this:
{ "nIndexesWas" : 2, "ok" : 1 }
- Create a compound index on city and name:
> db.mockdata.createIndex({'city': 1, 'first_name': 1})
You should see an output similar to this:
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
- Let's run the same fetch query again and examine the plan:
> plan = db.mockdata.find({city:'Boston', first_name: 'Sara'}).explain("executionStats")
> plan['executionStats']
You should see an output similar to this:
{
"executionSuccess": true,
"nReturned": 1,
"executionTimeMillis": 0,
"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,
"invalidates": 0,
"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,
"invalidates": 0,
"keyPattern": {
"city": 1,
"first_name": 1
},
"indexName": "city_1_first_name_1",
"isMultiKey": false,
"multiKeyPaths": {
"city": [],
"first_name": []
},
"isUnique": false,
"isSparse": false,
"isPartial": false,
"indexVersion": 2,
"direction": "forward",
"indexBounds": {
"city": [
"[\"Boston\", \"Boston\"]"
],
"first_name": [
"[\"Sara\", \"Sara\"]"
]
},
"keysExamined": 1,
"seeks": 1,
"dupsTested": 0,
"dupsDropped": 0,
"seenInvalidated": 0
}
}
}
推薦閱讀
- Spring技術內幕:深入解析Spring架構與設計
- Learning Bayesian Models with R
- Mastering Ubuntu Server
- Python高效開發實戰:Django、Tornado、Flask、Twisted(第3版)
- Visual Basic程序設計實驗指導(第4版)
- INSTANT Mercurial SCM Essentials How-to
- Mastering Python Networking
- 琢石成器:Windows環境下32位匯編語言程序設計
- 精通MATLAB(第3版)
- Access 2010數據庫應用技術(第2版)
- Visual Basic程序設計實驗指導(第二版)
- HTML5秘籍(第2版)
- 時空數據建模及其應用
- Hands-On JavaScript for Python Developers
- 大學計算機基礎實訓教程