官术网_书友最值得收藏!

How to do it...

  1. Assuming you have already created an index on the city field, create one by executing the command db.mockdata.createIndex({'city': 1}) again.
  2. Run a find() query:
 > plan = db.mockdata.find({city:'Boston', first_name: 'Sara'}).explain("executionStats")
  1. 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
}
}
}

  1. Now drop this index:
 > db.mockdata.dropIndex('city_1')

You should see an output similar to this:

 { "nIndexesWas" : 2, "ok" : 1 }
  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
}
  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
}
}
}
主站蜘蛛池模板: 咸阳市| 博客| 嘉荫县| 抚远县| 凤阳县| 榆社县| 丰镇市| 肃北| 刚察县| 通渭县| 凤冈县| 郁南县| 故城县| 丹棱县| 沈阳市| 西乌珠穆沁旗| 旌德县| 海原县| 镇安县| 图木舒克市| 桦川县| 清镇市| 台北县| 南康市| 泸西县| 耒阳市| 六枝特区| 阜阳市| 甘南县| 岱山县| 石楼县| 齐齐哈尔市| 大邑县| 广河县| 荃湾区| 南江县| 滨州市| 嵩明县| 连城县| 抚顺县| 伊通|