For any given query, the MongoDB query planner chooses and caches the most efficient query plan given the available indexes. To evaluate the efficiency of query plans, the query planner runs all candidate plans during a trial period. In general, the winning plan is the query plan that produces the most results during the trial period while performing the least amount of work.对于任何给定的查询,MongoDB查询计划器在给定可用索引的情况下选择并缓存最有效的查询计划。为了评估查询计划的效率,查询计划器在试用期内运行所有候选计划。一般来说,获胜计划是在试用期内产生最多结果同时执行最少工作量的查询计划。
The associated plan cache entry is used for subsequent queries with the same plan cache query shape.关联的计划缓存条目用于具有相同计划缓存查询形状的后续查询。
The following diagram illustrates the query planner logic:下图说明了查询计划器逻辑:
Note
Using 使用explain ignores all existing plan cache entries and prevents the MongoDB query planner from creating a new plan cache entry.explain会忽略所有现有的计划缓存条目,并阻止MongoDB查询计划器创建新的计划缓存条目的操作。
Plan Cache Entry State计划缓存条目状态
Each plan cache query shape is associated with one of three states in the cache:每个计划缓存查询形状都与缓存中的三个状态之一相关联:
Missing |
|
Inactive |
|
Active |
|
See Plan Cache Flushes for additional scenarios that trigger changes to the plan cache.有关触发计划缓存更改的其他场景,请参阅计划缓存刷新。
Query Plan and Cache Information查询计划和缓存信息
To view the query plan information for a given query, you can use 要查看给定查询的查询计划信息,可以使用db.collection.explain() or the cursor.explain() .db.collection.explain()或cursor.explain()。
To view plan cache information for a collection, you can use the 要查看集合的计划缓存信息,可以使用$planCacheStats aggregation stage.$planCacheStats聚合阶段。
Plan Cache Flushes计划缓存刷新
The query plan cache does not persist if a 如果mongod restarts or shuts down. In addition:mongod重新启动或关闭,查询计划缓存将不会持久。此外:
Catalog operations like index or collection drops clear the plan cache.索引或集合删除等目录操作会清除计划缓存。Least recently used (LRU) cache replacement mechanism clears the least recently accessed cache entry, regardless of state.最近最少使用(LRU)缓存替换机制会清除最近最少访问的缓存条目,而不管其状态如何。
Users can also:用户还可以:
Manually clear the entire plan cache using the使用PlanCache.clear()method.PlanCache.clear()方法手动清除整个计划缓存。Manually clear specific plan cache entries using the使用PlanCache.clearPlansByQuery()method.PlanCache.clearPlansByQuery()方法手动清除特定的计划缓存条目。
Plan Cache Debug Info Size Limit计划缓存调试信息大小限制
Starting in MongoDB 5.0, the plan cache will save full 从MongoDB 5.0开始,只有当所有集合的计划缓存的累积大小低于0.5 GB时,计划缓存才会保存完整的计划缓存条目。plan cache entries only if the cumulative size of the plan caches for all collections is lower than 0.5 GB. When the cumulative size of the 当所有集合的计划缓存的累积大小超过此阈值时,将存储其他计划缓存条目,但不包含以下调试信息:plan caches for all collections exceeds this threshold, additional plan cache entries are stored without the following debug information:
The estimated size in bytes of a plan cache entry is available in the output of $planCacheStats.$planCacheStats的输出中提供了计划缓存条目的估计大小(以字节为单位)。
planCacheShapeHash and 和planCacheKey
planCacheShapeHash
To help identify slow queries with the same plan cache query shape, each plan cache query shape is associated with a query hash. The plan cache query shape hash is a hexadecimal string that represents a hash of the query shape and is dependent only on the query shape.为了帮助识别具有相同计划缓存查询形状的慢速查询,每个计划缓存查询形状都与一个查询哈希相关联。计划缓存查询形状哈希是一个十六进制字符串,表示查询形状的哈希,仅依赖于查询形状。
Note
As with any hash function, two different query shapes may result in the same hash value. However, the occurrence of hash collisions between different query shapes is unlikely.与任何哈希函数一样,两个不同的查询形状可能会产生相同的哈希值。然而,不同查询形状之间不太可能发生哈希冲突。
Starting in MongoDB 8.0, the existing 从MongoDB 8.0开始,现有的queryHash field is duplicated in a new field named planCacheShapeHash. queryHash字段被复制到一个名为planCacheShapeHash的新字段中。If you're using an earlier MongoDB version, you'll only see the 如果你使用的是早期的MongoDB版本,你只会看到queryHash field. queryHash字段。Future MongoDB versions will remove the deprecated 未来的MongoDB版本将删除弃用的queryHash field, and you'll need to use the planCacheShapeHash field instead.queryHash字段,您将需要使用planCacheShapeHash字段。
planCacheKey
To provide more insight into the query plan cache, MongoDB offers the 为了更深入地了解查询计划缓存,MongoDB提供了planCacheKey.planCacheKey。
planCacheKey is a hash of the plan cache query shape, further distinguished by any available indexes for the shape.是计划缓存查询形状的哈希值,通过形状的任何可用索引进一步区分。
Note
Unlike 与planCacheShapeHash, planCacheKey is a function of both the query shape and the currently available indexes for the shape. planCacheShapeHash不同,planCacheKey是查询形状和形状当前可用索引的函数。That is, if indexes that can support the query shape are added/dropped, the 也就是说,如果添加/删除可以支持查询形状的索引,则planCacheKey value may change whereas the planCacheShapeHash value would not change.planCacheKey值可能会更改,而planCacheShapeHash值不会更改。
Starting in MongoDB 8.0, the existing 从MongoDB 8.0开始,现有的queryHash field is duplicated in a new field named planCacheShapeHash. queryHash字段被复制到一个名为planCacheShapeHash的新字段中。If you're using an earlier MongoDB version, you'll only see the 如果你使用的是早期的MongoDB版本,你只会看到queryHash field. queryHash字段。Future MongoDB versions will remove the deprecated 未来的MongoDB版本将删除弃用的queryHash field, and you'll need to use the planCacheShapeHash field instead.queryHash字段,您将需要使用planCacheShapeHash字段。
For example, consider a collection 例如,考虑一个具有以下索引的集合foo with the following indexes:foo:
db.foo.createIndex( { x: 1 } )
db.foo.createIndex( { x: 1, y: 1 } )
db.foo.createIndex( { x: 1, z: 1 }, { partialFilterExpression: { x: { $gt: 10 } } } )
The following queries on the collection have the same shape:对集合的以下查询具有相同的形状:
db.foo.explain().find( { x: { $gt: 5 } } ) // Query Operation 1
db.foo.explain().find( { x: { $gt: 20 } } ) // Query Operation 2
Given these queries, the index with the partial filter expression can support query operation 2 but not support query operation 1. 给定这些查询,具有部分筛选表达式的索引可以支持查询操作2,但不支持查询操作1。Since the indexes available to support query operation 1 differs from query operation 2, the two queries have different 由于可用于支持查询操作1的索引不同于查询操作2,因此这两个查询具有不同的planCacheKey.planCacheKey。
If one of the indexes were dropped, or if a new index 如果其中一个索引被删除,或者添加了新的索引{ x: 1, a: 1 } were added, the planCacheKey for both query operations will change.{ x: 1, a: 1 },则两个查询操作的planCacheKey都将更改。
Availability可用性
The planCacheShapeHash and planCacheKey are available in:planCacheShapeHash和planCacheKey可在以下位置获得:
explain()output输出fields:字段:Starting in MongoDB 8.0, the existing从MongoDB 8.0开始,现有的queryHashfield is duplicated in a new field namedplanCacheShapeHash.queryHash字段被复制到一个名为planCacheShapeHash的新字段中。If you're using an earlier MongoDB version, you'll only see the如果你使用的是早期的MongoDB版本,你只会看到queryHashfield. Future MongoDB versions will remove the deprecatedqueryHashfield, and you'll need to use theplanCacheShapeHashfield instead.queryHash字段。未来的MongoDB版本将删除弃用的queryHash字段,您将需要使用planCacheShapeHash字段。profiler log messages分析器日志消息and和diagnostic log messages (i.e. mongod/mongos log messages)诊断日志消息(即mongod/mongos日志消息)when logging slow queries.当记录慢速查询时。$planCacheStatsaggregation stage聚合阶段PlanCache.listQueryShapes()method方法/planCacheListQueryShapescommand命令PlanCache.getPlansByQuery()method方法/planCacheListPlanscommand命令
Index Filters索引筛选器
Index filters are set with the 使用planCacheSetFilter command and determine which indexes the planner evaluates for a query shape. planCacheSetFilter命令设置索引筛选器,并确定计划器为查询形状计算哪些索引。A plan cache query shape consists of a combination of query, sort, and projection specifications. If an index filter exists for a given query shape, the planner only considers those indexes specified in the filter.计划缓存查询形状由查询、排序和投影规范的组合组成。如果给定的查询形状存在索引筛选器,则计划器只考虑筛选器中指定的索引。
When an index filter exists for the plan cache query shape, MongoDB ignores the 当计划缓存查询形状存在索引筛选器时,MongoDB会忽略hint(). hint()。To see whether MongoDB applied an index filter for a query shape, check the 要查看MongoDB是否为查询形状应用了索引筛选器,请检查indexFilterSet field of either the db.collection.explain() or the cursor.explain() method.db.collection.explain()或cursor.explain()方法的indexFilterSet字段。
Index filters only affect which indexes the planner evaluates; the planner may still select the collection scan as the winning plan for a given plan cache query shape.索引筛选器只影响计划器评估哪些索引;规划者仍然可以选择集合扫描作为给定计划缓存查询形状的获胜计划。
Index filters exist for the duration of the server process and do not persist after shutdown. MongoDB also provides a command to manually remove filters.索引筛选器在服务器进程期间存在,并且在关闭后不会持续存在。MongoDB还提供了一个手动删除筛选器的命令。
Because index filters override the expected behavior of the planner as well as the 因为索引筛选器覆盖了规划器以及hint() method, use index filters sparingly.hint()方法的预期行为,所以要谨慎使用索引筛选器。
Starting in MongoDB 6.0, an index filter uses the collation previously set using the 从MongoDB 6.0开始,索引筛选器使用之前使用planCacheSetFilter command.planCacheSetFilter命令设置的排序规则。
Starting in MongoDB 8.0, use query settings instead of adding index filters. Index filters are deprecated starting in MongoDB 8.0.从MongoDB 8.0开始,使用查询设置,而不是添加索引筛选器。索引筛选器从MongoDB 8.0开始就被弃用。
Query settings have more functionality than index filters. Also, index filters aren't persistent and you cannot easily create index filters for all cluster nodes. 查询设置比索引筛选器具有更多功能。此外,索引筛选器不是持久的,您无法轻松为所有集群节点创建索引筛选器。To add query settings and explore examples, see 要添加查询设置并探索示例,请参阅setQuerySettings.setQuerySettings。