📜  MongoDB查询计划缓存命令

📅  最后修改于: 2020-11-23 01:03:41             🧑  作者: Mango

MongoDB查询计划缓存命令

MongoDB planCacheClear命令

planCacheClear命令用于删除集合的缓存查询计划。它声明查询形状以删除该形状的缓存查询计划。

句法:

   db.runCommand(
   {
      planCacheClear: ,
      query: ,
      sort: ,
      projection: 
   }
)

指令栏位

Field Type Description
query document It contains the prefix for the shape query.
projection document It contains the projection details associated with the query shape.
sort document It contains the sorting details associated with the query shape.

例:

我们具有以下查询形状:

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

以下代码清除为该形状缓存的查询计划:

db.runCommand(
   {
      planCacheClear: "orders",
      query: { "qty" : { "$gt" : 10 } },
      sort: { "ord_date" : 1 }
   }
)

MongoDB planCacheClearFilters命令

planCacheClearFilters命令删除集合上的索引过滤器。我们还可以使用此命令清除现有的索引过滤器,而索引过滤器仅在服务器进程期间存在,并且在关闭后不会持续存在。

句法:

db.runCommand(
   {
      planCacheClearFilters: ,
      query: ,
      sort: ,
      projection: 
   }
)

命令字段:

Field Type Description
planCache
ClearFilters
string It declares the name of the collection.
query document It contains the query predicate that is associated with the filter to remove.
sort document It contains the sorting criteria associated with the filter to remove.
projection document It contains the projection details that are associated with the filter to remove.

例:

图书收藏包含以下两个过滤器:

{
  "query" : { "status" : "A" },
  "sort" : { "ord_date" : -1 },
  "projection" : { },
  "indexes" : [ { "status" : 1, "cust_id" : 1 } ]
}

{
  "query" : { "status" : "A" },
  "sort" : { },
  "projection" : { },
  "indexes" : [ { "status" : 1, "cust_id" : 1 } ]
}

以下命令将删除第一个索引过滤器:

db.runCommand(
   {
      planCacheClearFilters: "orders",
      query: { "status" : "A" }
   }
)

MongoDB planCacheListFilters命令

palncacheListFilters命令列出与集合的查询形状关联的索引过滤器。

句法:

db.runCommand( { planCacheListFilters:  } )

指令栏位:

Field Type Description
planCacheListFilters string It declares the name of the collection

例:

{
   "filters" : [
      {
         "query" : 
         "sort" : ,
         "projection" : ,
         "indexes" : [
            ,
            ...
         ]
      },
      ...
   ],
   "ok" : 1
}

MongoDB palnCacheSetFilter命令

此命令为集合设置索引过滤器。它是查询形状已经存在的索引过滤器。 planCacheSetFilter覆盖先前的索引过滤器。

句法:

db.runCommand(
   {
      planCacheSetFilter: ,
      query: ,
      sort: ,
      projection: ,
      indexes: [ , , ...]
   }
)

命令过滤器

Field Type Description
planCacheSetFilter string This filed declares the name of the collection.
query document It contains the query predicate associated with the index filter.
sort document It contains the sorting details associated with the filter.
projection document It contains the projection associated with the filter.
indexes1 array It contains the array of the index for the specified query shape.

例:

该示例在books集合上创建一个索引过滤器,这样,对于仅包含购买字段上的相等匹配项而没有任何投影和排序的查询。

db.runCommand(
   {
      planCacheSetFilter: "books",
      query: { purchase: "MongoDB" },
      indexes: [
         { stud_id: 123, purchase: 2 },
         { purchage: 5, order_date: 01102020 }
      ]
   }
)