MongoDb sort is slow with lookup collections

[亡魂溺海] 提交于 2020-02-24 18:35:47

问题


I have two collections in my mongodb database as follows:

employee_details with approximately 330000 documents which has department_id as a reference from departments collection

departments collections with 2 fields _id and dept_name

I want to join the above two collections using department_id as foreign key by using lookup method. Join works fine but the mongo query execution takes long time when I add sort.

Note: The execution is fast If I remove the sort object or If I remove the lookup method.

I have referred several posts in different blogs and SO, but none of them give a solution with sort.

My query is given below:

db.getCollection("employee_details").aggregate([
  {
    $lookup: {
      from: "departments",
      localField: "department_id",
      foreignField: "_id",
      as: "Department"
    }
  },
  { $unwind: { path: "$Department", preserveNullAndEmptyArrays: true } },
  { $sort: { employee_fname: -1 } },
  { $limit: 10 }
]);

Can someone give a method to make the above query to work without delay, as my client cannot compromise with the performance delay. I hope there is some method to fix the performance issue as nosql is intented to handle large database.

Any indexing methods is available there? so that I can use it along with my same collection structure.

Thanks in advance.


回答1:


Currently lookup will be made for every employee_details which means for 330000 times, but if we first sort and limit before lookup, it will be only 10 times. This will greatly decrease query time.

db.getCollection('employee_details').aggregate([
    {$sort      : {employee_fname: -1}},
    {$limit     :10},
    {
        $lookup : {
            from         : "departments",
            localField   : "department_id",
            foreignField : "_id",
            as           : "Department"
        }
    },
    { $unwind   : { path: "$Department", preserveNullAndEmptyArrays: true }},
]) 

After trying this, if you even want to decrease the response time you can define an index on the sort field.

db.employee_details.createIndex( { employee_fname: -1 } )


来源:https://stackoverflow.com/questions/60036938/mongodb-sort-is-slow-with-lookup-collections

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