日期:2014-05-16  浏览次数:20475 次

通过show status 来优化MySQL数据库
1, 查看MySQL服务器配置信息

1
mysql> show variables;
 
2, 查看MySQL服务器运行的各种状态值

1
mysql> show global status;
3, 慢查询

01
mysql> show variables like '%slow%';
02
+------------------+-------+
03
| Variable_name    | Value |
04
+------------------+-------+
05
| log_slow_queries | OFF   |
06
| slow_launch_time | 2     |
07
+------------------+-------+
08
mysql> show global status like '%slow%';
09
+---------------------+-------+
10
| Variable_name       | Value |
11
+---------------------+-------+
12
| Slow_launch_threads | 0     |
13
| Slow_queries        | 279   |
14
+---------------------+-------+
配置中关闭了记录慢查询(最好是打开,方便优化),超过2秒即为慢查询,一共有279条慢查询

4, 连接数

01
mysql> show variables like 'max_connections';
02
+-----------------+-------+
03
| Variable_name   | Value |
04
+-----------------+-------+
05
| max_connections | 500   |
06
+-----------------+-------+
07

08
mysql> show global status like 'max_used_connections';
09
+----------------------+-------+
10
| Variable_name        | Value |
11
+----------------------+-------+
12
| Max_used_connections | 498   |
13
+----------------------+-------+
设置的最大连接数是500,而响应的连接数是498

max_used_connections / max_connections * 100% = 99.6% (理想值 ≈ 85%)

5, key_buffer_size
key_buffer_size是对MyISAM表性能影响最大的一个参数, 不过数据库中多为Innodb

01
mysql> show variables like 'key_buffer_size';
02
+-----------------+----------+
03
| Variable_name   | Value    |
04
+-----------------+----------+
05
| key_buffer_size | 67108864 |
06
+-----------------+----------+
07

08
mysql> show global status like 'key_read%';
09
+-------------------+----------+
10
| Variable_name     | Value    |
11
+-------------------+----------+
12
| Key_read_requests | 25629497 |
13
| Key_reads         | 66071    |
14
+-------------------+----------+
一共有25629497个索引读取请求,有66071个请求在内存中没有找到直接从硬盘读取索引,计算索引未命中缓存的概率:
key_cache_miss_rate = Key_reads / Key_read_requests * 100% =0.27%
需要适当加大key_buffer_size

1
mysql> show global status like 'key_blocks_u%';
2
+-------------------+-------+
3
| Variable_name     | Value |
4
+-------------------+-------+
5
| Key_blocks_unused | 10285 |
6
| Key_blocks_used   | 47705 |
7
+-------------------+-------+
Key_blocks_unused表示未使用的缓存簇(blocks)数,Key_blocks_used表示曾经用到的最大的blocks数
Key_blocks_used / (Key_blocks_unused + Key_blocks_used) * 100% ≈ 18% (理想值 ≈ 80%)

6, 临时表

1
mysql> show global status like 'created_tmp%';
2
+-------------------------+---------+
3
| Variable_name           | Value   |
4
+-------------------------+---------+
5
| Created_tmp_disk_tables | 4184337 |
6
| Created_tmp_files       | 4124    |
7
| Created_tmp_tables      | 4215028 |
8
+-------------------------+---------+
每次创建临时表,Created_tmp_tables增加,如果是在磁盘上创建临时表,Created_tmp_disk_tables也增加,Created_tmp_files表示MySQL服务创建的临时文件文件数: