简体   繁体   中英

Slow query on MongoDB when using $in with multiple values of indexed array field

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 3.0.7
  • Notebook i7 with 8 GB of RAM (2 GB used by mongodb), HDD
  • Linux + Docker
  • StorageEngine MMAP
  • collection with 50M documents (test purpose)
  • Index created with: db.collection.createIndex( { tag : 1 } )

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM