Understanding performance: mongo vs count aggregation

If I make a counting request, I get the results in <2seconds

db.coll.find({"A":1,"createDate":{"$gt":new Date("2011-05-21"),"$lt":new Date("2013-08-21")}}).count()

The following index is used here.

db.coll.ensureIndex({"A":1,"createDate":1})

Similarly, there are 4 columns A, B, C, D (the values ​​are always 0 or 1), for which I run 4 counter queries and get the results in <10seconds.

I looked at the documentation on the aggregation structure and created an aggregate query to execute all four sums.

db.coll.aggregate(  { $match : {"createDate":{$gt:new Date("2013-05-21"),$lt:new Date("2013-08-21")} } },
{ $group :
                         { _id:null,
                         totalA : { $sum : "$A" },
                         totalB : {$sum: "$B},
                         totalC:{$sum: "$C"},
                         totalD:{$sum: "$D"}}} 
 ) 

I also created an index:

db.coll..ensureIndex({"createDate":1,"A":1,"B":1,"C":1,"D":1})

According to the documentation, this index covers my aggregate function. But the return of the unit is ~ 18 seconds.

. - , , , , , . - , .

+4
2

-, 2.4.8, db.runCommand:

db.runCommand({
    aggregate: "coll",
    pipeline: [      
        { $match : 
            {"createDate":{$gt:new Date("2013-05-21"),$lt:new Date("2013-08-21")} } 
        },
        { $group : { 
              _id:null,
              totalA: {$sum :"$A"},
              totalB: {$sum: "$B"},
              totalC: {$sum: "$C"},
              totalD: {$sum: "$D"}
        }} 
    ],
    explain: true
})

, .

, , .

count() , .

. $match , .

. , . , , .

+6

: , mongo. (grouping ..), count() .

, mongodb . . explain ( , 2.4). , , . , , explain .

, .

     

  , $match, $limit $skip,   , .    , $match    .

     

$match, $sort       . , $match    .

.

+3

Source: https://habr.com/ru/post/1528038/


All Articles