On this page本页内容
planCacheSetFilter
Set an index filter for a collection. 为集合设置索引筛选器。If an index filter already exists for the query shape, the command overrides the previous index filter.如果查询形状已经存在索引筛选器,则该命令将覆盖上一个索引筛选器。
The command has the following syntax:该命令具有以下语法:
db.runCommand( { planCacheSetFilter: <collection>, query: <query>, sort: <sort>, projection: <projection>, indexes: [ <index1>, <index2>, ...], comment: <any> } )
The planCacheSetFilter
command has the following field:planCacheSetFilter
命令具有以下字段:
planCacheSetFilter | string | |
query | document |
|
sort | document | query and the projection , the sort make up the query shape for the specified index filter. sort 与query 和projection 一起构成指定索引筛选器的查询形状。 |
projection | document | query and the sort , the projection make up the query shape for the specified index filter. projection 与query 和sort 一起构成指定索引筛选器的查询形状。 |
indexes | array |
|
comment | any |
|
Index filters only exist for the duration of the server process and do not persist after shutdown; however, you can also clear existing index filters using the 索引筛选器仅在服务器进程期间存在,并且在关闭后不存在;但是,也可以使用planCacheClearFilters
command.planCacheClearFilters
命令清除现有索引筛选器。
A user must have access that includes the 用户必须具有包含planCacheIndexFilter
action.planCacheIndexFilter
操作的访问权限。
The following example creates an index filter on the 以下示例在orders
collection such that for queries that consist only of an equality match on the status
field without any projection and sort, the query optimizer evaluates only the two specified indexes and the collection scan for the winning plan:orders
集合上创建一个索引筛选器,以便对于仅包含status
字段上的相等匹配而没有任何投影和排序的查询,查询优化器仅评估两个指定的索引和获胜计划的集合扫描:
db.runCommand( { planCacheSetFilter: "orders", query: { status: "A" }, indexes: [ { cust_id: 1, status: 1 }, { status: 1, order_date: -1 } ] } )
In the query predicate, only the structure of the predicate, including the field names, are significant; the values are insignificant. 在查询谓词中,只有谓词的结构(包括字段名)是重要的;这些值无关紧要。As such, the created filter applies to the following operations:因此,创建的筛选器适用于以下操作:
db.orders.find( { status: "D" } ) db.orders.find( { status: "P" } )
To see whether MongoDB will apply 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
字段。
The following example creates an index filter for the 以下示例为orders
collection. orders
集合创建索引筛选器。The filter applies to queries whose predicate is an equality match on the 该筛选器适用于谓词与item
field, where only the quantity
field is projected and an ascending sort by order_date
is specified.item
字段相等匹配的查询,其中仅投影quantity
字段,并指定了按order_date
升序排序。
db.runCommand( { planCacheSetFilter: "orders", query: { item: "ABC" }, projection: { quantity: 1, _id: 0 }, sort: { order_date: 1 }, indexes: [ { item: 1, order_date: 1 , quantity: 1 } ] } )
For the query shape, the query optimizer will only consider indexed plans which use the index 对于查询形状,查询优化器将只考虑使用索引{ item: 1, order_date: 1, quantity: 1 }
.{ item: 1, order_date: 1, quantity: 1 }
的索引计划。