Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SERVER-78124: add query count in getPlanCache list, this can help us to get the hot query sql. #1556

Open
wants to merge 2 commits into
base: v5.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/mongo/db/query/explain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@ void Explain::planCacheEntryToBSON(const PlanCacheEntry& entry, BSONObjBuilder*
// Append whether or not the entry is active.
out->append("isActive", entry.isActive);
out->append("works", static_cast<long long>(entry.works));
// we can use this counter to get the highest frequency query sql in tht plancache
out->append("queryCount", static_cast<long long>(entry.queryCounters));
out->append("timeOfCreation", entry.timeOfCreation);

if (entry.debugInfo) {
Expand Down
3 changes: 3 additions & 0 deletions src/mongo/db/query/get_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,9 @@ class PrepareExecutionHelper {
"query"_attr = redact(_cq->toStringShort()));
}

PlanCache* cache = CollectionQueryInfo::get(_collection).getPlanCache();
cache->increaseCacheQueryCounters(*_cq);

return buildCachedPlan(
std::move(querySolution), plannerParams, cs->decisionWorks);
}
Expand Down
15 changes: 15 additions & 0 deletions src/mongo/db/query/plan_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ PlanCacheEntry::PlanCacheEntry(std::unique_ptr<const SolutionCacheData> plannerD
planCacheKey(planCacheKey),
isActive(isActive),
works(works),
queryCounters(0),
debugInfo(std::move(debugInfo)),
estimatedEntrySizeBytes(_estimateObjectSizeInBytes()) {
invariant(this->plannerData);
Expand Down Expand Up @@ -669,6 +670,20 @@ void PlanCache::deactivate(const CanonicalQuery& query) {
entry->isActive = false;
}

//increase the query counters which hit the plan
void PlanCache::increaseCacheQueryCounters(const CanonicalQuery& query) {
PlanCacheKey key = computeKey(query);
stdx::lock_guard<Latch> cacheLock(_cacheMutex);
PlanCacheEntry* entry = nullptr;
Status cacheStatus = _cache.get(key, &entry);
if (!cacheStatus.isOK()) {
invariant(cacheStatus == ErrorCodes::NoSuchKey);
return;
}
invariant(entry);
entry->queryCounters++;
}

PlanCache::GetResult PlanCache::get(const CanonicalQuery& query) const {
PlanCacheKey key = computeKey(query);
return get(key);
Expand Down
9 changes: 9 additions & 0 deletions src/mongo/db/query/plan_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,9 @@ class PlanCacheEntry {
// cause this value to be increased.
size_t works = 0;

//the query count that use this plan to run the sql
uint64_t queryCounters = 0;

// Optional debug info containing detailed statistics. Includes a description of the query which
// resulted in this plan cache's creation as well as runtime stats from the multi-planner trial
// period that resulted in this cache entry.
Expand Down Expand Up @@ -519,6 +522,12 @@ class PlanCache {
*/
void deactivate(const CanonicalQuery& query);

/**
* increase the query counters that hit this cached plan.
* through this counters, we can get the hot query
*/
void increaseCacheQueryCounters(const CanonicalQuery& query);

/**
* Look up the cached data access for the provided 'query'. Used by the query planner
* to shortcut planning.
Expand Down