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

Bad index selection on aggregate with $match + $sort

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Duplicate
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Aggregation Framework
    • Labels:
      None
    • Operating System:
      ALL

      Description

      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

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                9 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: