即使只投影索引,MongoDB也不会生成覆盖查询
这是我正在使用的MongoDB驱动程序(Maven依赖项): 我运行了即使只投影索引,MongoDB也不会生成覆盖查询,mongodb,mongodb-query,Mongodb,Mongodb Query,这是我正在使用的MongoDB驱动程序(Maven依赖项): 我运行了db.collection.getIndexes(),它们实际上就在那里 [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "reddit.dump" }, { "v" : 2, "key" : {
db.collection.getIndexes()
,它们实际上就在那里
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "reddit.dump"
},
{
"v" : 2,
"key" : {
"created_utc" : 1
},
"name" : "created_utc_1",
"ns" : "reddit.dump"
},
{
"v" : 2,
"key" : {
"author" : 1,
"created_utc" : 1,
"subreddit" : 1
},
"name" : "author_1_created_utc_1_subreddit_1",
"ns" : "reddit.dump"
}
]
然后我提出了这个问题:
MongoCursor<Document> cursor = this.collection
.find(and(
gte("created_utc", this.lowerBound),
lt("created_utc", this.upperBound)))
.projection(fields(include("author", "created_utc", "subreddit"),
exclude("_id"))).iterator();
我得到一个IXSCAN
,它从FETCH
派生而来,并且totalDocsExamined
不是零。所以这不是一个覆盖查询。以下是解释的完整输出:
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "reddit.dump",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"created_utc" : {
"$lt" : 1543622401
}
},
{
"created_utc" : {
"$gt" : 1543622399
}
}
]
},
"winningPlan" : {
"stage" : "PROJECTION",
"transformBy" : {
"author" : 1,
"created_utc" : 1,
"subreddit" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"created_utc" : 1
},
"indexName" : "created_utc_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"created_utc" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"created_utc" : [
"(1543622399.0, 1543622401.0)"
]
}
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 44,
"executionTimeMillis" : 0,
"totalKeysExamined" : 44,
"totalDocsExamined" : 44,
"executionStages" : {
"stage" : "PROJECTION",
"nReturned" : 44,
"executionTimeMillisEstimate" : 0,
"works" : 45,
"advanced" : 44,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"transformBy" : {
"author" : 1,
"created_utc" : 1,
"subreddit" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "FETCH",
"nReturned" : 44,
"executionTimeMillisEstimate" : 0,
"works" : 45,
"advanced" : 44,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 44,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 44,
"executionTimeMillisEstimate" : 0,
"works" : 45,
"advanced" : 44,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"created_utc" : 1
},
"indexName" : "created_utc_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"created_utc" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"created_utc" : [
"(1543622399.0, 1543622401.0)"
]
},
"keysExamined" : 44,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
}
},
"serverInfo" : {
"host" : "user-System-Product-Name",
"port" : 27017,
"version" : "3.6.3",
"gitVersion" : "9586e557d54ef70f9ca4b43c26892cd55257e1a5"
},
"ok" : 1
}
如何进行覆盖查询以获得相同的结果
以下是我的收藏中的文档的外观:
{
"archived": false,
"author": "HRNNNGH",
"author_created_utc": 1533311589,
"author_flair_background_color": null,
"author_flair_css_class": null,
"author_flair_richtext": [],
"author_flair_template_id": null,
"author_flair_text": null,
"author_flair_text_color": null,
"author_flair_type": "text",
"author_fullname": "t2_1kcmg9n3",
"author_patreon_flair": false,
"body": "I LOVE THIS",
"can_gild": true,
"can_mod_post": false,
"collapsed": false,
"collapsed_reason": null,
"controversiality": 0,
"created_utc": 1543622400,
"distinguished": null,
"edited": false,
"gilded": 0,
"gildings": {
"gid_1": 0,
"gid_2": 0,
"gid_3": 0
},
"id": "eatm38p",
"is_submitter": false,
"link_id": "t3_a1vhzf",
"no_follow": false,
"parent_id": "t3_a1vhzf",
"permalink": "/r/ACPocketCamp/comments/a1vhzf/welcome_to_my_hair_salon/eatm38p/",
"removal_reason": null,
"retrieved_on": 1546258274,
"score": 4,
"send_replies": true,
"stickied": false,
"subreddit": "ACPocketCamp",
"subreddit_id": "t5_3pg1s",
"subreddit_name_prefixed": "r/ACPocketCamp",
"subreddit_type": "public"
}
下面是我希望通过覆盖查询实现的目标:
{ "author" : "MyPhantomAccount", "created_utc" : 1543622400, "subreddit" : "ireland" }
{ "author" : "ardentArcane", "created_utc" : 1543622400, "subreddit" : "heroesofthestorm" }
{ "author" : "bbrownabbb", "created_utc" : 1543622400, "subreddit" : "nonononoyes" }
{ "author" : "DANKNESSLEVEL-69", "created_utc" : 1543622400, "subreddit" : "memes" }
{ "author" : "HRNNNGH", "created_utc" : 1543622400, "subreddit" : "ACPocketCamp" }
{ "author" : "DomIstKrieg", "created_utc" : 1543622400, "subreddit" : "AskReddit" }
{ "author" : "befooks", "created_utc" : 1543622400, "subreddit" : "northernlion" }
{ "author" : "meekosmom", "created_utc" : 1543622400, "subreddit" : "raisedbynarcissists" }
{ "author" : "[deleted]", "created_utc" : 1543622400, "subreddit" : "wallstreetbets" }
{ "author" : "Red580", "created_utc" : 1543622400, "subreddit" : "ForwardsFromKlandma" }
{ "author" : "rauland", "created_utc" : 1543622400, "subreddit" : "melbourne" }
{ "author" : "Mr-Morrison94", "created_utc" : 1543622400, "subreddit" : "FortNiteBR" }
{ "author" : "huskiesofinternets", "created_utc" : 1543622400, "subreddit" : "aww" }
{ "author" : "AnimaCorpusLucra", "created_utc" : 1543622400, "subreddit" : "wallstreetbets" }
{ "author" : "Shadow14l", "created_utc" : 1543622400, "subreddit" : "financialindependence" }
{ "author" : "potrap", "created_utc" : 1543622400, "subreddit" : "popheads" }
{ "author" : "FireandBloodandStuff", "created_utc" : 1543622400, "subreddit" : "dankmemes" }
{ "author" : "XChihiro", "created_utc" : 1543622400, "subreddit" : "AskReddit" }
{ "author" : "bjacks12", "created_utc" : 1543622400, "subreddit" : "latterdaysaints" }
据我所知,MangGDB不会考虑这个查询的3字段索引,因为它没有“作者”来减少索引搜索空间(即使你是从所有作者那里得到的)。即使给出了一个使用3字段索引的方法,它仍然不会生成覆盖查询(我无法告诉您原因)
要获得覆盖查询,您需要重新创建三字段索引,并首先列出
created\u utc
据我所知,MunGDB不会考虑你的3字段索引,因为它没有“作者”来减少索引搜索空间(即使你是从所有作者那里得到的)。即使给出了一个使用3字段索引的方法,它仍然不会生成覆盖查询(我无法告诉您原因)
要获得覆盖查询,您需要重新创建三字段索引,并首先列出
created\u utc
如果你
覆盖查询是完全可以使用
索引,无需检查任何文档。索引包括
当以下两项都适用时进行查询:
- 查询中的所有字段都是索引的一部分,并且
- 结果中返回的所有字段都在同一索引中
- 查询中的所有字段都是索引的一部分,并且
- 结果中返回的所有字段都在同一索引中
没有明确说明,,但是仔细阅读这篇文章可以清楚地解释它意味着一个同时满足查询和投影的索引。解释说它使用的是三字段索引还是创建的仅utc索引?我已经将解释的完整输出添加到问题中解释说它使用的是三字段索引还是创建的仅utc索引?我已经添加了解释问题的完整输出非常有意义,涵盖的查询的全部要点是使用单个索引进行查找和结果;如果这些不一样,那么您已经失去了主要的好处。“要获得覆盖查询,您需要首先使用created_utc重新创建三字段索引。”这与预期的一样有效。完全有道理,覆盖查询的全部要点是使用单个索引进行查找和结果;如果这些不相同,则您已经失去了主要优势。“要获得覆盖查询,您需要重新创建三字段索引,并首先列出created_utc。”这与预期一样有效。
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "reddit.dump",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"created_utc" : {
"$lt" : 1543622401
}
},
{
"created_utc" : {
"$gt" : 1543622399
}
}
]
},
"winningPlan" : {
"stage" : "PROJECTION",
"transformBy" : {
"author" : 1,
"created_utc" : 1,
"subreddit" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"created_utc" : 1
},
"indexName" : "created_utc_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"created_utc" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"created_utc" : [
"(1543622399.0, 1543622401.0)"
]
}
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 44,
"executionTimeMillis" : 0,
"totalKeysExamined" : 44,
"totalDocsExamined" : 44,
"executionStages" : {
"stage" : "PROJECTION",
"nReturned" : 44,
"executionTimeMillisEstimate" : 0,
"works" : 45,
"advanced" : 44,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"transformBy" : {
"author" : 1,
"created_utc" : 1,
"subreddit" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "FETCH",
"nReturned" : 44,
"executionTimeMillisEstimate" : 0,
"works" : 45,
"advanced" : 44,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 44,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 44,
"executionTimeMillisEstimate" : 0,
"works" : 45,
"advanced" : 44,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"created_utc" : 1
},
"indexName" : "created_utc_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"created_utc" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"created_utc" : [
"(1543622399.0, 1543622401.0)"
]
},
"keysExamined" : 44,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
}
},
"serverInfo" : {
"host" : "user-System-Product-Name",
"port" : 27017,
"version" : "3.6.3",
"gitVersion" : "9586e557d54ef70f9ca4b43c26892cd55257e1a5"
},
"ok" : 1
}
{
"archived": false,
"author": "HRNNNGH",
"author_created_utc": 1533311589,
"author_flair_background_color": null,
"author_flair_css_class": null,
"author_flair_richtext": [],
"author_flair_template_id": null,
"author_flair_text": null,
"author_flair_text_color": null,
"author_flair_type": "text",
"author_fullname": "t2_1kcmg9n3",
"author_patreon_flair": false,
"body": "I LOVE THIS",
"can_gild": true,
"can_mod_post": false,
"collapsed": false,
"collapsed_reason": null,
"controversiality": 0,
"created_utc": 1543622400,
"distinguished": null,
"edited": false,
"gilded": 0,
"gildings": {
"gid_1": 0,
"gid_2": 0,
"gid_3": 0
},
"id": "eatm38p",
"is_submitter": false,
"link_id": "t3_a1vhzf",
"no_follow": false,
"parent_id": "t3_a1vhzf",
"permalink": "/r/ACPocketCamp/comments/a1vhzf/welcome_to_my_hair_salon/eatm38p/",
"removal_reason": null,
"retrieved_on": 1546258274,
"score": 4,
"send_replies": true,
"stickied": false,
"subreddit": "ACPocketCamp",
"subreddit_id": "t5_3pg1s",
"subreddit_name_prefixed": "r/ACPocketCamp",
"subreddit_type": "public"
}
{ "author" : "MyPhantomAccount", "created_utc" : 1543622400, "subreddit" : "ireland" }
{ "author" : "ardentArcane", "created_utc" : 1543622400, "subreddit" : "heroesofthestorm" }
{ "author" : "bbrownabbb", "created_utc" : 1543622400, "subreddit" : "nonononoyes" }
{ "author" : "DANKNESSLEVEL-69", "created_utc" : 1543622400, "subreddit" : "memes" }
{ "author" : "HRNNNGH", "created_utc" : 1543622400, "subreddit" : "ACPocketCamp" }
{ "author" : "DomIstKrieg", "created_utc" : 1543622400, "subreddit" : "AskReddit" }
{ "author" : "befooks", "created_utc" : 1543622400, "subreddit" : "northernlion" }
{ "author" : "meekosmom", "created_utc" : 1543622400, "subreddit" : "raisedbynarcissists" }
{ "author" : "[deleted]", "created_utc" : 1543622400, "subreddit" : "wallstreetbets" }
{ "author" : "Red580", "created_utc" : 1543622400, "subreddit" : "ForwardsFromKlandma" }
{ "author" : "rauland", "created_utc" : 1543622400, "subreddit" : "melbourne" }
{ "author" : "Mr-Morrison94", "created_utc" : 1543622400, "subreddit" : "FortNiteBR" }
{ "author" : "huskiesofinternets", "created_utc" : 1543622400, "subreddit" : "aww" }
{ "author" : "AnimaCorpusLucra", "created_utc" : 1543622400, "subreddit" : "wallstreetbets" }
{ "author" : "Shadow14l", "created_utc" : 1543622400, "subreddit" : "financialindependence" }
{ "author" : "potrap", "created_utc" : 1543622400, "subreddit" : "popheads" }
{ "author" : "FireandBloodandStuff", "created_utc" : 1543622400, "subreddit" : "dankmemes" }
{ "author" : "XChihiro", "created_utc" : 1543622400, "subreddit" : "AskReddit" }
{ "author" : "bjacks12", "created_utc" : 1543622400, "subreddit" : "latterdaysaints" }