Mongo $in with order indexing

久未见 提交于 2020-01-15 01:50:16

问题


I have a collection following this "schema" :

{
  _id: ObjectId,
  order: Number,
  fieldA: ObjectId,
  fieldB: Array[ObjectId]
}

And an index defined like this :

{
  fieldA: 1,
  fieldB: 1,
  order: 1
}

When running a find query like this one :

{
  $and: [
    {fieldA: {$in: [{"$oid":"592edae196232608d00f78f5"},{"$oid":"592edadc96232608d00f5614"}]}},
    {fieldB: {$in:[{"$oid":"592edace96232608d00ef77f"},{"$oid":"592edacd96232608d00ef34b"}]}}
  ]
}

with sort defined as

{
  order: 1
}

The query runs fine, the index covers the query, and the explain plan shows me :

  • 4 IXSCANs
  • 1 SORT_MERGE
  • 1 FETCH

Issue: If I query fieldA and/or fieldB with a huge $in (I'm trying to make something like a join), then the indexing does not act the same, the query is not covered, the sort is made in memory and the explain plan shows me :

  • 1 IXSCAN
  • 1 FETCH
  • 1 SORT_KEY_GENERATOR
  • 1 SORT

Even worse, if I run this query from Mongoose, the sort operation goes out of memory :/

来源:https://stackoverflow.com/questions/46318304/mongo-in-with-order-indexing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!