使用特殊字符的Mongodb正则表达式查询非常慢

时间:2019-04-15 09:29:26

标签: regex mongodb

我有一个名为person的集合,其中包含约3000万个文档,并且我的索引带有键personaname

查询时

db.person.find({'personaname':{'$regex':'^kaLi'}}).limit(1)

获得结果非常快。

但是当我如下查询

db.person.find({'personaname':{'$regex':'^kaLi\\|.*'}}).limit(1)

即使无法获得结果也非常慢。

如何更改第二个查询?我正在使用MongoDB 3.4。

索引

{
       "v" : 2,
       "key" : {
              "personaname" : 1
       },
       "name" : "personaname_1",
       "ns" : "mydb.person",
       "background" : true
}

我应该提到

db.person.find({'personaname':{'$regex':'^kaLi\\|'}}).limit(1)

此查询的速度与第二查询一样慢。

第一个查询解释统计信息

{
  "queryPlanner" : {
    "plannerVersion" : 1,
    "namespace" : "mydb.person",
    "indexFilterSet" : false,
    "parsedQuery" : {
      "personaname" : /^kaLi/
    },
    "winningPlan" : {
      "stage" : "LIMIT",
      "limitAmount" : 1,
      "inputStage" : {
        "stage" : "FETCH",
        "inputStage" : {
          "stage" : "IXSCAN",
          "keyPattern" : {
            "personaname" : 1
          },
          "indexName" : "personaname_1",
          "isMultiKey" : false,
          "multiKeyPaths" : {
            "personaname" : [ ]
          },
          "isUnique" : false,
          "isSparse" : false,
          "isPartial" : false,
          "indexVersion" : 2,
          "direction" : "forward",
          "indexBounds" : {
            "personaname" : [
              "[\"kaLi\", \"kaLj\")",
              "[/^kaLi/, /^kaLi/]"
            ]
          }
        }
      }
    },
    "rejectedPlans" : [ ]
  },
  "executionStats" : {
    "executionSuccess" : true,
    "nReturned" : 1,
    "executionTimeMillis" : 0,
    "totalKeysExamined" : 1,
    "totalDocsExamined" : 1,
    "executionStages" : {
      "stage" : "LIMIT",
      "nReturned" : 1,
      "executionTimeMillisEstimate" : 0,
      "works" : 2,
      "advanced" : 1,
      "needTime" : 0,
      "needYield" : 0,
      "saveState" : 0,
      "restoreState" : 0,
      "isEOF" : 1,
      "invalidates" : 0,
      "limitAmount" : 1,
      "inputStage" : {
        "stage" : "FETCH",
        "nReturned" : 1,
        "executionTimeMillisEstimate" : 0,
        "works" : 1,
        "advanced" : 1,
        "needTime" : 0,
        "needYield" : 0,
        "saveState" : 0,
        "restoreState" : 0,
        "isEOF" : 0,
        "invalidates" : 0,
        "docsExamined" : 1,
        "alreadyHasObj" : 0,
        "inputStage" : {
          "stage" : "IXSCAN",
          "nReturned" : 1,
          "executionTimeMillisEstimate" : 0,
          "works" : 1,
          "advanced" : 1,
          "needTime" : 0,
          "needYield" : 0,
          "saveState" : 0,
          "restoreState" : 0,
          "isEOF" : 0,
          "invalidates" : 0,
          "keyPattern" : {
            "personaname" : 1
          },
          "indexName" : "personaname_1",
          "isMultiKey" : false,
          "multiKeyPaths" : {
            "personaname" : [ ]
          },
          "isUnique" : false,
          "isSparse" : false,
          "isPartial" : false,
          "indexVersion" : 2,
          "direction" : "forward",
          "indexBounds" : {
            "personaname" : [
              "[\"kaLi\", \"kaLj\")",
              "[/^kaLi/, /^kaLi/]"
            ]
          },
          "keysExamined" : 1,
          "seeks" : 1,
          "dupsTested" : 0,
          "dupsDropped" : 0,
          "seenInvalidated" : 0
        }
      }
    },
    "allPlansExecution" : [ ]
  },
  "serverInfo" : {
    "host" : "8ab85811f595",
    "port" : 27017,
    "version" : "3.4.0-rc2",
    "gitVersion" : "217fb0a4d2b808fa45921e4ca1d73db32330620b"
  }
}

第三查询解释

{   
  "queryPlanner" : {
    "plannerVersion" : 1,
    "namespace" : "mydb.person",
    "indexFilterSet" : false,
    "parsedQuery" : {
      "personaname" : /^kaLi\|/
    },
    "winningPlan" : {
      "stage" : "LIMIT",
      "limitAmount" : 1,
      "inputStage" : {
        "stage" : "FETCH",
        "inputStage" : {
          "stage" : "IXSCAN",
          "filter" : {
            "personaname" : /^kaLi\|/
          },
          "keyPattern" : {
            "personaname" : 1
          },
          "indexName" : "personaname_1",
          "isMultiKey" : false,
          "multiKeyPaths" : {
            "personaname" : [ ]
          },
          "isUnique" : false,
          "isSparse" : false,
          "isPartial" : false,
          "indexVersion" : 2,
          "direction" : "forward",
          "indexBounds" : {
            "personaname" : [
              "[\"\", {})",
              "[/^kaLi\\|/, /^kaLi\\|/]"
            ]
          }
        }
      }
    },
    "rejectedPlans" : [ ]
  },
  "executionStats" : {
    "executionSuccess" : true,
    "nReturned" : 0,
    "executionTimeMillis" : 330496,
    "totalKeysExamined" : 384927061,
    "totalDocsExamined" : 0,
    "executionStages" : {
      "stage" : "LIMIT",
      "nReturned" : 0,
      "executionTimeMillisEstimate" : 306045,
      "works" : 384927062,
      "advanced" : 0,
      "needTime" : 384927061,
      "needYield" : 0,
      "saveState" : 3008262,
      "restoreState" : 3008262,
      "isEOF" : 1,
      "invalidates" : 0,
      "limitAmount" : 1,
      "inputStage" : {
        "stage" : "FETCH",
        "nReturned" : 0,
        "executionTimeMillisEstimate" : 298689,
        "works" : 384927062,
        "advanced" : 0,
        "needTime" : 384927061,
        "needYield" : 0,
        "saveState" : 3008262,
        "restoreState" : 3008262,
        "isEOF" : 1,
        "invalidates" : 0,
        "docsExamined" : 0,
        "alreadyHasObj" : 0,
        "inputStage" : {
          "stage" : "IXSCAN",
          "filter" : {
            "personaname" : /^kaLi\|/
          },
          "nReturned" : 0,
          "executionTimeMillisEstimate" : 290473,
          "works" : 384927062,
          "advanced" : 0,
          "needTime" : 384927061,
          "needYield" : 0,
          "saveState" : 3008262,
          "restoreState" : 3008262,
          "isEOF" : 1,
          "invalidates" : 0,
          "keyPattern" : {
            "personaname" : 1
          },
          "indexName" : "personaname_1",
          "isMultiKey" : false,
          "multiKeyPaths" : {
            "personaname" : [ ]
          },
          "isUnique" : false,
          "isSparse" : false,
          "isPartial" : false,
          "indexVersion" : 2,
          "direction" : "forward",
          "indexBounds" : {
            "personaname" : [
              "[\"\", {})",
              "[/^kaLi\\|/, /^kaLi\\|/]"
            ]
          },
          "keysExamined" : 384927061,
          "seeks" : 1,
          "dupsTested" : 0,
          "dupsDropped" : 0,
          "seenInvalidated" : 0
        }
      }
    },
    "allPlansExecution" : [ ]
  },
  "serverInfo" : {
    "host" : "8ab85811f595",
    "port" : 27017,
    "version" : "3.4.0-rc2",
    "gitVersion" : "217fb0a4d2b808fa45921e4ca1d73db32330620b"
  }
}

似乎问题出在indexBounds查询范围内。但我不知道如何解决。

0 个答案:

没有答案