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

Sort function does not use the compound index without equality matches on prefix keys

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.2.4
    • Component/s: Index Maintenance, Querying
    • None
    • ALL
      1. create a collection with the above schema
      2. create compound index with all 3 fields
      3. query with find on the first field and sort on the 3rd field
      4. use explain()
    • None
    • 0
    • None
    • None
    • None
    • None
    • None
    • None

      Original summary

      Sort function does not use the compound index

      Original Description

      This is the collection schema:

      {
          a: 11,
          b: 22,
          c: 33
      }
      

      The index is:

      {
          a: 1,
          b: 1,
          c: 1
      }
      

      If the query is:

      db.test_coll.find({a: 11, b: 22}).sort({ c: 1}).explain()
      

      I can see that there is no SORT stage which is find.
      But, If I remove the b value from the query:

      db.test_coll.find({a: 11}).sort({ c: 1}).explain()
      

      The queryPlanner adds another SORT stage which is very bad for performance issues.

      In general, I noticed that when mongo uses minKey and maxKey index bounds, it doesn't use the index for sorting.

      If I'll use $in for b value with all the possible values, it will use the index correctly also for sorting.
      But if it uses the minKey and maxKey values, it does't use the index for sorting.
      I don't understand what is the difference between the two.
      Looks like a bug.

            Assignee:
            kelsey.schubert@mongodb.com Kelsey Schubert
            Reporter:
            tomg1988@gmail.com Tom Grossman
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: