LinuxEye - Linux系统教程

LinuxEye - Linux系统教程

当前位置: 主页 > 数据库 >

mysql数据库性能优化方案

时间:2012-07-28 11:24来源:未知 编辑:admin 点击:
mysql的优化同sql server相比,更为麻烦与复杂,同样的设置,在不同的环境下 ,由于内存,访问量,读写频率,数据差异等等情况,可能会出现不同的结果,因此简单地根据某个给出方案
mysql的优化同sql server相比,更为麻烦与复杂,同样的设置,在不同的环境下 ,由于内存,访问量,读写频率,数据差异等等情况,可能会出现不同的结果,因此简单地根据某个给出方案来配置mysql是行不通的,最好能使用 status信息对mysql进行具体的优化。
mysql> show global status;

可以列出mysql服务器运行各种状态值,另外,查询mysql服务器配置信息语句:
mysql> show variables;


一、慢查询

mysql> show variables like 'slow%';
+------------------+-------+
| variable_name     | value |
+------------------+-------+
| log_slow_queries | on     |
| slow_launch_time | 2      |
+------------------+-------+

mysql> show global status like 'slow%';
+---------------------+-------+
| variable_name        | value |
+---------------------+-------+
| slow_launch_threads | 0      |
| slow_queries         | 4148 |
+---------------------+-------+ 
配置中打开了记录慢查询,执行时间超过2秒的即为慢查询,系统显示有4148个慢查询,你可以分析慢查询日志,找出有问题的sql语句,慢查询时间不宜设置过长,否则意义不大,最好在5秒以内,如果你需要微秒级别的慢查询,可以考虑给mysql打补丁:http://www.percona.com /docs/wiki/release:start,记得找对应的版本。

打开慢查询日志可能会对系统性能有一点点影响,如果你的mysql是主-从结构,可以考虑打开其中一台从服务器的慢查询日志,这样既可以监控慢查询,对系统性能影响又小。

二、连接数

经常会遇见”mysql: error 1040: too many connections”的情况,一种是访问量确实很高,mysql服务器抗不住,这个时候就要考虑增加从服务器分散读压力,另外一种情况是mysql配 置文件中max_connections值过小:
mysql> show variables like 'max_connections';
+-----------------+-------+
| variable_name    | value |
+-----------------+-------+
| max_connections | 256   |
+-----------------+-------+
这台mysql服务器最大连接数是256,然后查询一下服务器响应的最大连接数:
mysql> show global status like 'max_used_connections';
mysql服务器过去的最大连接数是245,没有达到服务器连接数上限256,应该没有出现1040错误,比较理想的设置是
max_used_connections / max_connections * 100% ≈ 85%
最大连接数占上限连接数的85%左右,如果发现比例在10%以下,mysql服务器连接数上限设置的过高了。

三、key_buffer_size

key_buffer_size是对myisam表性能影响最大的一个参数,下面一台以myisam为主要存储引擎服务器的配置:

mysql> show variables like 'key_buffer_size';

    +-----------------+------------+
    | variable_name    | value |
    +-----------------+------------+
    | key_buffer_size | 536870912 |
    +-----------------+------------+
分配了512mb内存给key_buffer_size,我们再看一下key_buffer_size的使用情况:
mysql> show global status like 'key_read%';
+------------------------+-------------+
| variable_name           | value        |
+------------------------+-------------+
| key_read_requests       | 27813678764 |
| key_reads               | 6798830      |
+------------------------+-------------+
一共有27813678764个索引读取请求,有6798830个请求在内存中没有找到直接从硬盘读取索引,计算索引未命中缓存的概率:
key_cache_miss_rate = key_reads / key_read_requests * 100%
比如上面的数据,key_cache_miss_rate为0.0244%,4000个索引读取请求才有一个直接读硬盘,已经很bt了,key_cache_miss_rate在0.1%以下都很好(每1000个请求有一个直接读硬盘),如果key_cache_miss_rate在 0.01%以下的话,key_buffer_size分配的过多,可以适当减少。

mysql服务器还提供了key_blocks_*参数:

mysql> 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_buffer_size,要么就是过渡索引了,把缓存占满了。比较理想的设置:
key_blocks_used / (key_blocks_unused + key_blocks_used) * 100% ≈ 80%


四、临时表

mysql> show global status like 'created_tmp%';
+-------------------------+---------+
| variable_name            | value    |
+-------------------------+---------+
| created_tmp_disk_tables | 21197    |
| created_tmp_files        | 58       |
| created_tmp_tables       | 1771587 |
+-------------------------+---------+
每次创建临时表,created_tmp_tables增加,如果是在磁盘上创建临时表,created_tmp_disk_tables也增加,created_tmp_files表示mysql服务创建的临时文件文件数,比较理想的配置是:
created_tmp_disk_tables / created_tmp_tables * 100% <= 25%比如上面的服务器created_tmp_disk_tables / created_tmp_tables * 100% = 1.20%,应该相当好了。我们再看一下mysql服务器对临时表的配置:
mysql> show variables where variable_name in ('tmp_table_size', 'max_heap_table_size');
+---------------------+-----------+
| variable_name        | value      |
+---------------------+-----------+
| max_heap_table_size | 268435456 |
| tmp_table_size       | 536870912 |
+---------------------+-----------+
只有256mb以下的临时表才能全部放内存,超过的就会用到硬盘临时表。

五、open table情况

mysql> show global status like 'open%tables%';
+---------------+-------+
| variable_name | value |
+---------------+-------+
| open_tables    | 919    |
| opened_tables | 1951  |
+---------------+-------+
open_tables表示打开表的数量,opened_tables表示打开过的表数量,如果opened_tables数量过大,说明配置中 table_cache(5.1.3之后这个值叫做table_open_cache)值可能太小,我们查询一下服务器table_cache值:
mysql> show variables like 'table_cache';
+---------------+-------+
| variable_name | value |
+---------------+-------+
| table_cache    | 2048  |
+---------------+-------+
比较合适的值为:
open_tables / opened_tables * 100% >= 85%
open_tables / table_cache * 100% <= 95%


六、进程使用情况

mysql> show global status like 'thread%';
+-------------------+-------+
| variable_name      | value |
+-------------------+-------+
| threads_cached     | 46     |
| threads_connected | 2      |
| threads_created    | 570    |
| threads_running    | 1      |
+-------------------+-------+
如果我们在mysql服务器配置文件中设置了thread_cache_size,当客户端断开之后,服务器处理此客户的线程将会缓存起来以响应下一个客 户而不是销毁(前提是缓存数未达上限)。threads_created表示创建过的线程数,如果发现threads_created值过大的话,表明 mysql服务器一直在创建线程,这也是比较耗资源,可以适当增加配置文件中thread_cache_size值,查询服务器 thread_cache_size配置:
mysql> show variables like 'thread_cache_size';
+-------------------+-------+
| variable_name      | value |
+-------------------+-------+
| thread_cache_size | 64     |
+-------------------+-------+
示例中的服务器还是挺健康的。

七、查询缓存(query cache)

mysql> show global status like 'qcache%';
+-------------------------+-----------+
| variable_name            | value      |
+-------------------------+-----------+
| qcache_free_blocks       | 22756      |
| qcache_free_memory       | 76764704  |
| qcache_hits              | 213028692 |
| qcache_inserts           | 208894227 |
| qcache_lowmem_prunes     | 4010916    |
| qcache_not_cached        | 13385031  |
| qcache_queries_in_cache | 43560      |
| qcache_total_blocks      | 111212     |
+-------------------------+-----------+

mysql查询缓存变量解释:

qcache_free_blocks:缓存中相邻内存块的个数。数目大说明可能有碎片。flush query cache会对缓存中的碎片进行整理,从而得到一个空闲块。

qcache_free_memory:缓存中的空闲内存。

qcache_hits:每次查询在缓存中命中时就增大

qcache_inserts:每次插入一个查询时就增大。命中次数除以插入次数就是不中比率。

qcache_lowmem_prunes: 缓存出现内存不足并且必须要进行清理以便为更多查询提供空间的次数。这个数字最好长时间来看;如果这个数字在不断增长,就表示可能碎片非常严重,或者内存 很少。(上面的 free_blocks和free_memory可以告诉您属于哪种情况)

qcache_not_cached:不适合进行缓存的查询的数量,通常是由于这些查询不是 select 语句或者用了now()之类的函数。

qcache_queries_in_cache:当前缓存的查询(和响应)的数量。

qcache_total_blocks:缓存中块的数量。

我们再查询一下服务器关于query_cache的配置:

mysql> show variables like 'query_cache%';
+------------------------------+-----------+
| variable_name                 | value      |
+------------------------------+-----------+
| query_cache_limit             | 2097152    |
| query_cache_min_res_unit      | 4096       |
| query_cache_size              | 203423744 |
| query_cache_type              | on         |
| query_cache_wlock_invalidate | off        |
+------------------------------+-----------+

各字段的解释:

query_cache_limit:超过此大小的查询将不缓存

query_cache_min_res_unit:缓存块的最小大小

query_cache_size:查询缓存大小

query_cache_type:缓存类型,决定缓存什么样的查询,示例中表示不缓存 select sql_no_cache 查询

query_cache_wlock_invalidate:当有其他客户端正在对myisam表进行写操作时,如果查询在query cache中,是否返回cache结果还是等写操作完成再读表获取结果。

query_cache_min_res_unit的配置是一柄”双刃剑”,默认是4kb,设置值大对大数据查询有好处,但如果你的查询都是小数据查询,就容易造成内存碎片和浪费。

查询缓存碎片率 = qcache_free_blocks / qcache_total_blocks * 100%

如果查询缓存碎片率超过20%,可以用flush query cache整理缓存碎片,或者试试减小query_cache_min_res_unit,如果你的查询都是小数据量的话。

查询缓存利用率 = (query_cache_size - qcache_free_memory) / query_cache_size * 100%

查询缓存利用率在25%以下的话说明query_cache_size设置的过大,可适当减小;查询缓存利用率在80%以上而且qcache_lowmem_prunes > 50的话说明query_cache_size可能有点小,要不就是碎片太多。

查询缓存命中率 = (qcache_hits - qcache_inserts) / qcache_hits * 100%

示例服务器 查询缓存碎片率 = 20.46%,查询缓存利用率 = 62.26%,查询缓存命中率 = 1.94%,命中率很差,可能写操作比较频繁吧,而且可能有些碎片。
 

八、排序使用情况

mysql> show global status like 'sort%';
+-------------------+------------+
| variable_name      | value       |
+-------------------+------------+
| sort_merge_passes | 29          |
| sort_range         | 37432840    |
| sort_rows          | 9178691532 |
| sort_scan          | 1860569     |
+-------------------+------------+
sort_merge_passes 包括两步。mysql 首先会尝试在内存中做排序,使用的内存大小由系统变量 sort_buffer_size 决定,如果它的大小不够把所有的记录都读到内存中,mysql 就会把每次在内存中排序的结果存到临时文件中,等 mysql 找到所有记录之后,再把临时文件中的记录做一次排序。这再次排序就会增加 sort_merge_passes。实际上,mysql 会用另一个临时文件来存再次排序的结果,所以通常会看到 sort_merge_passes 增加的数值是建临时文件数的两倍。因为用到了临时文件,所以速度可能会比较慢,增加 sort_buffer_size 会减少 sort_merge_passes 和 创建临时文件的次数。但盲目的增加 sort_buffer_size 并不一定能提高速度,见 how fast can you sort data with mysql?
另外,增加read_rnd_buffer_size(3.2.3是record_rnd_buffer_size)的值对排序的操作也有一点的好处。

九、文件打开数(open_files)

mysql> show global status like 'open_files';
+---------------+-------+
| variable_name | value |
+---------------+-------+
| open_files     | 1410  |
+---------------+-------+
mysql> show variables like 'open_files_limit';
+------------------+-------+
| variable_name     | value |
+------------------+-------+
| open_files_limit | 4590  |
+------------------+-------+
比较合适的设置:open_files / open_files_limit * 100% <= 75%

十、表锁情况

mysql> show global status like 'table_locks%';
+-----------------------+-----------+
| variable_name          | value      |
+-----------------------+-----------+
| table_locks_immediate | 490206328 |
| table_locks_waited     | 2084912    |
+-----------------------+-----------+
table_locks_immediate 表示立即释放表锁数,table_locks_waited表示需要等待的表锁数,如果 table_locks_immediate / table_locks_waited > 5000,最好采用innodb引擎,因为innodb是行锁而myisam是表锁,对于高并发写入的应用innodb效果会好些。示例中的服务器 table_locks_immediate / table_locks_waited = 235,myisam就足够了。

十一、表扫描情况

mysql> show global status like 'handler_read%';
+-----------------------+-------------+
| variable_name          | value        |
+-----------------------+-------------+
| handler_read_first     | 5803750      |
| handler_read_key       | 6049319850  |
| handler_read_next      | 94440908210 |
| handler_read_prev      | 34822001724 |
| handler_read_rnd       | 405482605    |
| handler_read_rnd_next | 18912877839 |
+-----------------------+-------------+
Handler_read_first

The number of times the first entry was read from an index. If this value is high, it suggests that the server is doing a lot of full index scans; for example, SELECT col1 FROM foo, assuming that col1 is indexed.

此选项表明SQL是在做一个全索引扫描,注意是全部,而不是部分,所以说如果存在WHERE语句,这个选项是不会变的。如果这个选项的数值很大,既是好事 也是坏事。说它好是因为毕竟查询是在索引里完成的,而不是数据文件里,说它坏是因为大数据量时,简便是索引文件,做一次完整的扫描也是很费时的。
FLUSH STATUS;

SELECT col1 FROM foo;

mysql> SHOW SESSION STATUS LIKE 'Handler_read%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| Handler_read_first    | 1     |
| Handler_read_key      | 0     |
| Handler_read_next     | 9     |
| Handler_read_prev     | 0     |
| Handler_read_rnd      | 0     |
| Handler_read_rnd_next | 0     |
+-----------------------+-------+
6 rows in set (0.00 sec)

mysql> EXPLAIN SELECT col1 FROM foo\G
         type: index
        Extra: Using index

Handler_read_key

The number of requests to read a row based on a key. If this value is high, it is a good indication that your tables are properly indexed for your queries.

此选项数值如果很高,那么恭喜你,你的系统高效的使用了索引,一切运转良好。
FLUSH STATUS;

SELECT * FROM foo WHERE col1 = 'e';

mysql> SHOW SESSION STATUS LIKE 'Handler_read%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| Handler_read_first    | 0     |
| Handler_read_key      | 1     |
| Handler_read_next     | 1     |
| Handler_read_prev     | 0     |
| Handler_read_rnd      | 0     |
| Handler_read_rnd_next | 0     |
+-----------------------+-------+

mysql> EXPLAIN SELECT * FROM foo WHERE col1 = 'e'\G
         type: ref
        Extra: Using where

Handler_read_next

The number of requests to read the next row in key order. This value is incremented if you are querying an index column with a range constraint or if you are doing an index scan.

此选项表明在进行索引扫描时,按照索引从数据文件里取数据的次数。
FLUSH STATUS;

SELECT col1 FROM foo ORDER BY col1 ASC;

mysql> SHOW SESSION STATUS LIKE 'Handler_read%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| Handler_read_first    | 1     |
| Handler_read_key      | 0     |
| Handler_read_next     | 9     |
| Handler_read_prev     | 0     |
| Handler_read_rnd      | 0     |
| Handler_read_rnd_next | 0     |
+-----------------------+-------+

mysql> EXPLAIN SELECT * FROM foo WHERE col1 = 'e'\G
         type: index
        Extra: Using index

Handler_read_prev

The number of requests to read the previous row in key order. This read method is mainly used to optimize ORDER BY ... DESC.

此选项表明在进行索引扫描时,按照索引倒序从数据文件里取数据的次数,一般就是ORDER BY ... DESC。
FLUSH STATUS;

SELECT col1 FROM foo ORDER BY col1 DESC;

mysql> SHOW SESSION STATUS LIKE 'Handler_read%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| Handler_read_first    | 0     |
| Handler_read_key      | 0     |
| Handler_read_next     | 0     |
| Handler_read_prev     | 9     |
| Handler_read_rnd      | 0     |
| Handler_read_rnd_next | 0     |
+-----------------------+-------+

mysql> EXPLAIN SELECT col1 FROM foo ORDER BY col1 DESC\G
         type: index
        Extra: Using index


Handler_read_rnd

The number of requests to read a row based on a fixed position. This value is high if you are doing a lot of queries that require sorting of the result. You probably have a lot of queries that require MySQL to scan entire tables or you have joins that don't use keys properly.

简单的说,就是查询直接操作了数据文件,很多时候表现为没有使用索引或者文件排序。

FLUSH STATUS;

SELECT * FROM foo ORDER BY col2 DESC;

mysql> SHOW SESSION STATUS LIKE 'Handler_read%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| Handler_read_first    | 0     |
| Handler_read_key      | 0     |
| Handler_read_next     | 0     |
| Handler_read_prev     | 0     |
| Handler_read_rnd      | 9     |
| Handler_read_rnd_next | 10    |
+-----------------------+-------+

mysql> EXPLAIN SELECT * FROM foo ORDER BY col2 DESC\G
         type: ALL
        Extra: Using filesort


Handler_read_rnd_next

The number of requests to read the next row in the data file. This value is high if you are doing a lot of table scans. Generally this suggests that your tables are not properly indexed or that your queries are not written to take advantage of the indexes you have.

此选项表明在进行数据文件扫描时,从数据文件里取数据的次数。
FLUSH STATUS;

SELECT * FROM foo;

mysql> SHOW SESSION STATUS LIKE 'Handler_read%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| Handler_read_first    | 0     |
| Handler_read_key      | 0     |
| Handler_read_next     | 0     |
| Handler_read_prev     | 0     |
| Handler_read_rnd      | 0     |
| Handler_read_rnd_next | 10    |
+-----------------------+-------+

mysql> EXPLAIN SELECT * FROM foo ORDER BY col2 DESC\G
         type: ALL
        Extra: Using filesort

不同平台,不同版本的MySQL,在运行上面例子的时候,Handler_read_*的数值可能会有所不同,这并不要紧,关键是你要意识到 Handler_read_*可以协助你理解MySQL处理查询的过程,很多时候,为了完成一个查询任务,我们往往可以写出几种查询语句,这时,你不妨挨 个按照上面的方式执行,根据结果中的Handler_read_*数值,你就能相对容易的判断各种查询方式的优劣。

说到判断查询方式优劣这个问题,就再顺便提提show profile语法,在新版MySQL里提供了这个功能:
mysql> set profiling=on;

mysql> use mysql;
mysql> select * from user;

mysql> show profile;
+--------------------+----------+
| Status             | Duration |
+--------------------+----------+
| starting           | 0.000078 |
| Opening tables     | 0.000022 |
| System lock        | 0.000010 |
| Table lock         | 0.000014 |
| init               | 0.000054 |
| optimizing         | 0.000008 |
| statistics         | 0.000015 |
| preparing          | 0.000014 |
| executing          | 0.000007 |
| Sending data       | 0.000139 |
| end                | 0.000007 |
| query end          | 0.000007 |
| freeing items      | 0.000044 |
| logging slow query | 0.000004 |
| cleaning up        | 0.000005 |
+--------------------+----------+
15 rows in set (0.00 sec)

mysql> show profiles;
+----------+------------+--------------------+
| Query_ID | Duration   | Query              |
+----------+------------+--------------------+
|        1 | 0.00017725 | SELECT DATABASE(). |
|        2 | 0.00042675 | select * from user |
+----------+------------+--------------------+
2 rows in set (0.00 sec)

调出服务器完成的查询请求次数:
mysql> show global status like 'com_select';
+---------------+-----------+
| variable_name | value      |
+---------------+-----------+
| com_select     | 222693559 |
+---------------+-----------+
计算表扫描率:

表扫描率 = handler_read_rnd_next / com_select

如果表扫描率超过4000,说明进行了太多表扫描,很有可能索引没有建好,增加read_buffer_size值会有一些好处,但最好不要超过8mb。

转载请保留固定链接: https://linuxeye.com/database/724.html

------分隔线----------------------------
标签:mysqlmysql性能优化
栏目列表
推荐内容