2

I have a big $or query and MongoDB is not picking the index that I expect it to.

Query:

{"$or" : [
    {"del_at" : {"$gt" : Timestamp(1448895834, 2)}}, 
    {"rts" : {"$elemMatch" : {"k" : "app","v" : "38a91025-1109-4256-b08c-df559c0a74df","a" : false,"del_at" : {"$gt" : Timestamp(1448895834, 2)}}}}, 
    {"rts" : {"$elemMatch" : {"k" : "appv","v" : "e1d1daa4-8fb0-4aef-a6a2-9d80005bb8de","a" : false,"del_at" : {"$gt" : Timestamp(1448895834, 2)}}}}, 
    {"rts" : {"$elemMatch" : {"k" : "devg","v" : "ebfb77ea-3568-4725-84f6-8d5ee2582868","a" : false,"del_at" : {"$gt" : Timestamp(1448895834, 2)}}}}, 
    {"rts" : {"$elemMatch" : {"k" : "dev","v" : "5275cb0e-85f1-4f88-970b-e76ee0c55576","a" : false,"del_at" : {"$gt" : Timestamp(1448895834, 2)}}}}, 
    {"rts" : {"$elemMatch" : {"k" : "usrg","v" : "acab2d5a-2814-4e19-98a0-95b4e0748789","a" : false,"del_at" : {"$gt" : Timestamp(1448895834, 2)}}}}, 
    {"rts" : {"$elemMatch" : {"k" : "usr","v" : "d3f2aef1-64d4-4d3f-a36a-cdfb0d496c94","a" : false,"del_at" : {"$gt" : Timestamp(1448895834, 2)}}}}
]}

Indexes:

1. {"del_at" : 1,"rts.k" : 1,"rts.v" : 1,"rts.a" : 1,"rts.del_at" : 1}
2. {"rts.a" : 1,"rts.k" : 1,"rts.v" : 1,"del_at" : 1,"up_at" : 1}

In MongoDB 2.6, it does not select an index, however both exist.

In MongoDB 3.0 and 3.2, it selects the second index, but I'd expect it to use the first. Does anyone know why?

Thanks for the help!

1
  • Optimizer can choose a different index for every $or clause. Can you share the explain() output from 2.X and 3.X?
    – Antonios
    Dec 13, 2015 at 18:57

1 Answer 1

1

I asked this question on the Google group for Mongodb. See Dwight's explanation and work around.

https://groups.google.com/forum/?__hstc=21977188.535555756f91d668db0d32d8604f6233.1443211966594.1443211966594.1443211966594.1&__hssc=21977188.11.1443211966594&__hsfp=469890013#!topic/mongodb-user/iKtaqA5KrKY

Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.