• Type: Bug
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Query Optimization
    • ALL
    • Hide

       

      db.adminCommand({
        setParameter: 1,
        internalQueryCBRCEMode: "samplingCE",
        internalQueryPlanRanker: "costBased",
        internalQueryStatsSampleRate: 1,
      });
      for (const coll of ["a", "b"]) {
        db[coll].drop();
        db[coll].insertMany(Array.from({length: 1000}, () => ({a: 0, b: 0})));
        db[coll].createIndex({ a: 1 });
        db[coll].createIndex({ b: 1 });
      }
      db.adminCommand({ setParameter: 1, internalQueryStatsCacheSize: "0MB" }); // drop all the existing entries
      db.adminCommand({ setParameter: 1, internalQueryStatsCacheSize: "1%" }); // the default
      db.a.aggregate([{$match: { a: 0, b: 0 }}, {$lookup: {from: "b", localField: "a", foreignField: "a", as: "c", pipeline: [{$match: {b: 1}}]}}]).toArray();
      printjson(
        db
          .getSiblingDB("admin")
          .aggregate([
            { $queryStats: {} },
            //{ $match: { "key.queryShape.cmdNs.coll": "a" } },
            { $project: { key: 1, cbr: "$metrics.queryPlanner.costBasedRanker" } },
          ])
          .toArray(),
      );
      

       
      Reports:

          cbr: {
            cardinalityEstimationMethods: {
              Histogram: Long('0'),
              Sampling: Long('3'),
              Heuristics: Long('0'),
              Mixed: Long('0'),
              Metadata: Long('0'),
              Code: Long('0'),
              HyperLogLog: Long('0')
            },
            nDocsSampled: {
              sum: Long('380'),
              max: Long('380'),
              min: Long('380'),
              sumOfSquares: Decimal128('144400')
            }
          }

      So it reports only the size of one of the samples despite sampling three times (as evidenced by the counter in cardinalityEstimationMethods + with the help of a debugger).
      The first sample is to cost the access path on the first collection. The other two are for the second collection. This is due to the plan cache taking at least two queries to start using an entry.

      Show
        db.adminCommand({ setParameter: 1, internalQueryCBRCEMode: "samplingCE" , internalQueryPlanRanker: "costBased" , internalQueryStatsSampleRate: 1, }); for ( const coll of [ "a" , "b" ]) { db[coll].drop(); db[coll].insertMany(Array.from({length: 1000}, () => ({a: 0, b: 0}))); db[coll].createIndex({ a: 1 }); db[coll].createIndex({ b: 1 }); } db.adminCommand({ setParameter: 1, internalQueryStatsCacheSize: "0MB" }); // drop all the existing entries db.adminCommand({ setParameter: 1, internalQueryStatsCacheSize: "1%" }); // the default db.a.aggregate([{$match: { a: 0, b: 0 }}, {$lookup: {from: "b" , localField: "a" , foreignField: "a" , as: "c" , pipeline: [{$match: {b: 1}}]}}]).toArray(); printjson( db .getSiblingDB( "admin" ) .aggregate([ { $queryStats: {} }, //{ $match: { "key.queryShape.cmdNs.coll" : "a" } }, { $project: { key: 1, cbr: "$metrics.queryPlanner.costBasedRanker" } }, ]) .toArray(), );   Reports: cbr: { cardinalityEstimationMethods: { Histogram: Long('0'), Sampling: Long('3'), Heuristics: Long('0'), Mixed: Long('0'), Metadata: Long('0'), Code: Long('0'), HyperLogLog: Long('0') }, nDocsSampled: { sum: Long('380'), max: Long('380'), min: Long('380'), sumOfSquares: Decimal128('144400') } } So it reports only the size of one of the samples despite sampling three times (as evidenced by the counter in cardinalityEstimationMethods + with the help of a debugger). The first sample is to cost the access path on the first collection. The other two are for the second collection. This is due to the plan cache taking at least two queries to start using an entry.
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Currently the counter is not additive so queries which plan more than once (eg. $lookup) under-report.

      Found by Claude Opus 5.5.

            Assignee:
            Unassigned
            Reporter:
            Kartal Kaan Bozdogan
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: