Slow query on MongoDB when using $in with multiple values of indexed array field.
1) query with 1 element in array is fast ( < 15 ms)
db.collection.count ( { tag : { $in : [ 1 ] } })
I repeated the query for all values of "tag", and all queries are fast.
i = 1; while (i < 1000) {
db.collection.count( { tag : { $in : [ i ] } });
i++;
}
2) query with 2 or more elements is slow ( > 1m50s)
db.collection.count ( { tag : { $in : [ 1, 2, 3 ] } } )
Some information:
MongoDB using COUNT_SCAN for one value in $in array and FETCH for more than 1 value inside $in array. This is the bug in MongoDB for covered count query with $in operator.
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.