首页 > 代码库 > Mongodb查看执行计划和强制索引策略

Mongodb查看执行计划和强制索引策略

查看执行计划的方法:explain ()

winningPlan是最终的执行计划 其中的indexBounds索引计划

  1. 在age上建立索引
  2. > db.person.ensureIndex({age:1});
  3. {
  4.    "createdCollectionAutomatically" : false,
  5.    "numIndexesBefore" : 1,
  6.    "numIndexesAfter" : 2,
  7.    "ok" : 1
  8. }
  9. > db.person.getIndexes();
  10. [
  11.    {
  12.       "v" : 1,
  13.       "key" : {
  14.          "_id" : 1
  15.       },
  16.       "name" : "_id_",
  17.       "ns" : "mydb.person"
  18.    },
  19.    {
  20.       "v" : 1,
  21.       "key" : {
  22.          "age" : 1
  23.       },
  24.       "name" : "age_1",
  25.       "ns" : "mydb.person"
  26.    }
  27. ]
  28. 查看执行计划 indexBounds可以看到走了age的索引
  29. > db.person.find({age:30}).explain();
  30. {
  31.    "queryPlanner" : {
  32.       "plannerVersion" : 1,
  33.       "namespace" : "mydb.person",
  34.       "indexFilterSet" : false,
  35.       "parsedQuery" : {
  36.          "age" : {
  37.             "$eq" : 30
  38.          }
  39.       },
  40.       "winningPlan" : {
  41.          "stage" : "FETCH",
  42.          "inputStage" : {
  43.             "stage" : "IXSCAN",
  44.             "keyPattern" : {
  45.                "age" : 1
  46.             },
  47.             "indexName" : "age_1",
  48.             "isMultiKey" : false,
  49.             "direction" : "forward",
  50.             "indexBounds" : {
  51.                "age" : [
  52.                   "[30.0, 30.0]"
  53.                ]
  54.             }
  55.          }
  56.       },
  57.       "rejectedPlans" : [ ]
  58.    },
  59.    "serverInfo" : {
  60.       "host" : "localhost.localdomain",
  61.       "port" : 27017,
  62.       "version" : "3.0.6",
  63.       "gitVersion" : "1ef45a23a4c5e3480ac919b28afcba3c615488f2"
  64.    },
  65.    "ok" : 1
  66. }
  67. 没有走索引的计划是这样的
  68. > db.person.find({name:‘zjf‘}).explain();
  69. {
  70.    "queryPlanner" : {
  71.       "plannerVersion" : 1,
  72.       "namespace" : "mydb.person",
  73.       "indexFilterSet" : false,
  74.       "parsedQuery" : {
  75.          "name" : {
  76.             "$eq" : "zjf"
  77.          }
  78.       },
  79.       "winningPlan" : {
  80.          "stage" : "COLLSCAN",
  81.          "filter" : {
  82.             "name" : {
  83.                "$eq" : "zjf"
  84.             }
  85.          },
  86.          "direction" : "forward"
  87.       },
  88.       "rejectedPlans" : [ ]
  89.    },
  90.    "serverInfo" : {
  91.       "host" : "localhost.localdomain",
  92.       "port" : 27017,
  93.       "version" : "3.0.6",
  94.       "gitVersion" : "1ef45a23a4c5e3480ac919b28afcba3c615488f2"
  95.    },
  96.    "ok" : 1
  97. }

强制索引 hint()方法:

  1. > db.person.ensureIndex({name:1,age:1});
  2. {
  3.    "createdCollectionAutomatically" : false,
  4.    "numIndexesBefore" : 3,
  5.    "numIndexesAfter" : 4,
  6.    "ok" : 1
  7. }
  8. > db.person.getIndexes();
  9. [
  10.    {
  11.       "v" : 1,
  12.       "key" : {
  13.          "_id" : 1
  14.       },
  15.       "name" : "_id_",
  16.       "ns" : "mydb.person"
  17.    },
  18.    {
  19.       "v" : 1,
  20.       "key" : {
  21.          "age" : 1
  22.       },
  23.       "name" : "age_1",
  24.       "ns" : "mydb.person"
  25.    },
  26.    {
  27.       "v" : 1,
  28.       "key" : {
  29.          "name" : 1
  30.       },
  31.       "name" : "name_1",
  32.       "ns" : "mydb.person"
  33.    },
  34.    {
  35.       "v" : 1,
  36.       "key" : {
  37.          "name" : 1,
  38.          "age" : 1
  39.       },
  40.       "name" : "name_1_age_1",
  41.       "ns" : "mydb.person"
  42.    }
  43. ]
  44. //如果不加hint 默认走age上的索引。
  45. > db.person.find({age:{$gt:0}}).explain();
  46. {
  47.    "queryPlanner" : {
  48.       "plannerVersion" : 1,
  49.       "namespace" : "mydb.person",
  50.       "indexFilterSet" : false,
  51.       "parsedQuery" : {
  52.          "age" : {
  53.             "$gt" : 0
  54.          }
  55.       },
  56.       "winningPlan" : {
  57.          "stage" : "FETCH",
  58.          "inputStage" : {
  59.             "stage" : "IXSCAN",
  60.             "keyPattern" : {
  61.                "age" : 1
  62.             },
  63.             "indexName" : "age_1",
  64.             "isMultiKey" : false,
  65.             "direction" : "forward",
  66.             "indexBounds" : {
  67.                "age" : [
  68.                   "(0.0, inf.0]"
  69.                ]
  70.             }
  71.          }
  72.       },
  73.       "rejectedPlans" : [ ]
  74.    },
  75.    "serverInfo" : {
  76.       "host" : "localhost.localdomain",
  77.       "port" : 27017,
  78.       "version" : "3.0.6",
  79.       "gitVersion" : "1ef45a23a4c5e3480ac919b28afcba3c615488f2"
  80.    },
  81.    "ok" : 1
  82. }
  83. //使用hint
  84. > db.person.find({age:{$gt:0}}).hint({name:1,age:1}).explain();
  85. {
  86.    "queryPlanner" : {
  87.       "plannerVersion" : 1,
  88.       "namespace" : "mydb.person",
  89.       "indexFilterSet" : false,
  90.       "parsedQuery" : {
  91.          "age" : {
  92.             "$gt" : 0
  93.          }
  94.       },
  95.       "winningPlan" : {
  96.          "stage" : "KEEP_MUTATIONS",
  97.          "inputStage" : {
  98.             "stage" : "FETCH",
  99.             "filter" : {
  100.                "age" : {
  101.                   "$gt" : 0
  102.                }
  103.             },
  104.             "inputStage" : {
  105.                "stage" : "IXSCAN",
  106.                "keyPattern" : {
  107.                   "name" : 1,
  108.                   "age" : 1
  109.                },
  110.                "indexName" : "name_1_age_1",
  111.                "isMultiKey" : false,
  112.                "direction" : "forward",
  113.                "indexBounds" : {
  114.                   "name" : [
  115.                      "[MinKey, MaxKey]"
  116.                   ],
  117.                   "age" : [
  118.                      "[MinKey, MaxKey]"
  119.                   ]
  120.                }
  121.             }
  122.          }
  123.       },
  124.       "rejectedPlans" : [ ]
  125.    },
  126.    "serverInfo" : {
  127.       "host" : "localhost.localdomain",
  128.       "port" : 27017,
  129.       "version" : "3.0.6",
  130.       "gitVersion" : "1ef45a23a4c5e3480ac919b28afcba3c615488f2"
  131.    },
  132.    "ok" : 1
  133. }

Mongodb查看执行计划和强制索引策略