Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-21471

Bad index selection on aggregate with $match + $sort

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Aggregation Framework
    • None
    • ALL

      I have a collection with 2m records. unique index on _id and foo columns

      db.col.aggregate([ { "$match" : { "foo" : "abcd" } }, { "$sort" : { "_id" : 1 } } ])
      

      takes about 5 seconds to return;

      db.col.aggregate([ { "$match" : { "foo" : "abcd" } } ])
      

      returns instantly. An explain shows that the first one chooses to use the _id index for some reason therefore causing a whole table scan. I seem to recall this also being an issue back in mongo 2.4 but is still there in mongo 3

            Assignee:
            charlie.swanson@mongodb.com Charlie Swanson
            Reporter:
            mark.zealey Mark Zealey
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: