I’m using MongoDB version 3.4.6. I have a collection with the following two indexes:
"key": {
"create_time": -1,
"user_id": 1
},
"name": "create_time_-1_user_id_1"
"key": {
"user_id": 1
"create_time": -1,
},
"name": "user_id_1_create_time_-1"
then the query statement is
queryBson := bson.M{
"user_id": bson.M{
"$in": []int{1,2,3,4},
},
"create_time": bson.M{
"$lte": "time",
},
}
we found that “user_id” $in with more than 300 user_id,mongo will use the index “user_id_1_create_time_-1” which cause slow query
in order to avoid this slow query,when the “user_id” $in with more than 300 user_id,we use the hint index “create_time_-1_user_id_1”
so I want to ask, why mongo choose the wrong index?
If a query can be satisfied by multiple indexes defined in the collection, MongoDB will test all the applicable indexes in parallel. can anybody tell me the underlying principle? I want to understand deeper how mongo choose index when a query can be satisfied by multiple indexes defined in the collection
the other question,is necessary create two indexs “create_time_-1_user_id_1” and “user_id_1_create_time_-1”? this wasting memory space
If we could only create one index,which index should we create?