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

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • Icon: Major - P3 Major - P3
    • None
    • None
    • Querying
    • Query Optimization

    Description

      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

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated: