MYSQL performance view (hit rate, slow query)

There are many articles on the Internet that teach how to configure the MySQL server, but considering the difference in server hardware configuration and specific applications, the methods in those articles can only be used as a reference for preliminary settings. We need to optimize the configuration according to our own situation. A good practice is The MySQL server runs stably for a period of time, and is optimized according to the "state" of the server.
  mysql> show global status;
  You can list the MySQL server running various status values. In addition, query the MySQL server configuration information statement:
  mysql> show variables;
  1. Slow query
  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 |
  +---------------------+-------+  
        The slow query log is turned on in the configuration. If the execution time exceeds 2 seconds, it is a slow query. The system shows that there are 4148 slow queries. You can analyze the slow query log to find out the problematic SQL statement. The slow query time should not be set too long. Otherwise, it doesn't make much sense, preferably within 5 seconds. If you need microsecond-level slow queries, you can consider patching MySQL: http://www.percona.com/docs/wiki/release:start , remember to find the corresponding version of.
  Turning on the slow query log may have a little impact on system performance. If your MySQL is a master-slave structure, you can consider opening the slow query log of one of the slave servers, so that you can monitor slow queries and have little impact on system performance. .
 
  Second, the number of connections
  I often encounter the situation of "MySQL: ERROR 1040: Too many connections". One is that the traffic volume is really high, and the MySQL server can't stand it. At this time, it is necessary to consider increasing the distributed reading pressure from the server. Another situation is the MySQL configuration. The max_connections value in the file is too small:
  mysql> show variables like 'max_connections';
  +-----------------+-------+
  | Variable_name | Value |
  +-----------------+-------+
  | max_connections | 256 |
  +-----------------+-------+  
       The maximum number of connections to this MySQL server is 256, and then query the maximum number of connections the server responds to:
  mysql> show global status like ‘Max_used_connections’;
  The maximum number of connections of the MySQL server in the past was 245, which did not reach the upper limit of the number of server connections of 256. There should be no 1040 error. The ideal setting is:
  Max_used_connections / max_connections * 100% ≈ 85%
  The maximum number of connections accounts for about 85% of the upper limit of connections. If the ratio is found to be less than 10%, the upper limit of MySQL server connections is set too high.
 
  三、Key_buffer_size
  key_buffer_size is a parameter that has the greatest impact on the performance of MyISAM tables. The following configuration uses MyISAM as the main storage engine server:
  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 | mysql
  +------------------------+-------------+
  | 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?(引自http://qroom.blogspot.com/2007/09/mysql-select-sort.html,貌似被墙) mysql
  另外,增加read_rnd_buffer_size(3.2.3是record_rnd_buffer_size)的值对排序的操作也有一点的好处,参见:http://www.mysqlperformanceblog.com/2007/07/24/what-exactly-is-read_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 |
  +-----------------------+-------------+  
        各字段解释参见http://hi.baidu.com/thinkinginlamp/blog/item/31690cd7c4bc5cdaa144df9c.html,调出服务器完成的查询请求次数:
  mysql> show global status like 'com_select';
  +---------------+-----------+
  | Variable_name | Value |
  +---------------+-----------+
  | Com_select | 222693559 |
  +---------------+-----------+  
       计算表扫描率:
  表扫描率 = Handler_read_rnd_next / Com_select
  如果表扫描率超过4000,说明进行了太多表扫描,很有可能索引没有建好,增加read_buffer_size值会有一些好处,但最好不要超过8MB。
  后记:
  文中提到一些数字都是参考值,了解基本原理就可以,除了MySQL提供的各种status值外,操作系统的一些性能指标也很重要,比如常用的top,iostat等,尤其是iostat,现在的系统瓶颈一般都在磁盘IO上,关于iostat的使用.
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325979324&siteId=291194637