aggregation framework - MongoDB Aggregate Slow Performance When Using Sort -


i have collection (tvshow episodes) more 1,200,000 document,

here schema :

  var episodeschema = new schema({     imdbid: { type : string },     showid: {type : string},     episodeid: { type : string },     episodeidnumber:{ type : number },     episodetitle:{ type : string },     showtitle:{type : string},     seasonnumber:{type : number},     episodenumber:{type : number},     airdate : {type : string},     summary:{type : string} }); 

i created index episodetitle episodeidnumber seasonnumber episodenumber episodeid , showid

now used mongodb aggregate group every tvshow episodes

here aggregate query used :

episode.aggregate( [             { $match : { showid : "scorpion" } },             {$sort:{"episodenumber":-1}},             { $group: {              _id: "$seasonnumber", count: { $sum: 1 } ,             episodes : { $push:  { episodeid : "$episodeid" , episodetitle: "$episodetitle" , episodenumber: "$episodenumber" , seasonnumber: "$seasonnumber" , airdate: "$airdate"  } }              } }             ,             { $sort : { _id : -1  } } ] ) 

now when run query take more 2605.907 ms , after digging found out why slow , because of using {$sort:{"episodenumber":-1}} , without using {$sort:{"episodenumber":-1}} take around 19.178 ms run.

as mentioned above create index episodenumber field , based on mongodb aggregation pipeline optimization used sort after match ok , , didn't wrong.

so after thought wrong indexes , removed episodenumber index , reindexd , had same time nothing changed.

at end 1 time tried run aggregate group query without episodenumber indexed , surprisingly faster ! take around 20.118 ms .

i wants know why happened , isn't indexes faster query ?

update

query explain output :

{         "waitedms" : numberlong(0),         "stages" : [             {                 "$cursor" : {                     "query" : {                         "showid" : "scorpion"                     },                     "sort" : {                         "episodenumber" : -1                     },                     "fields" : {                         "airdate" : 1,                         "episodeid" : 1,                         "episodenumber" : 1,                         "episodetitle" : 1,                         "seasonnumber" : 1,                         "_id" : 0                     },                     "queryplanner" : {                         "plannerversion" : 1,                         "namespace" : "test.episodes",                         "indexfilterset" : false,                         "parsedquery" : {                             "showid" : {                                 "$eq" : "scorpion"                             }                         },                         "winningplan" : {                             "stage" : "eof"                         },                         "rejectedplans" : [ ]                     }                 }             },             {                 "$group" : {                     "_id" : "$seasonnumber",                     "count" : {                         "$sum" : {                             "$const" : 1                         }                     },                     "episodes" : {                         "$push" : {                             "episodeid" : "$episodeid",                             "episodetitle" : "$episodetitle",                             "episodenumber" : "$episodenumber",                             "seasonnumber" : "$seasonnumber",                             "airdate" : "$airdate"                         }                     }                 }             },             {                 "$sort" : {                     "sortkey" : {                         "_id" : -1                     }                 }             }         ],         "ok" : 1     } 


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -