Databases Reference
In-Depth Information
there is still data left to store, the server reserves a new block—again of at least the
minimum size—and continues storing the data in that block. When the result is fin-
ished, if there is space left in the last block the server trims it to size and merges the
leftover space into the adjacent free block. Figure 7-3 illustrates this process. 16
Figure 7-3. How the query cache allocates blocks to store a result
When we say the server “reserves a block,” we don't mean it is asking the operating
system to allocate memory with malloc() or a similar call. It does that only once, when
it creates the query cache. What we mean is that the server is examining its list of blocks
and either choosing the best place to put a new block or, if necessary, removing the
oldest cached query to make room. In other words, the MySQL server manages its own
memory; it does not rely on the operating system to do it.
So far, this is all pretty straightforward. However, the picture can become quite a bit
more complicated than it appeared in Figure 7-3 . Let's suppose the average result is
quite small, and the server is sending results to two client connections simultaneously.
Trimming the results can leave a free block that's smaller than query_cache
_min_res_unit and cannot be used for storing future cache results. The block alloca-
tion might end up looking something like Figure 7-4 .
16. We've simplified the diagrams in this section for the purposes of illustration. The server really reserves
query cache blocks in a more complicated fashion than we've shown here. If you're interested in how it
works, the comments at the top of sql/sql_cache.cc in the server's source code explain it very clearly.
 
Search WWH ::




Custom Search