$planCacheStats
On this page本页内容
Definition定义
$planCacheStats
-
Returns plan cache information for a collection.返回集合的计划缓存信息。The stage returns a document for each plan cache entry.该阶段为每个计划缓存条目返回一个文档。The$planCacheStats
stage must be the first stage in the pipeline. The stage takes an empty document as a parameter and has the following syntax:$planCacheStats
阶段必须是管道中的第一个阶段。该阶段将一个空文档作为参数,并具有以下语法:{ $planCacheStats: { } }
See also: 另请参阅:
Considerations注意事项
Pipeline管道
$planCacheStats
must be the first stage in an aggregation pipeline.必须是聚合管道中的第一个阶段。
Restrictions限制
$planCacheStats
is not allowed in:不允许出现在:transactions事务$facet
aggregation stage聚合阶段
$planCacheStats
requires read concern level需要读取关注级别"local"
."local"
。
Access Control访问控制
On systems running with 在使用authorization
, the user must have the planCacheRead
privilege for the collection.authorization
运行的系统上,用户必须具有集合的planCacheRead
权限。
Redaction
When using Queryable Encryption, the 使用可查询加密时,$planCacheStats
stage omits operations against encrypted collections, even though the operations are cached as normal.$planCacheStats
阶段会省略针对加密集合的操作,即使这些操作是正常缓存的。
Read Preference读取首选项
$planCacheStats
observes the read preference in selecting the host(s) from which to return the plan cache information.在选择从其返回计划高速缓存信息的主机时观察读取偏好。
Applications may target different members of a replica set. 应用程序可以针对副本集的不同成员。As such, each replica set member might receive different read commands and have plan cache information that differs from other members. 因此,每个副本集成员可能接收不同的读取命令,并且具有与其他成员不同的计划缓存信息。Nevertheless, running 然而,在副本集或分片集群上运行$planCacheStats
on a replica set or a sharded cluster obeys the normal read preference rules. $planCacheStats
遵循正常的读取偏好规则。That is, on a replica set, the operation gathers plan cache information from just one member of replica set, and on a sharded cluster, the operation gathers plan cache information from just one member of each shard replica set.也就是说,在副本集中,该操作仅从副本集中的一个成员集合计划缓存信息,而在分片集群中,该操作从每个分片副本集中的仅一个成员中集合计划高速缓存信息。
Output输出
Changed in version 7.07.0版更改.
The output of $planCacheStats
depends on the query engine used to complete the query. $planCacheStats
的输出取决于用于完成查询的查询引擎。The value of the version
field of the $planCacheStats
indicates which query engine was used:$planCacheStats
的version
字段的值指示使用了哪个查询引擎:
1
indicates that the classic engine was used.表明使用了经典发动机。2
indicates that the slot-based query execution engine was used.指示使用了基于槽的查询执行引擎。
For queries that use the classic execution engine, 对于使用经典执行引擎的查询,$planCacheStats
returns a document similar to the following:$planCacheStats
返回类似于以下内容的文档:
{
"version" : 1,
"createdFromQuery" : <document>,
"queryHash" : <hexadecimal string>,
"planCacheKey" : <hexadecimal string>,
"isActive" : <boolean>,
"works" : <NumberLong>,
"cachedPlan" : {
"stage" : <STAGE1>,
"filter" : <document>,
"inputStage" : {
"stage" : <STAGE2>,
...
}
},
"timeOfCreation" : <date>,
"creationExecStats" : [ // Exec Stats Document for each candidate plan
{
"nReturned" : <num>,
"executionTimeMillisEstimate" : <num>,
"totalKeysExamined" : <num>,
"totalDocsExamined" :<num>,
"executionStages" : {
"stage" : <STAGE A>,
...
"inputStage" : {
"stage" : <STAGE B>,
...
}
}
},
...
],
"candidatePlanScores" : [
<number>,
...
],
"indexFilterSet" : <boolean>,
"estimatedSizeBytes" : <num>,
"host" : <string>,
"shard" : <string>
}
Each document includes various query plan and execution stats, including:每个文档包括各种查询计划和执行统计信息,包括:
version |
|
createdFromQuery |
{ |
isActive |
|
queryHash |
|
planCacheKey |
|
cachedPlan |
|
works |
|
timeOfCreation | |
creationExecStats |
|
candidatePlanScores |
|
indexFilterSet |
|
estimatedSizeBytes |
|
host |
|
shard |
|
For queries that use the slot-based query execution engine, 对于使用基于槽的查询执行引擎的查询,$planCacheStats
returns a document similar to the following:$planCacheStats
返回类似于以下内容的文档:
{
"version" : 2,
"queryHash" : <hexadecimal string>,
"planCacheKey" : <hexadecimal string>,
"isActive" : <boolean>,
"works" : <NumberLong>,
"cachedPlan" : {
"slots" : <string>,
"stages": <string>
},
"indexFilterSet" : <boolean>,
"estimatedSizeBytes" : <num>,
"host" : <string>
}
Each document includes various query plan and execution stats, including:每个文档包括各种查询计划和执行统计信息,包括:
version |
|
queryHash |
|
planCacheKey |
|
isActive |
|
works |
|
cachedPlan |
|
indexFilterSet |
|
estimatedSizeBytes |
|
host |
|
Examples实例
The examples in this section use the following 本节中的示例使用以下orders
collection:orders
集合:
db.orders.insertMany( [
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("12"), "quantity" : 2, "type": "apparel" },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : 1, "type": "electronics" },
{ "_id" : 3, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : 5, "type": "apparel" },
{ "_id" : 4, "item" : "abc", "price" : NumberDecimal("8"), "quantity" : 10, "type": "apparel" },
{ "_id" : 5, "item" : "jkl", "price" : NumberDecimal("15"), "quantity" : 15, "type": "electronics" }
] )
Create the following indexes on the collection:在集合上创建以下索引:
db.orders.createIndex( { item: 1 } );
db.orders.createIndex( { item: 1, quantity: 1 } );
db.orders.createIndex( { quantity: 1 } );
db.orders.createIndex( { quantity: 1, type: 1 } );
db.orders.createIndex(
{ item: 1, price: 1 },
{ partialFilterExpression: { price: { $gte: NumberDecimal("10")} } }
);
Index 索引{ item: 1, price: 1 }
is a partial index and only indexes documents with price
field greater than or equal to NumberDecimal("10")
.{ item: 1, price: 1 }
是部分索引,仅对price
字段大于或等于NumberDecimal("10")
的文档进行索引。
Run some queries against the collection:对集合运行一些查询:
db.orders.find( { item: "abc", price: { $gte: NumberDecimal("10") } } )
db.orders.find( { item: "abc", price: { $gte: NumberDecimal("5") } } )
db.orders.find( { quantity: { $gte: 20 } } )
db.orders.find( { quantity: { $gte: 5 }, type: "apparel" } )
The preceding queries are completed using the slot-based query execution engine.前面的查询是使用基于槽的查询执行引擎完成的。
Return Information for All Entries in the Query Cache返回查询缓存中所有条目的信息
The following aggregation pipeline uses 以下聚合管道使用$planCacheStats
to return information on the plan cache entries for the collection:$planCacheStats
返回集合的计划缓存项的信息:
db.orders.aggregate( [
{ $planCacheStats: { } }
] )
Output:输出:
[
{ // Plan Cache Entry 1
version: '2',
queryHash: '478AD696',
planCacheKey: '21AE23AD',
isActive: true,
works: Long("7"),
timeOfCreation: ISODate("2023-05-22T20:33:49.031Z"),
cachedPlan: {
...
},
indexFilterSet: false,
isPinned: false,
estimatedSizeBytes: Long("8194"),
host: 'mongodb1.example.net:27018'
},
{ // Plan Cache Entry 2
version: '2',
queryHash: '3D8AFDC6',
planCacheKey: '1C2C4360',
isActive: true,
works: Long("6"),
timeOfCreation: ISODate("2023-05-22T20:33:50.584Z"),
cachedPlan: {
...
},
indexFilterSet: false,
isPinned: false,
estimatedSizeBytes: Long("11547"),
host: 'mongodb1.example.net:27018'
},
{ // Plan Cache Entry 3
version: '2',
queryHash: '27285F9B',
planCacheKey: '20BB9404',
isActive: true,
works: Long("1"),
timeOfCreation: ISODate("2023-05-22T20:33:49.051Z"),
cachedPlan: {
...
},
indexFilterSet: false,
isPinned: false,
estimatedSizeBytes: Long("7406"),
host: 'mongodb1.example.net:27018'
},
{ // Plan Cache Entry 4
version: '2',
queryHash: '478AD696',
planCacheKey: 'B1435201',
isActive: true,
works: Long("5"),
timeOfCreation: ISODate("2023-05-22T20:33:49.009Z"),
cachedPlan: {
...
},
indexFilterSet: false,
isPinned: false,
estimatedSizeBytes: Long("7415"),
host: 'mongodb1.example.net:27018'
}
],
See also 另请参见planCacheKey
.planCacheKey
。
Find Cache Entry Details for a Query Hash查找查询哈希的缓存项详细信息
To return plan cache information for a particular query hash, the 要返回特定查询哈希的计划缓存信息,可以在$planCacheStats
stage can be followed by a $match
on the planCacheKey
field.$planCacheStats
阶段后面的planCacheKey
字段中添加$match
。
The following aggregation pipeline uses 以下聚合管道使用$planCacheStats
followed by a $match
stage to return specific information for a particular query hash:$planCacheStats
和$match
阶段来返回特定查询哈希的特定信息:
db.orders.aggregate( [
{ $planCacheStats: { } },
{ $match: { planCacheKey: "B1435201"} }
] )
Output:输出:
[
{
version: '2',
queryHash: '478AD696',
planCacheKey: 'B1435201',
isActive: true,
works: Long("5"),
timeOfCreation: ISODate("2023-05-22T20:33:49.009Z"),
cachedPlan: {
slots: '$$RESULT=s11 env: { s3 = 1684787629009 (NOW), s6 = Nothing, s5 = Nothing, s1 = TimeZoneDatabase(Asia/Kuwait...Etc/UCT) (timeZoneDB), s10 = {"item" : 1, "price" : 1}, s2 = Nothing (SEARCH_META) }',
stages: '[2] nlj inner [] [s4, s7, s8, s9, s10] \n' +
' left \n' +
' [1] cfilter {(exists(s5) && exists(s6))} \n' +
' [1] ixseek s5 s6 s9 s4 s7 s8 [] @"358822b7-c129-47b7-ad7f-40017a51b03c" @"item_1_price_1" true \n' +
' right \n' +
' [2] limit 1 \n' +
' [2] seek s4 s11 s12 s7 s8 s9 s10 none none [] @"358822b7-c129-47b7-ad7f-40017a51b03c" true false \n'
},
indexFilterSet: false,
isPinned: false,
estimatedSizeBytes: Long("7415"),
host: 'mongodb1.example.net:27018'
}
]
See also 另请参阅planCacheKey
and queryHash
.planCacheKey
和queryHash
。