How MySQL’s Query Cache Works: Inside the Hash‑Based Mechanism
MySQL’s Query Cache stores the results of SELECT statements by hashing the query, keeping the result set in memory, and invalidating cached entries whenever related tables change, allowing identical queries to be served instantly without re‑execution.
MySQL’s Query Cache implementation is relatively straightforward: when a client issues a SELECT query, the statement is processed through a hash algorithm to produce a hash value that is placed in a hash bucket.
At the same time, the query’s result set (ResultSet) is stored in an in‑memory cache. Each node in the hash‑value list also records the memory address of the cached result set, identifiers of all tables referenced by the query, and other related metadata.
When the server receives any SELECT query, it first computes the hash value, looks it up in the Query Cache, and if an identical query is found, returns the previously cached result set directly to the client, bypassing all further execution steps.
If any row in any table changes, the storage engine notifies the Query Cache, which then invalidates all cached queries that involve that table and frees the associated memory, allowing subsequent queries to use fresh cache entries.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
