【问题标题】:What sorting alogrithm is used by mongodb's sort and nested array sort?mongodb排序和嵌套数组排序使用什么排序算法?
【发布时间】:2020-06-18 04:45:12
【问题描述】:

我正在试验 MongoDB 的 sort 性能,以决定是否将用户的位置历史记录 {ts: DATE(), location: "loc"} (maxHistorySize=100) 存储在用户对象中或单独的集合中。

我的目标是实现更好的 TPS。预计写入次数将多于读取次数。

我现在主要关心的是排序的性能(我在查询和管理时需要它,在插入(创建索引)时也是如此)。

当存储在中时,写查询将是这样的。 我担心的是它可以多快完成排序。 由于每次插入时都会运行$sort,因此该数组将在很大程度上已经排序。 我想知道sort 的运行时间是否为O(log(n))O(nlog(n))

        update: {
          $setOnInsert: {userId: 'userId'},
          $push: {
            locations: {
              $each: [{location: 'location', ts: new Date()}],
              $slice: -100,
              $sort: {ts: 1}
            }
          },

单独存储时,我必须在ts 上创建一个索引。由于 MongoDB 使用B-tree 创建索引,我猜开销只是 O(log(n))。缺点是它使用了额外的空格。

我不确定从哪里开始,任何建议都会受到赞赏。谢谢

【问题讨论】:

    标签: mongodb performance sorting database-performance


    【解决方案1】:

    MongoDB 使用std::sort(从sortChildren 调用,从PushNode::performPush 调用。

    我相信 std::sort 是 O(nlog(n))。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多