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

Estimate $type using histogramCE

    • Type: Icon: Improvement Improvement
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • Query Optimization
    • QO 2025-02-03

      Currently:

      • if there is no index, $type is estimated via heuristics, and the estimate is off
      • if there is an index, $type is reported as ceSource: Mixed, and is also inaccurate.
      Enterprise test> db.foo.find({a: {$type: 'int'}}).explain().queryPlanner.winningPlan;
      ...
          cardinalityEstimate: 16.5, <- incorrect
          numKeysEstimate: 50, <- correct
      

      From what I can see, there is a valid indexBound:

          indexBounds: { a: [ '[nan.0, inf.0]' ] }
      

      So a correct estimation should be possible.

      To reproduce:

      db.foo.drop();
      
      let docs = [];
      for (let i = 0; i < 50; i++) {
          docs.push({a: i});
      }
      
      db.foo.createIndex({a: 1});
      db.foo.insertMany(docs);
      db.foo.runCommand({analyze: "foo", key: "a"});
      db.adminCommand({setParameter: 1, planRankerMode: "histogramCE"});
      db.foo.find({a: {$type: 'int'}}).explain().queryPlanner.winningPlan;
      

            Assignee:
            timour.katchaounov@mongodb.com Timour Katchaounov
            Reporter:
            philip.stoev@mongodb.com Philip Stoev
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: