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

Optimal index not chosen for query plan when many indexes match same prefix

    • Query Optimization
    • ALL

      The plan enumerator outputs 6 plans for the below query (one for each of the first 6 six indexes created), none of which use the 7th / most optimal index {a: 1, h: 1}.

      db.foo.ensureIndex({a: 1, b: 1});
      db.foo.ensureIndex({a: 1, c: 1});
      db.foo.ensureIndex({a: 1, d: 1});
      db.foo.ensureIndex({a: 1, e: 1});
      db.foo.ensureIndex({a: 1, f: 1});
      db.foo.ensureIndex({a: 1, g: 1});
      db.foo.ensureIndex({a: 1, h: 1});
      
      for (var i = 0; i < 10000; i++) {
          db.foo.insert({a: 1, h: i});
      }
      
      // Below assertion fails with "Error: [1] != [10000] are not equal"
      assert.eq(1, db.foo.find({a: 1, h: 1}).explain().nscanned);
      

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            rassi J Rassi
            Votes:
            12 Vote for this issue
            Watchers:
            34 Start watching this issue

              Created:
              Updated:
              Resolved: