planCacheSetFilter
定義
planCacheSetFilter
Set an index filter for a collection. If an index filter already exists for the プランキャッシュクエリシェイプ, the command overrides the previous index filter.
クエリ設定
バージョン8.0の新機能。
MongoDB 8.0以降では、インデックス フィルターを追加する 代わりに、 クエリ設定を使用します 。 インデックス フィルターは MongoDB 8.0以降非推奨です。
クエリ設定は、インデックス フィルターよりも多くの機能を持ちます。 また、インデックス フィルターは永続的ではなく、すべてのクラスター ノードに対してインデックス フィルターを簡単に作成することはできません。 クエリ設定を追加して例を探すには、 setQuerySettings
を参照してください。
クエリオプティマイザは、クエリ設定中にクエリ設定を追加入力として使用し、クエリを実行するために選択されたプランに影響します。
The settings apply to the クエリシェイプ on the entire cluster. The cluster retains the settings after shutdown.
find
、distinct
、aggregate
コマンドのクエリ設定を追加できます。
クエリ設定を削除するには、 removeQuerySettings
を使用します。 クエリ設定を取得するには、集計パイプラインの$querySettings
ステージを使用します。
互換性
このコマンドは、次の環境でホストされている配置で使用できます。
MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです
重要
このコマンドは、M0、M2、M5、および Flex クラスターではサポートされていません。詳細については、「 サポートされていないコマンド 」を参照してください。
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
構文
このコマンドの構文は、次のとおりです。
db.runCommand( { planCacheSetFilter: <collection>, query: <query>, sort: <sort>, projection: <projection>, collation: { <collation> }, indexes: [ <index1>, <index2>, ...], comment: <any> } )
The plan cache query shape for the index filter is the combination of:
query
sort
projection
collation
コマンドフィールド
コマンドには次のフィールドがあります:
フィールド | タイプ | 説明 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
| string | The name of the collection for the index filter. | ||||||||||
| ドキュメント | The query predicate for the index filter. Only the predicate structure, including the field names, is used in the index filter. The field values in the query predicate are not used. Therefore, the query predicate in an index filter is used by similar queries that differ only in the field values. | ||||||||||
| ドキュメント | Optional. The sort for the index filter. | ||||||||||
| ドキュメント | Optional. The projection for the index filter. | ||||||||||
| ドキュメント | 操作に使用する照合を指定します。 照合を指定すると、大文字・小文字やアクセント記号など、文字列を比較するための言語独自のルールを指定できます。 照合オプションの構文は次のとおりです。
照合を指定する場合、 照合が指定されていなくても、コレクションにデフォルトの照合が設定されている場合( コレクションにも操作にも照合が指定されていない場合、MongoDB では以前のバージョンで使用されていた単純なバイナリ比較によって文字列が比較されます。 1 つの操作に複数の照合は指定できません。たとえば、フィールドごとに異なる照合を指定できません。また、ソートと検索を一度に実行する場合、検索とソートで別の照合を使用できません。 MongoDB 6.0 以降、インデックス フィルターは、以前は MongoDB 8.0以降では、インデックス フィルターを追加する 代わりに、 クエリ設定を使用します 。 インデックス フィルターは MongoDB 8.0以降非推奨です。 クエリ設定は、インデックス フィルターよりも多くの機能を持ちます。 また、インデックス フィルターは永続的ではなく、すべてのクラスター ノードに対してインデックス フィルターを簡単に作成することはできません。 クエリ設定を追加して例を探すには、 | ||||||||||
| 配列 | An array of index filters for the specified plan cache query shape. Specify the index filters as one of these arrays:
The クエリオプティマイザ uses either a collection scan or the index arrays for the query plan. If the specified indexes do not exist or are hidden, the optimizer uses a collection scan. For multiple indexes with the same key pattern, you must specify the index as an array of names. | ||||||||||
| any | 任意。このコマンドに添付するユーザー指定のコメント。設定すると、このコメントは以下の場所にこのコマンドの記録と合わせて表示されます。
コメントには、有効な BSON 型(string, integer, object, array など)を使用できます。 |
Index filters only exist for the duration of the server process and do
not persist after shutdown. To clear the index filters, use the
planCacheClearFilters
command.
必要なアクセス権
ユーザーには、 planCacheIndexFilter
アクションを含むアクセス権が必要です。
例
Set Filter on Plan Cache Query Shape Consisting of Predicate Only
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:
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 plan cache query shape,
check the indexFilterSet
field of either
the db.collection.explain()
or the cursor.explain()
method.
Set Filter on Plan Cache Query Shape Consisting of Predicate, Projection, and Sort
The following example creates an index filter for the orders
collection. 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.
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 plan cache query shape, the query optimizer will only consider indexed
plans which use the index { item: 1, order_date: 1, quantity: 1 }
.
Set Filter on Plan Cache Query Shape Consisting of Predicate and Collation
The following example creates an index filter for the orders
collection. The filter applies to queries whose predicate is an equality
match on the item
field and the collation en_US
(English United
States).
db.runCommand( { planCacheSetFilter: "orders", query: { item: "Movie" }, collation: { locale: "en_US" }, indexes: [ { item: 1, order_date: 1 , quantity: 1 } ] } )
For the plan cache query shape, the query optimizer only uses indexed plans that
use the index { item: 1, order_date: 1, quantity: 1 }
.
Starting in MongoDB 6.0, an index filter uses the collation previously set using the planCacheSetFilter
command.
MongoDB 8.0以降では、インデックス フィルターを追加する 代わりに、 クエリ設定を使用します 。 インデックス フィルターは MongoDB 8.0以降非推奨です。
クエリ設定は、インデックス フィルターよりも多くの機能を持ちます。 また、インデックス フィルターは永続的ではなく、すべてのクラスター ノードに対してインデックス フィルターを簡単に作成することはできません。 クエリ設定を追加して例を探すには、 setQuerySettings
を参照してください。
以下も参照してください。