13.7.8.2 CACHE INDEX Statement语句

CACHE INDEX {
tbl_index_list [, tbl_index_list] ...
    | tbl_name PARTITION (partition_list)
  }
  IN key_cache_name
tbl_index_list:
tbl_name [{INDEX|KEY} (index_name[, index_name] ...)]
partition_list: {
partition_name[, partition_name] ...
  | ALL
}

The CACHE INDEX statement assigns table indexes to a specific key cache. CACHE INDEX语句将表索引分配给特定的键缓存。It applies only to MyISAM tables, including partitioned MyISAM tables. 它仅适用于MyISAM表,包括分区的MyISAM表。After the indexes have been assigned, they can be preloaded into the cache if desired with LOAD INDEX INTO CACHE.分配索引后,如果需要,可以使用LOAD INDEX INTO CACHE将它们预加载到缓存中。

The following statement assigns indexes from the tables t1, t2, and t3 to the key cache named hot_cache:下面的语句将表t1t2t3中的索引分配给名为hot_cache的键缓存:

mysql> CACHE INDEX t1, t2, t3 IN hot_cache;
+---------+--------------------+----------+----------+
| Table   | Op                 | Msg_type | Msg_text |
+---------+--------------------+----------+----------+
| test.t1 | assign_to_keycache | status   | OK       |
| test.t2 | assign_to_keycache | status   | OK       |
| test.t3 | assign_to_keycache | status   | OK       |
+---------+--------------------+----------+----------+

The syntax of CACHE INDEX enables you to specify that only particular indexes from a table should be assigned to the cache. CACHE INDEX的语法允许您指定仅将表中的特定索引分配给缓存。However, the implementation assigns all the table's indexes to the cache, so there is no reason to specify anything other than the table name.但是,该实现将表的所有索引分配给缓存,因此没有理由指定表名以外的任何内容。

The key cache referred to in a CACHE INDEX statement can be created by setting its size with a parameter setting statement or in the server parameter settings. CACHE INDEX语句中引用的键缓存可以通过参数设置语句或服务器参数设置来设置其大小来创建。For example:例如:

SET GLOBAL keycache1.key_buffer_size=128*1024;

Key cache parameters are accessed as members of a structured system variable. 键缓存参数作为结构化系统变量的成员进行访问。See Section 5.1.9.5, “Structured System Variables”.请参阅第5.1.9.5节,“结构化系统变量”

A key cache must exist before you assign indexes to it, or an error occurs:在为键缓存分配索引之前,它必须存在,否则会发生错误:

mysql> CACHE INDEX t1 IN non_existent_cache;
ERROR 1284 (HY000): Unknown key cache 'non_existent_cache'

By default, table indexes are assigned to the main (default) key cache created at the server startup. 默认情况下,表索引被分配给服务器启动时创建的主(默认)键缓存。When a key cache is destroyed, all indexes assigned to it are reassigned to the default key cache.当键缓存被销毁时,分配给它的所有索引都会重新分配给默认键缓存。

Index assignment affects the server globally: If one client assigns an index to a given cache, this cache is used for all queries involving the index, no matter which client issues the queries.索引分配会全局影响服务器:如果一个客户端将索引分配给给定的缓存,则该缓存将用于涉及该索引的所有查询,无论是哪个客户端发出查询。

CACHE INDEX is supported for partitioned MyISAM tables. 分区MyISAM表支持CACHE INDEXYou can assign one or more indexes for one, several, or all partitions to a given key cache. 可以将一个、多个或所有分区的一个或多个索引分配给给定的键缓存。For example, you can do the following:例如,您可以执行以下操作:

CREATE TABLE pt (c1 INT, c2 VARCHAR(50), INDEX i(c1))
    ENGINE=MyISAM
    PARTITION BY HASH(c1)
    PARTITIONS 4;

SET GLOBAL kc_fast.key_buffer_size = 128 * 1024;
SET GLOBAL kc_slow.key_buffer_size = 128 * 1024;

CACHE INDEX pt PARTITION (p0) IN kc_fast;
CACHE INDEX pt PARTITION (p1, p3) IN kc_slow;

The previous set of statements performs the following actions:前一组语句执行以下操作:

If you wish instead to assign the indexes for all partitions in table pt to a single key cache named kc_all, you can use either of the following two statements:如果希望将表pt中所有分区的索引分配给名为kc_all的单键缓存,可以使用以下两种语句之一:

CACHE INDEX pt PARTITION (ALL) IN kc_all;

CACHE INDEX pt IN kc_all;

The two statements just shown are equivalent, and issuing either one has exactly the same effect. 刚才显示的两个语句是等效的,发布其中任何一个语句都具有完全相同的效果。In other words, if you wish to assign indexes for all partitions of a partitioned table to the same key cache, the PARTITION (ALL) clause is optional.换句话说,如果希望将分区表的所有分区的索引分配给同一个密钥缓存,那么PARTITION (all)子句是可选的。

When assigning indexes for multiple partitions to a key cache, the partitions need not be contiguous, and you need not list their names in any particular order. 将多个分区的索引分配给密钥缓存时,分区不需要是连续的,也不需要按任何特定顺序列出它们的名称。Indexes for any partitions not explicitly assigned to a key cache automatically use the server default key cache.任何未显式分配给密钥缓存的分区的索引都会自动使用服务器默认密钥缓存。

Index preloading is also supported for partitioned MyISAM tables. 分区MyISAM表也支持索引预加载。For more information, see Section 13.7.8.5, “LOAD INDEX INTO CACHE Statement”.有关更多信息,请参阅第13.7.8.5节,“将索引加载到缓存语句”