参数意义
MyISAM 设置索引块的大小,它被所有的线程共享
手工添加索引缓存块降低线程间的竞争
创建新的索引缓存
- set global hot_cache2.key_buffer_size = 100 * 1024 * 1024 建立一个100M索引缓存
- cache index tbl1,tbl2 in hot_cache2 把先关表的索引放到指定的索引缓存中
- set global hot_cache2.key_buffer_size = 0; 删除索引缓存
默认索引缓存
- load index into cache tbl; 将tbl的索引装载到默认的索引缓存中
- 默认的索引缓存不能删除
key_buffer与表之间的关联
服务器每次重启都会将key_buffer中的数据清空,每次重启要将相应表的索引缓存到key_buffer中,通过配置文件中的init-file关联,如下例子:
key_buffer_size = 4G
hot_cache.key_buffer_size = 2G
cold_cache.key_buffer_size = 2G
init_file=/path/mysqld-index-init.sql
mysqld-index-init.sql文件内容如下:
cache index a.tbl1, a.tbl2, b.tbl3 in hot_cache;
cache index a.t3, a.t4, b.t1 in cold_cache
计算索引未命中缓存的概率
- show global status like 'key_read%' 查看key_buffer_size的使用情况
+------------------------+-------------+
| Variable_name | Value |
+------------------------+-------------+
| Key_read_requests | 27813678764 |
| Key_reads | 6798830 |
+------------------------+-------------+
- Key_read_requests:从缓存读取索引的请求次数。
- Key_reads:从磁盘读取索引的请求次数。
缓存命中概率
key_cache_miss_rate = (key_reads / key_read_requests) * 100%, 如果key_cache_miss_rate 在0.01%以下的话,key_buffer_size分配的过多,可以适当减少。
查看未使用的缓存簇(blocks)数
show global status like 'key_blocks_u%';
+------------------------+-------------+
| Variable_name | Value |
+------------------------+-------------+
| Key_blocks_unused | 0 |
| Key_blocks_used | 413543 |
+------------------------+-------------+
- Key_blocks_unused表示未使用的缓存簇(blocks)数
- Key_blocks_used表示曾经用到的最大的blocks数
- key_clocks_unused为0,所有的缓存都用到了,可以增加key_buffer_size的值,或者过度索引
比较合理的设置
Key_blocks_used / (Key_blocks_unused + Key_blocks_used) * 100% ≈ 80%
网友评论