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

Compound index not used for sort if sort spec doesn't match index spec, even with equality predicate on sort prefix

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Querying
    • Query Optimization

      The query planner elects to use a blocking sort for queries where the sort spec doesn't match the index spec, even in the case where there is an equality match predicate on some prefix of the sort spec. In this special case a sort is only truly required on the remaining suffix of the sort spec, for which the index could possibly be used. For example, with an index on {a:1,b:1}:

      1. db.foo.find({a:2}).sort({a:1,b:1}) - sort uses the index
      2. db.foo.find({a:2}).sort({a:-1,b:1}) - blocking sort
      3. db.foo.find({a:2}).sort({a:-1,b:-1}) - sort uses the index
      4. db.foo.find({a:2}).sort({a:1,b:-1}) - blocking sort

      Logically, query 2 is the same as 1, and query 4 is the same as 3

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            jon.rangel@mongodb.com Jon Rangel (Inactive)
            Votes:
            2 Vote for this issue
            Watchers:
            12 Start watching this issue

              Created:
              Updated: