mongodb - MongoDB 索引未与聚合查询一起使用
问题描述
语境
=> 检查编辑!
我正在运行一些查询,其中应用了排序并且应该利用索引。对于某些索引,对于给定环境中的某些查询(聚合),查询会绕过索引,我还无法弄清楚原因。
设置
我有一个包含 3 个索引的 mongo 集合:
- _ID
- 定义.名称
- definition.financial.profitability.highlights.prof_net
我正在使用 2 个查询来测试每个索引的使用情况:
B. 查找和排序查询
db.getCollection('properties_po').find().sort({"definition.financial.profitability.highlights.prof_net" : 1.0 }).limit(1);
db.getCollection('properties_po').find().sort({"definition.name" : 1.0}).limit(1);
B. 用排序查询聚合
db.getCollection("properties_po").aggregate([{"$sort":{"definition.name":1.0}},{"$limit":1}])
db.getCollection("properties_po").aggregate([{"$sort":{"definition.financial.profitability.highlights.prof_net":1.0}},{"$limit":1}])
我db.properties_po.aggregate( [ { $indexStats: { } } ] )
用来检查每次查询后索引使用量是否增加。
结果
环境 1(本地 mongodb 3.4):
两个索引上的两个查询都按预期工作,每次查询后都会增加每个索引的使用量。
环境 2(产品 monhodb 3.6):
查询 A 按预期工作,每次查询后和每个索引都会增加索引使用量。
查询 B 适用于索引“definition.name”,其中索引使用量正确增加。
查询 B不适用于未使用索引(非常慢的查询)的索引“definition.financial.profitability.highlights.prof_net”,因为它反映了索引使用量未增加。
我不确定在哪里看,可能是我缺少一些 mongodb 配置,一些嵌套文档属性限制,或者我的索引可能没有正确创建。
谢谢你的帮助
编辑 18/03 它与 mongodb 版本无关,因为我刚刚测试了在产品服务器上创建相同的集合,并且在那里我也有 2 个不同的结果。
集合 1: 15026 条记录 带有说明的聚合查询的输出:
db.getCollection("properties").explain("executionStats").aggregate([{"$sort":{"definition.financial.profitability.highlights.prof_net":1.0}},{"$limit":1}])
-
{
"stages" : [
{
"$cursor" : {
"query" : {
},
"queryPlanner" : {
"plannerVersion" : 1.0,
"namespace" : "collec.properties",
"indexFilterSet" : false,
"parsedQuery" : {
},
"winningPlan" : {
"stage" : "COLLSCAN",
"direction" : "forward"
},
"rejectedPlans" : [
]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 15026.0,
"executionTimeMillis" : 1522.0,
"totalKeysExamined" : 0.0,
"totalDocsExamined" : 15026.0,
"executionStages" : {
"stage" : "COLLSCAN",
"nReturned" : 15026.0,
"executionTimeMillisEstimate" : 1506.0,
"works" : 15028.0,
"advanced" : 15026.0,
"needTime" : 1.0,
"needYield" : 0.0,
"saveState" : 128.0,
"restoreState" : 128.0,
"isEOF" : 1.0,
"invalidates" : 0.0,
"direction" : "forward",
"docsExamined" : 15026.0
}
}
}
},
{
"$sort" : {
"sortKey" : {
"definition.financial.profitability.highlights.prof_net" : 1.0
},
"limit" : NumberLong(1)
}
}
],
"ok" : 1.0
}
集合 2: 2 条记录
相同的查询输出:
db.getCollection("properties_pl").explain("executionStats").aggregate([{"$sort":{"definition.financial.profitability.highlights.prof_net":1.0}},{"$limit":1}])
-
{
"stages" : [
{
"$cursor" : {
"query" : {
},
"sort" : {
"definition.financial.profitability.highlights.prof_net" : 1.0
},
"limit" : NumberLong(1),
"queryPlanner" : {
"plannerVersion" : 1.0,
"namespace" : "patrimmoine.properties_pl",
"indexFilterSet" : false,
"parsedQuery" : {
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"definition.financial.profitability.highlights.prof_net" : 1.0
},
"indexName" : "definition.financial.profitability.highlights.prof_net_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"definition.financial.profitability.highlights.prof_net" : [
]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2.0,
"direction" : "forward",
"indexBounds" : {
"definition.financial.profitability.highlights.prof_net" : [
"[MinKey, MaxKey]"
]
}
}
},
"rejectedPlans" : [
]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 2.0,
"executionTimeMillis" : 0.0,
"totalKeysExamined" : 2.0,
"totalDocsExamined" : 2.0,
"executionStages" : {
"stage" : "FETCH",
"nReturned" : 2.0,
"executionTimeMillisEstimate" : 0.0,
"works" : 3.0,
"advanced" : 2.0,
"needTime" : 0.0,
"needYield" : 0.0,
"saveState" : 1.0,
"restoreState" : 1.0,
"isEOF" : 1.0,
"invalidates" : 0.0,
"docsExamined" : 2.0,
"alreadyHasObj" : 0.0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 2.0,
"executionTimeMillisEstimate" : 0.0,
"works" : 3.0,
"advanced" : 2.0,
"needTime" : 0.0,
"needYield" : 0.0,
"saveState" : 1.0,
"restoreState" : 1.0,
"isEOF" : 1.0,
"invalidates" : 0.0,
"keyPattern" : {
"definition.financial.profitability.highlights.prof_net" : 1.0
},
"indexName" : "definition.financial.profitability.highlights.prof_net_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"definition.financial.profitability.highlights.prof_net" : [
]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2.0,
"direction" : "forward",
"indexBounds" : {
"definition.financial.profitability.highlights.prof_net" : [
"[MinKey, MaxKey]"
]
},
"keysExamined" : 2.0,
"seeks" : 1.0,
"dupsTested" : 0.0,
"dupsDropped" : 0.0,
"seenInvalidated" : 0.0
}
}
}
}
}
],
"ok" : 1.0
}
因此,一个查询确实没有使用任何索引并执行“COLLSCAN”,而另一个查询正在使用“IXSCAN”执行“FETCH”。
所以我现在的查询是,集合的内容如何改变查询的行为?
解决方案
推荐阅读
- lua - 使用旋转块脚本时,Roblox 部件未正确碰撞
- c# - Crystal Reports 在不同环境下登录失败
- python - 为什么我需要使用“self”来引用类方法中的类变量?
- json - 字段的颠簸变换顺序
- javascript - 数组映射不起作用后递减
- django - 姜戈 | 休息 | 添加顶级 JSON 字段中断过滤选项
- java - 使用 Reactor Netty HttpClient,如何将客户端配置为使用 Flux 发布者将多个项目发送到服务器?
- html - 下拉菜单在移动设备上时不显示
- sql - ORACLE - 在大表上设置重复的排名,需要优化
- android - 我能知道输入变量为什么它总是给我错误,非常感谢