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

Optimize away { $expr : true } in $match clauses

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 6.1.0-rc0
    • Affects Version/s: 5.0.6
    • Component/s: None
    • Labels:
    • Fully Compatible
    • 161

      { $expr : true }

      is equivalent to { }, but the two generate different query plans (see below). The .NET/C# driver will generate

      { $expr : true }

      if all variables are known at run-time on the client-side and evaluate to true. (Other query frameworks that generate MQL dynamically will likely do something similar.)

      The .NET/C# driver is implementing a client-side optimization to render

      { $expr : true }

      as { } in $match clauses, but older versions (and other drivers) won't have this optimization. It is desirable for the query engine to implement this optimization itself.

      Note that this is similar to SERVER-33925, which considers the case of

      { $expr : false }

      .

      Query plans...

      > db.coll.explain().aggregate([{$match:{ $expr: true }}])
      {
        "explainVersion": "1",
        "queryPlanner": {
          "namespace": "test.coll",
          "indexFilterSet": false,
          "parsedQuery": {
            "$expr": {
              "$const": true
            }
          },
          "queryHash": "0C022C46",
          "planCacheKey": "C3C05147",
          "optimizedPipeline": true,
          "maxIndexedOrSolutionsReached": false,
          "maxIndexedAndSolutionsReached": false,
          "maxScansToExplodeReached": false,
          "winningPlan": {
            "stage": "COLLSCAN",
            "filter": {
              "$expr": {
                "$const": true
              }
            },
            "direction": "forward"
          },
          "rejectedPlans": [ ]
        },
      ...
      
      > db.coll.explain().aggregate([{$match:{}}])
      {
        "explainVersion": "1",
        "queryPlanner": {
          "namespace": "test.coll",
          "indexFilterSet": false,
          "parsedQuery": {
      
          },
          "queryHash": "8B3D4AB8",
          "planCacheKey": "D542626C",
          "optimizedPipeline": true,
          "maxIndexedOrSolutionsReached": false,
          "maxIndexedAndSolutionsReached": false,
          "maxScansToExplodeReached": false,
          "winningPlan": {
            "stage": "COLLSCAN",
            "direction": "forward"
          },
          "rejectedPlans": [ ]
        },
      ...
      

            Assignee:
            henri.nikku@mongodb.com Henri Nikku
            Reporter:
            james.kovacs@mongodb.com James Kovacs
            Votes:
            0 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved: