Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
针对列表中的子文档优化Spring MongoDB查询_Spring_Mongodb_Indexing_Subdocument - Fatal编程技术网

针对列表中的子文档优化Spring MongoDB查询

针对列表中的子文档优化Spring MongoDB查询,spring,mongodb,indexing,subdocument,Spring,Mongodb,Indexing,Subdocument,Spring/Mongo应用程序的查询性能很差,其中有一个包含子文档列表的文档。我曾尝试添加复合索引来帮助处理特定的查询,但当我在子文档列表中搜索时,它们似乎没有多大帮助。在查询列表中的子文档时,这只是一个坏主意,还是我可以做些什么来改进这一点?我尝试从mmapv1升级到wiredTiger存储引擎,但发现wiredTiger对于同一查询实际上慢了约30% 我的理解是,我们的索引行数最终将是文档行数*我们试图用复合索引索引的每个列表中的子文档数。这似乎不太理想,但我想知道是否有某种方法可以优化它

Spring/Mongo应用程序的查询性能很差,其中有一个包含子文档列表的文档。我曾尝试添加复合索引来帮助处理特定的查询,但当我在子文档列表中搜索时,它们似乎没有多大帮助。在查询列表中的子文档时,这只是一个坏主意,还是我可以做些什么来改进这一点?我尝试从mmapv1升级到wiredTiger存储引擎,但发现wiredTiger对于同一查询实际上慢了约30%

我的理解是,我们的索引行数最终将是文档行数*我们试图用复合索引索引的每个列表中的子文档数。这似乎不太理想,但我想知道是否有某种方法可以优化它,并且仍然将子文档列在列表中

Spring Mongo文档类的伪/简化示例:

@Document
@CompoundIndexes({
@CompoundIndex(name = "index1", def = "{ 'attr':1, 'things:attr1': 1, 'things:attr2': 1}", unique = false)
})
public class Foo {
  String attr
  List things
}
因此,MongoDB文档如下所示:

{
attr: value,
things: [
    {
      attr1: value1,
      attr2: value2
    },
    {
      attr1: value1,
      attr2: value2
    }
  ]
}
{
attr: value,
thing1:
    {
      attr1: value1,
      attr2: value2
    },
thing2:
    {
      attr1: value1,
      attr2: value2
    }
  ]
}
查询示例:

{ "$and" : [ {"attr": "value"}, { "things" : { "$elemMatch" : { "attr1" : "value2" , "attr2" : "value2"}}}]}
我的下一个最佳解决方案是为列表中的每个子文档直接在文档的根上放置一个字段,但这将需要对我们的代码库进行痛苦而昂贵的重构,重构后的文档将如下所示:

{
attr: value,
things: [
    {
      attr1: value1,
      attr2: value2
    },
    {
      attr1: value1,
      attr2: value2
    }
  ]
}
{
attr: value,
thing1:
    {
      attr1: value1,
      attr2: value2
    },
thing2:
    {
      attr1: value1,
      attr2: value2
    }
  ]
}

index1应该足以满足您的查询。你能做一个find.explain来确认index1用于你的查询吗?是的,索引是我们所能做到的最好的,但是即使使用索引,性能也是不可接受的。