简体   繁体   中英

My mongodb aggregation query is taking too much time. How can I enhance the performance?

I'm joining two collection using aggregation and using "output" to generate a new collection with the joined data, but it's taking too much time(never ends). How can I enhance the performance? The collection A has 260k documents and collection B has 17.2m.

I have already tested the same script with different data sets and it works fine. At first glance, issue seems to be related with the size of collections.

db.colection_A.aggregate([
    {
        $match : { property_X: "X" }
    },
    {   "$lookup":
        {
            from: "collection_B",
            localField: "property_A",
            foreignField: "property_B",
            as: "joined_data"
        }
    },
    {   $unwind:
        {
            path: "$joined_data",
            preserveNullAndEmptyArrays: false
        }
    },
    {   $project: 
        {   
            "_id": 0,
            "joined_data": 1
         } 
    },
    {   $replaceRoot: 
            { newRoot: "$joined_data" } 
    },
    //{ $limit : 1 }
    { $out: "new_collection"}
    ]);

Expected result is creation of the collection "new_collection" containing data filtered in the "match" and "lookup" conditions.

The aggregation query was working fine, the problem was related with indexing in Mongo. After creation of the index, the query performed much better. Index was created with:

db.collection_B.createIndex( { property_B: 1 } );  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM