当前位置:Gxlcms > 数据库问题 > mysql优化思路

mysql优化思路

时间:2021-07-01 10:21:17 帮助过:27人阅读

 

使用profiler来分析一条query的执行时间和性能瓶颈,
开启 profiling ;

set profiling=1;

 

随便执行一条语句 select count(*) from user where id>2;

show profiles;

 

得到

+----------+------------+--------------------------------------+
| Query_ID | Duration   | Query                                |
+----------+------------+--------------------------------------+
|        2 | 0.00009200 | set profiling=1                      |
|        5 | 0.02003525 | select count(*) from user where id>2 |
+----------+------------+--------------------------------------+

 

包含一个query_id和执行时间和query语句
通过query_id可以查看到更详细的信息;

show profile cpu ,block io  for query 5;

 

+----------------------+----------+----------+------------+--------------+---------------+
| Status               | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+----------------------+----------+----------+------------+--------------+---------------+
| starting             | 0.000170 | 0.000000 |   0.000000 |            0 |             0 |
| checking permissions | 0.000019 | 0.000000 |   0.000000 |            0 |             0 |
| Opening tables       | 0.000033 | 0.000000 |   0.000000 |            0 |             0 |
| init                 | 0.000061 | 0.000000 |   0.000000 |            0 |             0 |
| System lock          | 0.000021 | 0.000000 |   0.000000 |            0 |             0 |
| optimizing           | 0.000021 | 0.000000 |   0.000000 |            0 |             0 |
| statistics           | 0.010826 | 0.000000 |   0.000000 |          184 |             0 |
| preparing            | 0.000041 | 0.000000 |   0.000000 |            0 |             0 |
| executing            | 0.000005 | 0.000000 |   0.000000 |            0 |             0 |
| Sending data         | 0.008731 | 0.008000 |   0.000000 |            0 |             0 |
| end                  | 0.000020 | 0.000000 |   0.000000 |            0 |             0 |
| query end            | 0.000018 | 0.000000 |   0.000000 |            0 |             0 |
| closing tables       | 0.000012 | 0.000000 |   0.000000 |            0 |             0 |
| freeing items        | 0.000032 | 0.000000 |   0.000000 |            0 |             0 |
| cleaning up          | 0.000027 | 0.000000 |   0.000000 |            0 |             0 |
+----------------------+----------+----------+------------+--------------+---------------+

 


对mysql服务器的优化不要一上来就去优化sql语句,应该首先观察全局情况,至少要先搞清楚
问题出在哪,应该使用脚本来观察服务器一段时间(一天或更长)的健康状况,比如cpu,io,进程连接数等
最后才分析具体原因处在哪里;针对解决

mysql优化思路

标签:tab   分析   exe   set   miss   应该   使用   sel   故障   

人气教程排行