MongoDB Fetching documents slow (Indexing used)

后端 未结 2 1488
一向
一向 2021-01-13 04:25

The FETCH-stage is the limiting factor in my queries. Ive been reaserching and it seems that mongodb is reading much more than it needs, and not utilize the bandwidth fully.

2条回答
  •  长情又很酷
    2021-01-13 05:20

    I encountered the same problem when I was fetching around 35000 documents. To solve it, I used the aggregate function (sakulstra:aggregate) and in my case it has incredibly boosted the request. The result format is obviously not the same, but it's still easy to use to compute all things I need.

    Before (7000ms) :

    const historicalAssetAttributes = HistoricalAssetAttributes.find({
            date:{'$gte':startDate,'$lte':endDate},
            assetId: {$in: assetIds}
        }, {
            fields:{
                "date":1,
                "assetId":1,
                "close":1
            }
        }).fetch();
    

    After (300ms):

    const historicalAssetAttributes = HistoricalAssetAttributes.aggregate([
            {
                '$match': {
                    date: {'$gte': startDate, '$lte': endDate},
                    assetId: {$in: assetIds}
                }
            }, {
                '$group':{
                    _id: {assetId: "$assetId"},
                    close: {
                        '$push': {
                            date: "$date",
                            value: "$close"
                        }
                    }
                }
            }
        ]);
    

提交回复
热议问题