Database Manual / CRUD Operations / CRUD Concepts

Query Shapes查询形状

Changed in version 8.0.在版本8.0中的更改。

A query shape is a set of specifications that group similar queries together. Specifications can include filters, sorts, projections, aggregation pipeline stages, a namespace, and others. Queries that have similar specifications have the same query shape.查询形状是一组将类似查询组合在一起的规范。规范可以包括筛选器、排序、投影、聚合管道阶段、名称空间等。具有相似规范的查询具有相同的查询形状。

Note

Starting in MongoDB 8.0, the pre-existing query shape is renamed to the plan cache query shape, and the queryHash field is renamed planCacheShapeHash. 从MongoDB 8.0开始,将预先存在的查询形状重命名为计划缓存查询形状,并将queryHash字段重命名为planCacheShapeHashFor the components of a command that differentiate plan cache query shapes, see the plan cache query shape definition. 关于区分计划缓存查询形状的命令组件,请参阅计划缓存查询的形状定义。MongoDB 8.0 uses the new query shape and the existing plan cache query shape as shown on this page.MongoDB 8.0使用新的查询形状和现有的计划缓存查询形状,如本页所示。

In MongoDB 8.0 and earlier versions, the existing plan cache query shape supports:在MongoDB 8.0及更早版本中,现有的计划缓存查询形状支持:

Starting in MongoDB 8.0, the new query shape supports:从MongoDB 8.0开始,新的查询形状支持:

Starting in MongoDB 8.0, you can use a $querySettings pipeline stage to return query settings specified for each query shape.从MongoDB 8.0开始,您可以使用$querySettings管道阶段返回为每个查询形状指定的查询设置。

Examples示例

The following sections show examples with query shapes for the following example pizzaOrders collection:以下部分显示了以下示例pizzaOrders集合的查询形状示例:

db.pizzaOrders.insertMany( [
{ _id: 0, type: "pepperoni", size: "small", price: 19,
totalNumber: 10, orderDate: ISODate( "2023-03-13T08:14:30Z" ) },
{ _id: 1, type: "pepperoni", size: "medium", price: 20,
totalNumber: 20, orderDate: ISODate( "2023-03-13T09:13:24Z" ) },
{ _id: 2, type: "pepperoni", size: "large", price: 21,
totalNumber: 30, orderDate: ISODate( "2023-03-17T09:22:12Z" ) },
{ _id: 3, type: "cheese", size: "small", price: 12,
totalNumber: 15, orderDate: ISODate( "2023-03-13T11:21:39.736Z" ) }
] )

Matching Query Shapes匹配查询形状

The following example query shape shows specifications for a find command on the pizzaOrders collection in the default test database:以下示例查询形状显示了默认test数据库中pizzaOrders集合上find命令的规范:

find: "pizzaOrders",
filter: {
orderDate: { $gt: ISODate( "2023-01-20T00:00:00Z" ) }
},
sort: {
totalNumber: 1
},
$db: "test"

The example filter limits the documents to those with an order date greater than the specified date. The example has an ascending sort on the total number of pizzas ordered.示例筛选器将文档限制为订单日期大于指定日期的文档。该示例对订购的披萨总数进行升序排序。

To view the MongoDB 8.0 queryShapeHash and planCacheShapeHash hexadecimal strings, you can use the explain command.要查看MongoDB 8.0 queryShapeHashplanCacheShapeHash十六进制字符串,可以使用explain命令。

The following explain examples contain queries with the same query shape:以下explain示例包含具有相同查询形状的查询:

db.pizzaOrders.explain().find(
{ orderDate: { $gt: ISODate( "2024-05-10T05:15:35Z" ) } } ).
sort( { totalNumber: 1 }
)

db.pizzaOrders.explain().find(
{ orderDate: { $gt: ISODate( "2024-02-05T07:07:16Z" ) } } ).
sort( { totalNumber: 1 }
)

db.pizzaOrders.explain().find(
{ orderDate: { $gt: ISODate( "2023-03-08T08:12:25Z" ) } } ).
sort( { totalNumber: 1 }
)

Because the query shapes are the same, the explain output has the same queryShapeHash for each of the examples, and the same planCacheShapeHash for each. For example:因为查询形状相同,所以explain输出对每个示例都有相同的queryShapeHash,对每个示例也有相同的planCacheShapeHash。例如:

queryShapeHash: 'AB8ECADEE8F0EB0F447A30744EB4813AE7E0BFEF523B0870CA10FCBC87F5D8F1'
planCacheShapeHash: '48E51110'

Different Query Shape不同的查询形状

A query has a different shape if the query has different specifications. For example, if a query has a different filter, sort, projection, namespace, or aggregation pipeline stages.如果查询具有不同的规范,则查询的形状也会不同。例如,如果查询具有不同的筛选器、排序、投影、名称空间或聚合管道阶段。

In the example in the previous section, the example sorts by the totalNumber field. If you change the query sort from totalNumber to a different field, the query has a different query shape.在上一节的示例中,该示例按totalNumber字段排序。如果将查询排序从totalNumber更改为其他字段,则查询具有不同的查询形状。

For example, sorting by pizza price changes the query shape:例如,按披萨price排序会改变查询形状:

db.pizzaOrders.explain().find(
{ orderDate: { $gt: ISODate( "2023-01-20T00:00:00Z" ) } } ).
sort( { price: 1 }
)

Because the query shape is different from the shape in the previous section, the explain output has a different queryShapeHash from the previous example, and a different planCacheShapeHash. For example:因为查询形状与上一节中的形状不同,所以explain输出的queryShapeHash与上一个示例不同,planCacheShapeHash也不同。例如:

queryShapeHash: 'AC1ECADBE8F1EB0F417A30741AB4813BE7E0BFEF523B0870CA11FCBC87F1A8B2'
planCacheShapeHash: '31A52130'

Behavior行为

Starting in MongoDB 8.0, add query settings for query shapes instead of index filters for collections. 从MongoDB 8.0开始,为查询形状添加查询设置,而不是为集合添加索引筛选器Index filters are deprecated starting in MongoDB 8.0. Query settings have more functionality than index filters, and index filters aren't persistent after cluster shutdown.索引筛选器从MongoDB 8.0开始就被弃用。查询设置比索引筛选器具有更多的功能,并且索引筛选器在集群关闭后不是持久的。

Query settings allow you to use an index for all executions of a query shape in a cluster. Also, to prevent an operation from causing excessive cluster workload, you can reject all operations associated with a query shape using an operation rejection filter.查询设置允许您对集群中查询形状的所有执行使用索引。此外,为了防止操作导致过多的集群工作负载,您可以使用操作拒绝筛选器拒绝与查询形状关联的所有操作。

The query optimizer uses the query settings as an additional input during query planning. The query settings affect the plan selected to run a query that has a matching query shape.查询优化器在查询规划期间使用查询设置作为附加输入。查询设置会影响为运行具有匹配查询形状的查询而选择的计划。

Get Started开始使用

Learn More了解更多