$planCacheStats
정의
$planCacheStats
버전 4.2에 추가되었습니다.
컬렉션 에 대한 계획 캐시 정보를 반환합니다. 이 단계는 각 계획 캐시 항목에 대한 문서 를 반환합니다.
$planCacheStats
단계는 파이프라인의 첫 번째 단계여야 합니다. 이 단계는 빈 문서를 매개 변수로 사용하며 구문은 다음과 같습니다.{ $planCacheStats: { } } 참고
4.4 변경 사항
버전 4.4부터
$planCacheStats
단계는mongos
인스턴스와mongod
인스턴스에서 실행할 수 있습니다. 4 에서 .2,$planCacheStats
단계는mongod
인스턴스에서만 실행할 수 있습니다.$planCacheStats
에는 호스트 필드와mongos
에 대해 실행되는 경우 샤드 필드와 같은 새로운 필드가 포함됩니다.mongo
shell 은PlanCache.list()
메서드를$planCacheStats
집계 단계의 래퍼로 제공합니다.MongoDB는 다음을 제거합니다.
planCacheListPlans
및planCacheListQueryShapes
명령PlanCache.getPlansByQuery()
및PlanCache.listQueryShapes()
메서드가 포함됩니다.
대신
$planCacheStats
또는PlanCache.list()
를 사용합니다.
고려 사항
파이프라인
$planCacheStats
는 집계 파이프라인의 첫 번째 단계여야 합니다.
제한 사항
$planCacheStats
다음에서는 허용되지 않습니다.$planCacheStats
에는 읽기 고려 수준"local"
이(가) 필요합니다.
액세스 제어
authorization
를 통해 실행되는 시스템에서 사용자는 컬렉션에 대한 planCacheRead
권한이 있어야 합니다.
읽기 설정
$planCacheStats
는 계획 캐시 정보를 반환할 호스트를 선택할 때 읽기 설정 을 관찰합니다.
애플리케이션은 복제본 세트의 다른 멤버를 대상으로 할 수 있습니다. 따라서 각 복제본 세트 멤버는 서로 다른 읽기 명령을 수신하고 다른 멤버와 다른 계획 캐시 정보를 가질 수 있습니다. 그럼에도 불구하고 복제본 세트 또는 샤드 클러스터에서 $planCacheStats
를 실행하는 것은 일반적인 읽기 설정 규칙을 따릅니다. 즉, 복제본 세트에서 작업은 복제본 세트의 한 멤버로부터만 계획 캐시 정보를 수집하고, 샤드 클러스터에서는 각 샤드 복제본 세트의 한 멤버로부터 계획 캐시 정보를 수집합니다.
출력
각 계획 캐시 항목에 대해 $planCacheStats
단계는 다음과 유사한 문서를 반환합니다.
{ "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>, // Available starting in MongoDB 5.0, 4.4.3, 4.2.12 "host" : <string>, // Available starting in MongoDB 4.4 "shard" : <string> // Available starting in MongoDB 4.4 if run on sharded cluster }
각 문서에는 다음과 같은 다양한 쿼리 계획 및 실행 통계가 포함되어 있습니다.
필드 | 설명 | |||||
---|---|---|---|---|---|---|
이 캐시 항목을 생성한 특정 쿼리가 포함된 문서입니다. 즉
| ||||||
| 항목이 활성 상태인지 비활성 상태인지를 나타내는 부울
| |||||
쿼리 형태 의 해시를 나타내는 16진수 문자열입니다. 자세한 내용은 | ||||||
이 쿼리와 연결된 계획 캐시 항목을 찾는 데 사용되는 키의 해시를 나타내는 16진수 문자열입니다. 계획 캐시 키는 쿼리 형태와 해당 형태에 대해 현재 사용 가능한 인덱스 모두에 영향을 받습니다. 자세한 내용은 다음을 참조하세요.
| ||||||
캐시된 계획의 세부 정보입니다. | ||||||
| 쿼리 플래너가 후보 계획을 평가하는 평가 기간 동안 쿼리 실행 계획에서 수행한 "작업 단위"의 수입니다. 자세한 내용은 다음을 참조하세요.
| |||||
| 항목의 생성 시간입니다. | |||||
실행 통계 문서의 배열. 배열에는 각 후보 계획에 대한 문서가 포함되어 있습니다. 실행 통계에 대한 자세한 내용은 | ||||||
| ||||||
| 쿼리 형태에 대한 인덱스 필터 가 있는지 여부를 나타내는 부울입니다. | |||||
| 계획 캐시 항목의 예상 크기를 바이트 단위로 설명하는 숫자입니다. 버전 5.0에 추가. MongoDB 5.0, 4.4.3 및 4.2.12부터 이 필드를 사용할 수 있습니다. | |||||
계획 캐시 정보가 반환된 샤딩된 클러스터 에서 실행 경우 작업 은 각 샤드 복제본 세트 의 단일 멤버 로부터 계획 캐시 항목 정보 를 반환 합니다 . 이 멤버는 샤드 및 호스팅하다 필드로 식별됩니다. 읽기 설정도 참조하세요. 버전 4.4에 추가되었습니다. | ||||||
예시
이 섹션의 예시에서는 다음의 orders
collection을 사용합니다.
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" } ] )
컬렉션에 다음 인덱스를 만듭니다.
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")} } } );
참고
인덱스 { item: 1, price: 1 }
은 부분 인덱스이며 price
필드가 NumberDecimal("10")
보다 크거나 같은 인덱스된 문서만 인덱싱합니다.
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" } )
쿼리 캐시의 모든 항목에 해당하는 정보 반환하기
다음 집계 파이프라인은 $planCacheStats
를 사용하여 컬렉션의 계획 캐시 항목에 대한 정보를 반환합니다.
db.orders.aggregate( [ { $planCacheStats: { } } ] )
이 작업은 캐시의 모든 항목을 반환합니다.
{ // Plan Cache Entry 1 "createdFromQuery" : { "query" : { "quantity" : { "$gte" : 5 }, "type" : "apparel" }, "sort" : { }, "projection" : { } }, "queryHash" : "4D151C4C", "planCacheKey" : "DD67E353", "isActive" : false, "works" : NumberLong(4), "cachedPlan" : { ... }, "timeOfCreation" : ISODate("2020-02-06T18:15:44.849Z"), "creationExecStats" : [ { ... // Exec Stats for Candidate 1 }, { ... // Exec Stats for Candidate 2 } ], "candidatePlanScores" : [ 1.5002, 1.5002 ], "indexFilterSet" : false, "estimatedSizeBytes" : NumberLong(3160), // Available starting in MongoDB 5.0, 4.4.3, 4.2.12 "host" : "mongodb1.example.net:27018", // Available starting in MongoDB 4.4 "shard" : "shardA" // Available starting in MongoDB 4.4 if run on sharded cluster } { // Plan Cache Entry 2 "createdFromQuery" : { "query" : { "quantity" : { "$gte" : 20 } }, "sort" : { }, "projection" : { } }, "queryHash" : "23B19B75", "planCacheKey" : "6F23F858", "isActive" : false, "works" : NumberLong(1), "cachedPlan" : { ... }, "timeOfCreation" : ISODate("2020-02-06T18:15:44.454Z"), "creationExecStats" : [ { ... // Exec Stats for Candidate 1 }, { ... // Exec Stats for Candidate 2 } ], "candidatePlanScores" : [ 1.0002, 1.0002 ], "indexFilterSet" : false, "estimatedSizeBytes" : NumberLong(2539), // Available starting in MongoDB 5.0, 4.4.3, 4.2.12 "host" : "mongodb1.example.net:27018", // Available starting in MongoDB 4.4 "shard" : "shardA" // Available starting in MongoDB 4.4 if run on sharded cluster } { // Plan Cache Entry 3 "createdFromQuery" : { "query" : { "item" : "abc", "price" : { "$gte" : NumberDecimal("5") } }, "sort" : { }, "projection" : { } }, "queryHash" : "117A6B10", "planCacheKey" : "A1824628", "isActive" : true, "works" : NumberLong(4), "cachedPlan" : { ... }, "timeOfCreation" : ISODate("2020-02-06T18:15:44.452Z"), "creationExecStats" : [ { ... // Exec Stats for Candidate 1 }, { ... // Exec Stats for Candidate 2 } ], "candidatePlanScores" : [ 1.7502, 1.7502 ], "indexFilterSet" : false, "estimatedSizeBytes" : NumberLong(3183), // Available starting in MongoDB 5.0, 4.4.3, 4.2.12 "host" : "mongodb1.example.net:27018", // Available starting in MongoDB 4.4 "shard" : "shardA" // Available starting in MongoDB 4.4 if run on sharded cluster } { // Plan Cache Entry 4 "createdFromQuery" : { "query" : { "item" : "abc", "price" : { "$gte" : NumberDecimal("10") } }, "sort" : { }, "projection" : { } }, "queryHash" : "117A6B10", "planCacheKey" : "2E6E536B", "isActive" : true, "works" : NumberLong(3), "cachedPlan" : { ... }, "timeOfCreation" : ISODate("2020-02-06T18:15:44.449Z"), "creationExecStats" : [ { ... // Exec Stats for Candidate 1 }, { ... // Exec Stats for Candidate 2 }, { ... // Exec Stats for Candidate 3 } ], "candidatePlanScores" : [ 1.6668666666666665, 1.6668666666666665, 1.6668666666666665 ], "indexFilterSet" : false, "estimatedSizeBytes" : NumberLong(4653), // Available starting in MongoDB 5.0, 4.4.3, 4.2.12 "host" : "mongodb1.example.net:27018", // Available starting in MongoDB 4.4 "shard" : "shardA" // Available starting in MongoDB 4.4 if run on sharded cluster }
planCacheKey
도 참조하세요.
쿼리 형태 나열
MongoDB 4.4에서는 더 이상 사용되지 않는 planCacheListQueryShapes
명령과 헬퍼 메서드 PlanCache.listQueryShapes()
가 제거되었습니다.
또는 $planCacheStats
단계를 사용하여 캐시된 계획이 있는 모든 쿼리 형태의 목록을 가져올 수 있습니다.
예를 들어 다음에서는 $project
단계를 사용하여 createdFromQuery
필드와 queryHash
필드만 출력합니다.
db.orders.aggregate( [ { $planCacheStats: { } } , { $project: {createdFromQuery: 1, queryHash: 1 } } ] )
이 작업은 다음과 같은 쿼리 형태를 반환합니다.
{ "createdFromQuery" : { "query" : { "item" : "abc", "price" : { "$gte" : NumberDecimal("5") } }, "sort" : { }, "projection" : { } }, "queryHash" : "117A6B10" } { "createdFromQuery" : { "query" : { "quantity" : { "$gte" : 5 }, "type" : "apparel" }, "sort" : { }, "projection" : { } }, "queryHash" : "4D151C4C" } { "createdFromQuery" : { "query" : { "quantity" : { "$gte" : 20 } }, "sort" : { }, "projection" : { } }, "queryHash" : "23B19B75" } { "createdFromQuery" : { "query" : { "item" : "abc", "price" : { "$gte" : NumberDecimal("10") } }, "sort" : { }, "projection" : { } }, "queryHash" : "117A6B10" }
쿼리 형태에 대한 캐시 항목 세부 정보 찾기
특정 쿼리 형태에 대한 계획 캐시 정보를 반환하려면 $planCacheStats
단계 다음에 planCacheKey
필드에 $match
를 입력하면 됩니다.
다음 집계 파이프라인은 $planCacheStats
, $match
, $project
} 순으로 사용하여 특정 쿼리 형태에 대한 특정 정보를 반환합니다.
db.orders.aggregate( [ { $planCacheStats: { } }, { $match: { planCacheKey: "DD67E353"} } ] )
이 연산은 다음을 반환합니다:
{ "createdFromQuery" : { "query" : { "quantity" : { "$gte" : 5 }, "type" : "apparel" }, "sort" : { }, "projection" : { } }, "queryHash" : "4D151C4C", "planCacheKey" : "DD67E353", "isActive" : false, "works" : NumberLong(4), "cachedPlan" : { "stage" : "FETCH", "filter" : { "type" : { "$eq" : "apparel" } }, "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "quantity" : 1 }, "indexName" : "quantity_1", "isMultiKey" : false, "multiKeyPaths" : { "quantity" : [ ] }, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 2, "direction" : "forward", "indexBounds" : { "quantity" : [ "[5.0, inf.0]" ] } } }, "timeOfCreation" : ISODate("2020-02-06T18:15:44.849Z"), "creationExecStats" : [ { "nReturned" : 2, "executionTimeMillisEstimate" : 0, "totalKeysExamined" : 3, "totalDocsExamined" : 3, "executionStages" : { "stage" : "FETCH", "filter" : { "type" : { "$eq" : "apparel" } }, "nReturned" : 2, "executionTimeMillisEstimate" : 0, "works" : 4, "advanced" : 2, "needTime" : 1, "needYield" : 0, "saveState" : 0, "restoreState" : 0, "isEOF" : 1, "docsExamined" : 3, "alreadyHasObj" : 0, "inputStage" : { "stage" : "IXSCAN", "nReturned" : 3, "executionTimeMillisEstimate" : 0, "works" : 4, "advanced" : 3, "needTime" : 0, "needYield" : 0, "saveState" : 0, "restoreState" : 0, "isEOF" : 1, "keyPattern" : { "quantity" : 1 }, "indexName" : "quantity_1", "isMultiKey" : false, "multiKeyPaths" : { "quantity" : [ ] }, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 2, "direction" : "forward", "indexBounds" : { "quantity" : [ "[5.0, inf.0]" ] }, "keysExamined" : 3, "seeks" : 1, "dupsTested" : 0, "dupsDropped" : 0 } } }, { "nReturned" : 2, "executionTimeMillisEstimate" : 0, "totalKeysExamined" : 3, "totalDocsExamined" : 2, "executionStages" : { "stage" : "FETCH", "nReturned" : 2, "executionTimeMillisEstimate" : 0, "works" : 4, "advanced" : 2, "needTime" : 1, "needYield" : 0, "saveState" : 0, "restoreState" : 0, "isEOF" : 1, "docsExamined" : 2, "alreadyHasObj" : 0, "inputStage" : { "stage" : "IXSCAN", "nReturned" : 2, "executionTimeMillisEstimate" : 0, "works" : 4, "advanced" : 2, "needTime" : 1, "needYield" : 0, "saveState" : 0, "restoreState" : 0, "isEOF" : 1, "keyPattern" : { "quantity" : 1, "type" : 1 }, "indexName" : "quantity_1_type_1", "isMultiKey" : false, "multiKeyPaths" : { "quantity" : [ ], "type" : [ ] }, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 2, "direction" : "forward", "indexBounds" : { "quantity" : [ "[5.0, inf.0]" ], "type" : [ "[\"apparel\", \"apparel\"]" ] }, "keysExamined" : 3, "seeks" : 2, "dupsTested" : 0, "dupsDropped" : 0 } } } ], "candidatePlanScores" : [ 1.5002, 1.5002 ], "indexFilterSet" : false, "estimatedSizeBytes" : NumberLong(3160), // Available starting in MongoDB 5.0, 4.4.3, 4.2.12 "host" : "mongodb1.example.net:27018", // Available starting in MongoDB 4.4 "shard" : "shardA" // Available starting in MongoDB 4.4 if run on sharded cluster }
planCacheKey
및 queryHash
도 참조하세요.