简体   繁体   中英

How to improve aggregation query in mongoDB?

Let's say i have this 2 of huge documents:

[
 {
  _id: ....,
  status: "A",
  class: "DIP1A",
  "created.user._id": ...,
  "created.dt": ....,
  "category": "private",
  price: 100.00 //type double
 },
 {
  _id: ....,
  status: "A",
  class: "DIP2A",
  "created.user._id": ...
  "created.dt": ...,
  "category": "public",
  price: 200.00 //type double
 },
];

Query:

    var pipeline = [

              {
                $match: {
                  "created.user._id": ....
                }
              },
              {
                $unwind: "$class"
              },
              {
                $unwind: "$price"
              },
              {
                $group: {
                  _id: "$class",
                  price: {
                    $sum: "$price"
                  },
                  count: {
                    $sum: 1
                  }
                }
              },
              {
                $project: {
                  _id: 0,
                  class: '$_id',
                  count: 1,
                  price: 1
                }
              }
    ];

 db.myCollection.aggregate(pipeline);

Problem issue:

  • Query without calculate/$sum "$price", it's running really faster;

Indexes:

db.myCollection.ensureIndex({ 'created.user._id': -1 });
db.myCollection.ensureIndex({ 'created.user._id': -1, class: 1 });
db.myCollection.ensureIndex({ 'created.user._id': -1, price: 1});

Performance:

  • without $sum calc : 5 second with huge of records.
  • with $sum cals : 20 minutes with huge of records.

The one thing you really should do is move the $project stage to right after the $match stage (if the documents contain more data then stated in your question (huge documents)). You want as little data as possible through the pipeline. Also i see an $unwind on price and class but in your example they aren't array's. It could be a copy/paste issue ;-)

Like :

var pipeline = [

          {
            $match: {
              "created.user._id": ....
            }
          },
         {
            $project: {
              _id: 0,
              class: '$_id',
              count: 1,
              price: 1
            }
          },
          {
            $unwind: "$class"
          },
          {
            $unwind: "$price"
          },
          {
            $group: {
              _id: "$class",
              price: {
                $sum: "$price"
              },
              count: {
                $sum: 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