PlanCache.getPlansByQuery()

在本页面

Definition

  • PlanCache. getPlansByQuery(* *)

查询优化器仅缓存那些具有多个可行计划的查询形状的计划。

该方法仅可从特定集合的计划缓存对象获得;即

db.collection.getPlanCache().getPlansByQuery( <query>, <projection>, <sort> )

PlanCache.getPlansByQuery()方法接受以下参数:

ParameterTypeDescription
querydocumentquery shape的查询谓词。仅谓词的结构(包括字段名)对形状很重要。查询谓词中的值无关紧要。
projectiondocument可选的。与query shape关联的投影。如果指定sort参数,则为必需。
sortdocument可选的。与query shape关联的排序。
Returns:查询形状的缓存查询计划数组。

要查看存在缓存的查询计划的查询形状,请使用PlanCache.listQueryShapes()方法。

3.6 版中的新增功能:PlanCache.getPlansByQuery()方法返回的输出与planCacheListPlans数据库命令相同。

Required Access

在运行authorization的系统上,用户必须具有包括planCacheRead操作的访问权限。

Example

如果集合orders具有以下查询形状:

{
  "query" : { "qty" : { "$gt" : 10 } },
  "sort" : { "ord_date" : 1 },
  "projection" : { }
}

以下操作显示为形状缓存的查询计划:

db.orders.getPlanCache().getPlansByQuery(
   { "qty" : { "$gt" : 10 } },
   { },
   { "ord_date" : 1 }
)