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

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
}
}
}
主站蜘蛛池模板: 讷河市| 兴仁县| 巨野县| 金乡县| 胶南市| 永修县| 顺义区| 新和县| 巫溪县| 孟连| 射洪县| 来安县| 克拉玛依市| 麦盖提县| 黎川县| 平定县| 分宜县| 从化市| 游戏| 泗水县| 罗山县| 区。| 灌云县| 巫山县| 克什克腾旗| 岑溪市| 紫金县| 钦州市| 巴东县| 应城市| 海南省| 江山市| 容城县| 潞西市| 田东县| 松阳县| 玉树县| 建水县| 洛宁县| 丹江口市| 临颍县|