Performance Optimization Performance Optimization -MySQL common commands

There are many online articles to teach how to configure MySQL server, but taking into account different server hardware configuration, the difference in specific applications, practice those articles only as a preliminary set of reference, we need to be configured to optimize according to their own circumstances, good practice MySQL server is running and stable operation after a period of time, be optimized for the "state" of the server.
  mysql> show global status;
  lists the various status values MySQL server running addition, MySQL server configuration information query statement:
  MySQL> Show Variables;
  a, slow query
  MySQL> Show Variables like '%% SLOW';
  ± --- ± ------ + --------------
  | variable_name | Value |
  ± ± ---- ----------------- - +
  | log_slow_queries | the ON |
  | slow_launch_time | 2 |
  ± ----------------- + ------ ±
  MySQL> Show Global Status like '%% SLOW' ;
  ± -------------------- ± ------ +
  | variable_name | the Value |
  ± -------------- + ------ ± ------
  | Slow_launch_threads | 0 |
  | Slow_queries | 4148 |
  ±--------------------±------+

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

Open the slow query log might affect system performance a little, if you are a MySQL master - slave structure, can be considered one of them open from the slow query log server, both to monitor slow queries, and small impact on system performance .

Second, the number of connections
  often meet the situation "MySQL: Too many connections: ERROR 1040" , one is indeed very high traffic, MySQL server can not withstand, this time we should consider increasing the dispersion pressure reading from the server, another MySQL case profile max_connections is too small:
  MySQL> Show Variables like 'max_connections';
  ± ± ------ ---------------- +
  | variable_name | the value |
  ---------------- ------ + ± ±
  | max_connections | 256 |
  ± ± ---------------- - ---- +

   这台MySQL服务器最大连接数是256,然后查询一下服务器响应的最大连接数: 

mysql> show global status like 'Max_used_connections ';
  maximum number of connections MySQL server past 245, did not reach the number of server connection cap 256, 1040 error should not occur, the ideal setting is:
  Max_used_connections / max_connections * 100% ≈ 85%
  maximum accounting for about 85% of the number of connections of the upper limit number of connection, if it is found at the ratio of 10% or less, MySQL server connections set upper limit too high.

Three, key_buffer_size
  the key_buffer_size greatest impact a performance parameter MyISAM table, below a main memory configured to MyISAM engine server:
  MySQL> Show Variables like 'the 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%
  such as the above data, key_cache_miss_rate 0.0244% 4000 index read requests have a direct disk read, a very BT, key_cache_miss_rate very good (per 1000 requests have less than 0.1% a direct disk read), if key_cache_miss_rate 0.01% or less, excessive key_buffer_size distribution, can be appropriately reduced.
  MySQL server also provides key_blocks_ * Parameters:
  MySQL> Show like Global Status 'key_blocks_u%';
  ± ± ----- ----------------------- + -------
  | variable_name | Value |
  ± ± ------------ ----------------------- +
  | Key_blocks_unused | 0 |
  | Key_blocks_used The | 413 543 |
  ± ± ----------------------- + -----------------------

   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%
  , such as above server Created_tmp_disk_tables / Created_tmp_tables * 100% = 1.20 %, should be quite good. Let's look at MySQL server configuration to a temporary table:
  MySQL> Show the Variables in the WHERE variable_name ( '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: the number of adjacent memory block cache. Description large number of possible debris. FLUSH QUERY CACHE cache debris will arrange to obtain a free block.
  Qcache_free_memory: cache of free memory.
  Qcache_hits: it increases each query hit in the cache
  Qcache_inserts: Each time you insert a query increases. The number of hits divided by the number of insertions is not in the ratio.
  Qcache_lowmem_prunes: cache out of memory and must be cleaned more queries in order to provide the number of spaces. This figure is best long term; If this number is growing, says that debris may very severe, or very little memory. (Above free_blocks and free_memory can tell you which part of the case)
  Qcache_not_cached: not suitable for the number of query cache is usually a result of these functions or query is not a SELECT statement with the now () and the like.
  Qcache_queries_in_cache: the number of queries (and responses) current cached.
  Qcache_total_blocks: the number of cache blocks.
  Let us check what server configuration on the query_cache:
  MySQL> Show the 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 |

   +——————————+———–+

The fields are:
  the query_cache_limit: larger than this query will not be cached
  query_cache_min_res_unit: minimum size of the cache block
  query_cache_size: Query Cache Size
  query_cache_type: cache type, to determine whether cache what query, the example indicates not cached select sql_no_cache query
  query_cache_wlock_invalidate: when there are other client is when MyISAM table is written, if the query in the query cache, the cache whether to return or wait for the result of the write operation to complete the read table for results.
  query_cache_min_res_unit configuration is a two-edged "sword", the default is 4KB, a larger value is good for large data queries, but if your queries are small data query, it is easy to cause memory fragmentation and waste.
  Query cache fragmentation rate = Qcache_free_blocks / Qcache_total_blocks * 100%
  if the query cache fragmentation rate of more than 20%, are to be cached fragments with FLUSH QUERY CACHE, or try to reduce query_cache_min_res_unit, if your queries are small amounts of data words.
  Query cache utilization = (query_cache_size - Qcache_free_memory) / query_cache_size * 100%
  query cache utilization rate of 25% or less in the case described query_cache_size set large, it can be properly reduced; query cache utilization rate at 80% and Qcache_lowmem_prunes> 50 words of description query_cache_size may be a bit small, or is too fragmented.
  Query cache hit ratio = (Qcache_hits - Qcache_inserts) / Qcache_hits * 100%
  sample server query cache fragmentation rate = 20.46%, the query cache utilization rate = 62.26%, query cache hit rate = 1.94%, hit rate is poor, you may write to Compare it frequently, but there may be some debris.

Eight, sorting usage
  MySQL> Show like Global Status 'Sort%';
  ± ± ------------------ ----------- +
  | variable_name | the Value |
  ± ± ------------------ ----------- +
  | Sort_merge_passes | 29 |
  | Sort_range | 37.43284 million |
  | Sort_rows | 9,178,691,532 |
  | 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

In addition, the added value of read_rnd_buffer_size (3.2.3 is record_rnd_buffer_size) operation to sort of have a little advantage, see: 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 |
  ±--------------±----------+

   计算表扫描率:

Table scan rate = Handler_read_rnd_next / Com_select
  if a table scan rate over 4000, instructions were too many table scan, index probably not built, there will be some increase in the value of read_buffer_size benefits, but preferably not more than 8MB.
  Postscript:
  The article mentions some figures are reference values, you can understand the basic principles, in addition to the various MySQL status values provided by the operating system some of the performance indicators are also important, such as the commonly used top, iostat, etc., especially iostat, now the system bottlenecks typically on disk IO, regarding the use of iostat.

Before attempting to learn a new language to understand the design principles of the language that allows you to maintain a sober and explore the development of new language when this state.

Released 1064 original articles · won praise 888 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_42528266/article/details/103991035