== About Segmented Key Cache
A segmented key cache is a collection of structures for regular MyISAM
key caches called key cache segments. Segmented key caches mitigate one
of the major problems of the simple key cache: thread contention for key
cache lock (mutex). With regular key caches, every call of a key cache
interface function must acquire this lock. So threads compete for this
lock even in the case when they have acquired shared locks for the file
and the pages they want to read from are in the key cache buffers.
When working with a segmented key cache any key cache interface
function that needs only one page has to acquire the key cache lock
only for the segment the page is assigned to. This makes the chances
for threads not having to compete for the same key cache lock better.
Any page from a file can be placed into a buffer of only one segment.
The number of the segment is calculated from the file number and the
position of the page in the file, and it's always the same for the
page. Pages are evenly distributed among segments.
The idea and the original code of the segmented key cache was provided
by Fredrik Nylander from Stardoll.com. The code was extensively
reworked, improved, and eventually merged into MariaDB by Igor Babaev
from Monty Program.
You can find some benchmark results comparing various settings of
<<code>>key_cache_segments<</code>> here: [[segmented-key-cache-performance | Segmented Key Cache Performance]]
== Segmented Key Cache Syntax
New global variable: <<code>>key_cache_segments<</code>>. This variable sets the number of
segments in a key cache. Valid values for this variable are whole
numbers between <<code>>0<</code>> and <<code>>64<</code>>. If the number
of segments is set to a number
greater than <<code>>64<</code>> the number of segments will be truncated to 64 and a
warning will be issued.
A value of "<<code>>0<</code>>" means the key cache is a regular (i.e. non-segmented)
key cache. This is the default. If <<code>>key_cache_segments<</code>> is
"<<code>>1<</code>>" (or higher) then the new key cache segmentation code is used. In
practice there is no practical use of a single-segment segmented key cache except
for testing purposes. For all practical purposes setting
<<code>>key_cache_segments = 1<</code>> should be slower than any other option and
should not be used in production.
Other global variables used when working with regular key caches also
apply to segmented key caches: <<code>>key_buffer_size<</code>>,
<<code>>key_cache_age_threshold<</code>>, <<code>>key_cache_block_size<</code>>, and
<<code>>key_cache_division_limit<</code>>. See the
[[server-system-variables|Server System Variables]] page for more on these
variables.
== Segmented Key Cache Statistics
Statistics about the key cache can be found by looking at the
<<code>>KEY_CACHES<</code>> table in the <<code>>INFORMATION_SCHEMA<</code>>
database. Columns in this table are:
<<style class="darkheader-nospace-borders">>
|= Column Name |= Description
| <<code>>KEY_CACHE_NAME<</code>> | The name of the key cache
| <<code>>SEGMENTS<</code>> | total number of segments (set to <<code>>NULL<</code>> for regular key caches)
| <<code>>SEGMENT_NUMBER<</code>> | segment number (set to <<code>>NULL<</code>> for any regular key caches and for rows containing aggregation statistics for segmented key caches)
| <<code>>FULL_SIZE<</code>> | memory for cache buffers/auxiliary structures
| <<code>>BLOCK_SIZE<</code>> | size of the blocks
| <<code>>USED_BLOCKS<</code>> | number of currently used blocks
| <<code>>UNUSED_BLOCKS<</code>> | number of currently unused blocks
| <<code>>DIRTY_BLOCKS<</code>> | number of currently dirty blocks
| <<code>>READ_REQUESTS<</code>> | number of read requests
| <<code>>READS<</code>> | number of actual reads from files into buffers
| <<code>>WRITE_REQUESTS<</code>> | number of write requests
| <<code>>WRITES<</code>> | number of actual writes from buffers into files
<</style>>
== See Also
* [[segmented-key-cache-performance|Segmented Key Cache Performance]]