mysql 性能优化工具

sanlanlan 2020-6-15 标签: mysql 浏览:983 评论:0


1.命令查看
show full processlist  //进程列表

show variables like 'max_connections' #最大连接

show status like 'max_used_connections'  #响应的连接数

Max_connections

MySQL的最大连接数,如果服务器的并发连接请求量比较大,建议调高此值,以增加并行连接数量,当然这建立在机器能支撑的情况下,因为如果连接数越多,介于MySQL会为每个连接提供连接缓冲区,就会开销越多的内存,所以要适当调整该值,不能盲目提高设值。

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

如果max_used_connections跟max_connections相同,那么就是max_connections设置过低或者超过服务器负载上限了,低于10%则设置过大。


2.慢查询日志

mysql> show variables like "%slow%";
+---------------------+-----------------------------------------------------------+
| Variable_name       | Value                                                     |
+---------------------+-----------------------------------------------------------+
| log_slow_queries    | OFF                                                       |
| slow_launch_time    | 2                                                         |
| slow_query_log      | OFF                                                       |
| slow_query_log_file | E:\phpStudy\PHPTutorial\MySQL\data\slow.log |
+---------------------+-----------------------------------------------------------+


查出访问次数最多的20个SQL语句:

mysqldumpslow-s c -t 20 host-slow.log


3.profiling分析查询


通过profiling命令得到更准确的SQL执行消耗系统资源的信息。

select @@profiling;

 mysql>set profiling=1; 执行需要测试的sql 语句:

 mysql>show profile for query 1(id); 
 +----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000027 |
| checking permissions | 0.000004 |
| Opening tables       | 0.000017 |
| System lock          | 0.000005 |
| init                 | 0.000007 |
| optimizing           | 0.000002 |
| statistics           | 0.000006 |
| preparing            | 0.000004 |
| executing            | 0.000001 |
| Sending data         | 0.000157 |
| end                  | 0.000002 |
| query end            | 0.000002 |
| closing tables       | 0.000003 |
| freeing items        | 0.000044 |
| logging slow query   | 0.000002 |
| cleaning up          | 0.000002 |
+----------------------+----------+
得到对应SQL语句执行的详细信息


show profile cpu for query 2 ;  
show profile memory for query 2 ; 
show profile block io,cpu for query 2;

测试完毕以后 ,关闭参数:mysql> set profiling=0

本文相关标签: mysql

发表评论: