【问题标题】:MongoDB Index Strategy for Path Based Document Sorting基于路径的文档排序的MongoDB索引策略
【发布时间】:2015-10-11 19:47:16
【问题描述】:

给定一组如下所示的文档,如果子文档中有不确定数量的路径,在顶级排序键上创建排序索引的最佳策略是什么?

{_id: 1, title: "Document 1", sort:{somePathA: 3, somePathB: 1, somePathC: 3}}
{_id: 2, title: "Document 2", sort:{somePathA: 1, somePathB: 2, somePathC: 2}}
{_id: 3, title: "Document 3", sort:{somePathA: 2, somePathB: 3, somePathC: 1}}

例如以下命令:

db.find().sort({'sort.somePathC': 1});

应该产生以下输出:

{_id: 3, title: "Document 3", sort:{somePathA: 2, somePathB: 3, somePathC: 1}}
{_id: 2, title: "Document 2", sort:{somePathA: 1, somePathB: 2, somePathC: 2}}
{_id: 1, title: "Document 1", sort:{somePathA: 3, somePathB: 1, somePathC: 3}}

【问题讨论】:

  • 你的意思是,sort 嵌入的文档字段可以是一个带有变量 nr 字段的文档?
  • 是的,就是这样。此外,嵌入的排序文档可能因文档而异。

标签: mongodb optimization indexing


【解决方案1】:

因此,Sort 子文档中可能有一个名为 somePathK 的字段,可能没有。成员的数量不是恒定的,而是动态的。如果我理解正确,您需要在新成员加入子文档时运行db.collection.createIndex({'sort.somePathK':1},{background:true, sparse:true}) 函数。如果未找到索引,该函数将创建一个索引。 Sparse 表达式是必需的,因为嵌入的排序文档可能因文档而异。

那么find().sort({'sort.fieldName': 1})方法就可以高效使用了。

另一方面,服务器RAM size 应考虑性能。

这个案子似乎很难处理。因此,未知数量的索引意味着需要未知大小的 RAM。我认为同样的考虑同样适用于所有其他数据库系统。

为了获得良好的性能,需要索引,并且为了存储排序索引,需要合适的 RAM。

祝你好运..

【讨论】:

    猜你喜欢
    • 2020-12-11
    • 2013-07-25
    • 2021-02-07
    • 1970-01-01
    • 2023-03-23
    • 2014-04-02
    • 1970-01-01
    • 2016-10-02
    • 2019-12-08
    相关资源
    最近更新 更多