【问题标题】:Performance aggregrate MongoDB match and sample性能聚合 MongoDB 匹配和示例
【发布时间】:2019-02-13 05:48:13
【问题描述】:

我有一个包含 700 万条记录的集合。我需要在特定日期范围内选择 X 个随机元素。

这是我的架构

mongoose.Schema({
        transactionId: {type: String, required: [true, 'transactionId is required'], index: true},
        createdAt: {type: Date, required: [true, 'date is required'], index: true},
        userId: {type: String, required: [true, 'userId is required']}
    });

这是我正在做的查询

TransactionModel.aggregate([
        {
            $match: {
                createdAt: {$gte: startDate, $lt: endDate}
            }
        },
        {
            $sample: {
                size: 100,
            }
        }
    ]

这些是我的结果:

Took 458ms to select 100 winners in date range: 1-5-2018 - 1-6-2018
Took 1524ms to select 100 winners in date range: 1-5-2018 - 1-9-2018
Took 2052ms to select 100 winners in date range: 1-4-2018 - 1-4-2019
Took 19249ms to select 100 winners in date range: 1-1-2018 - 1-1-2033

这 19 秒似乎比较长,当我从聚合函数中删除 $match 时,从 700 万条记录中选出 100 个获胜者只需要 142 毫秒。

有没有一种方法可以提高 match 子句的速度?

【问题讨论】:

  • 您是否在createdAt 字段上创建了索引?

标签: mongodb mongoose nosql aggregate database-performance


【解决方案1】:

正如 Anthony Winzlet 已经写过的,您需要在 createdAt 字段上建立一个索引。这可以是单个字段索引,也可以是复合索引,其中 createdAt 是第一部分。

除此之外,您应该考虑有一个 $project 阶段,以防文档的所有字段都不需要。

理想情况下,您有复合索引,它涵盖了您的查询。

您可以使用 explain() 来查看发生了什么:

collection.find(
  {createdAt: {$gte: startDate, $lt: endDate}}, 
  { created: 1, otherField: 1 }
).explain('executionStats')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-17
    • 2021-09-27
    • 2020-06-29
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多