- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的聚合管道之一运行速度相当慢。
该集合被命名为文档
,每个文档可以属于多个营销事件,并且位于“a”到“e”五个雕像之一。一小部分文档可能不属于任何文档,并且其 campaigns
字段设置为 null
。
示例文档:
{_id:id, campaigns:['c1', 'c2], status:'a', ...other fields...}
一些收集统计数据
如果状态为 ['a', 'b', 'c'],查询的目标是计算每个事件每个状态的文档数量
[
{$match:{campaigns:{$ne:null}, status:{$in:['a','b','c']}}},
{$unwind:'$campaigns'},
{$group:{_id:{campaign:'$campaigns', status:'$status'}, total:{$sum:1}}}
]
预计聚合将覆盖几乎整个集合。如果没有索引,聚合大约需要 8 秒
才能完成。
我尝试创建索引
{campaings:1, status:1}
解释计划显示索引已扫描,但聚合花费了近 11 秒
才能完成。
索引包含聚合进行计数所需的所有字段。聚合不应该只命中索引吗?该索引的大小只有 10MB。怎么可能慢一点呢?如果没有索引,还有其他建议来调整查询吗?
获胜计划显示:
{
"stage" : "FETCH",
"filter" : {"$not" : {"campaigns" : {"$eq" : null}}},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {"campaigns" : 1.0,"status" : 1.0},
"indexName" : "campaigns_1_status_1",
"isMultiKey" : true,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"campaigns" : ["[MinKey, null)", "(null, MaxKey]"],
"status" : [ "[\"a\", \"a\"]", "[\"b\", \"b\"]", "[\"c\", \"c\"]"]
}
}
}
如果没有索引,获胜方案:
{
"stage" : "COLLSCAN",
"filter" : {
"$and":[
{"status": {"$in": ["a", "b", "c"]}},
{"$not" : {"campaigns": {"$eq" : null}}}
]
},
direction" : "forward"
}
根据 @Kevin 的要求,以下是有关其他所有索引的一些详细信息,大小以 MB 为单位。
"indexSizes" : {
"_id_" : 32,
"team_1" : 8, //Single value field of ObjectId
"created_time_1" : 16, //Document publish time in source system.
"parent_1" : 2, //_id of parent document.
"by.id_1" : 13, //_id of author from a different collection.
"feedids_1" : 8, //Array, _id of ETL jobs contributing to sync of this doc.
"init_-1" : 2, //Initial load time of the doc.
"campaigns_1" : 10, //Array, _id of campaigns
"last_fetch_-1" : 13, //Last sync time of the doc.
"categories_1" : 8, //Array, _id of document categories.
"status_1" : 8, //Status
"campaigns_1_status_1" : 10 //Combined index of campaign _id and status.
},
最佳答案
阅读 MongoDB 的文档后,我发现了这一点:
The inequality operator $ne is not very selective since it often matches a large portion of the index. As a result, in many cases, a $ne query with an index may perform no better than a $ne query that must scan all documents in a collection. See also Query Selectivity.
使用 $type 运算符查看几篇不同的文章可能会解决该问题。
您可以使用此查询吗:
db.data.aggregate([
{$match:{campaigns:{$type:2},status:{$in:["a","b","c"]}}},
{$unwind:'$campaigns'},
{$group:{_id:{campaign:'$campaigns', status:'$status'}, total:{$sum:1}}}])
关于MongoDB - 聚合性能调优,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37408562/
我的代码遇到了很大的困难。我正在开发一个显示歌词和和弦的应用程序。我使用两个重叠的textview分隔了和弦和歌词。 我在这个项目中遇到的问题是音高改变功能。我尽我所能向我解释得更好: 和弦总数为12
我有一个游戏并使用 Tune 作为分析库。使用最新的 Unity (5.3.4f1) 并通过 Unity 获取 apk(无 eclipse/android studio)。 我的游戏在 Play 商店
我是一名优秀的程序员,十分优秀!