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

The regex filter operation should be applied in the IXSCAN stage if possible

    XMLWordPrintableJSON

Details

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

    Description

      Currently when an index is multikey the filter step for the regex match gets bumped out of the IXSCAN stage and into the FETCH stage. The logical reason for this is not immediately clear and it seems that this may be unnecessary. The result of this is that we fetch and examine documents that won't match the regex even though we could have concluded that based on information available during the IXSCAN.

      diff:PRIMARY> db.version()
      3.4.3
      diff:PRIMARY> c.createIndex({x:1})
      {
          "createdCollectionAutomatically" : false,
          "numIndexesBefore" : 1,
          "numIndexesAfter" : 2,
          "ok" : 1
      }
      diff:PRIMARY> c.find({x:/1/}).explain().queryPlanner.winningPlan
      {
          "stage" : "FETCH",
          "inputStage" : {
              "stage" : "IXSCAN",
              "filter" : {
                  "x" : {
                      "$regex" : "1"
                  }
              },
              "keyPattern" : {
                  "x" : 1
              },
              "indexName" : "x_1",
              "isMultiKey" : false,
              "multiKeyPaths" : {
                  "x" : [ ]
              },
              "isUnique" : false,
              "isSparse" : false,
              "isPartial" : false,
              "indexVersion" : 2,
              "direction" : "forward",
              "indexBounds" : {
                  "x" : [
                      "[\"\", {})",
                      "[/1/, /1/]"
                  ]
              }
          }
      }
      diff:PRIMARY> c.insert({x:[1,2]})
      WriteResult({ "nInserted" : 1 })
      diff:PRIMARY> c.find({x:/1/}).explain().queryPlanner.winningPlan
      {
          "stage" : "FETCH",
          "filter" : {
              "x" : {
                  "$regex" : "1"
              }
          },
          "inputStage" : {
              "stage" : "IXSCAN",
              "keyPattern" : {
                  "x" : 1
              },
              "indexName" : "x_1",
              "isMultiKey" : true,
              "multiKeyPaths" : {
                  "x" : [
                      "x"
                  ]
              },
              "isUnique" : false,
              "isSparse" : false,
              "isPartial" : false,
              "indexVersion" : 2,
              "direction" : "forward",
              "indexBounds" : {
                  "x" : [
                      "[\"\", {})",
                      "[/1/, /1/]"
                  ]
              }
          }
      }
      

      Attachments

        Activity

          People

            backlog-query-optimization Backlog - Query Optimization
            christopher.harris@mongodb.com Chris Harris
            Votes:
            5 Vote for this issue
            Watchers:
            21 Start watching this issue

            Dates

              Created:
              Updated: