Databases 2 min read

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.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
How MySQL’s Query Cache Works: Inside the Hash‑Based Mechanism

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

SQLmysqlHashingDatabase Performancequery cache
Java High-Performance Architecture
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.