`

MySQL SQL Tuning:Profile定位单条QUERY性能瓶颈

 
阅读更多

当生了病的query来就诊时,如果身为医生的我们"胡庸医乱用虎狼药"
不仅于事无补,还浪费了大量的人力和时间成本,甚至会拖垮服务器
所以,我们在接受优化一条SQL,第一件事情便是要明白query病在哪里?
是IO?是CPU?只有明白瓶颈在哪里,方可对症下药,也才能药到病除
而MySQL QUERY Profiler是一个使用非常方便的QUERY诊断工具,5.0引入。5.1GA版嵌入
这个工具详细呈现了SQL在整个生命周期的每个动作,这和Oracle开启1046事件类似
我们可以很清晰地明白该SQL是在数据存取还是运算(排序或分组等)上花费得多
那么我们就不会很盲目地看到order by就去tuning sort buffer而忽略sorting result时间是如此之少

Profile语法:

  1. SHOW PROFILE [type [, type] ... ]  
  2.     [FOR QUERY n]  
  3.     [LIMIT row_count [OFFSET offset]]  
  4.   
  5. type:  
  6.     ALL  
  7.   | BLOCK IO  
  8.   | CONTEXT SWITCHES  
  9.   | CPU  
  10.   | IPC  
  11.   | MEMORY  
  12.   | PAGE FAULTS  
  13.   | SOURCE  
  14.   | SWAPS  

注解:
默认输出结果只会展示Status和Duration,我们可以指定type来扩展输出
我比较常用的是外加CPU和BLOCK IO来输出CPU和IO的负载,其实这些已经够了

默认profile是关闭的,通过profiling参数控制,为session级
开启:SET profiling=1
关闭:set profiling=0
查询:select @@profiling 
show profiles保存的query条数由参数profiling_history_size控制,默认是15,超过了会把前面的剔掉

  1. mysql> set profiling=1;  
  2.   
  3. mysql> select @@profiling;  
  4. +-------------+  
  5. | @@profiling |  
  6. +-------------+  
  7. |           1 |  
  8. +-------------+  
  9.   
  10. mysql> select * from employees.t order by first_name;  
  11.   
  12. mysql> show profiles;  
  13. +----------+------------+-----------------------------------------------+  
  14. | Query_ID | Duration   | Query                                         |  
  15. +----------+------------+-----------------------------------------------+  
  16. |        1 | 0.21138800 | show create table employees.t                 |  
  17. |        2 | 8.21691600 | select * from employees.t order by first_name |  
  18. +----------+------------+-----------------------------------------------+  
  19. 2 rows in set (0.00 sec)  
  20.   
  21. mysql> show profile cpu,block io for query 2;  
  22. +----------------------+----------+----------+------------+--------------+---------------+  
  23. | Status               | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |  
  24. +----------------------+----------+----------+------------+--------------+---------------+  
  25. | starting             | 0.000160 | 0.000000 |   0.000000 |            0 |             0 |  
  26. | checking permissions | 0.000026 | 0.000000 |   0.000000 |            0 |             0 |  
  27. | Opening tables       | 0.000055 | 0.000000 |   0.000000 |            0 |             0 |  
  28. | System lock          | 0.000033 | 0.000000 |   0.000000 |            0 |             0 |  
  29. | init                 | 0.000050 | 0.000000 |   0.000000 |            0 |             0 |  
  30. | optimizing           | 0.000026 | 0.000000 |   0.000000 |            0 |             0 |  
  31. | statistics           | 0.000145 | 0.000000 |   0.000000 |            0 |             0 |  
  32. | preparing            | 0.000118 | 0.000000 |   0.000000 |            0 |             0 |  
  33. | executing            | 0.000011 | 0.000000 |   0.000000 |            0 |             0 |  
  34. | Sorting result       | 2.838465 | 1.396087 |   1.140071 |            0 |             0 |  
  35. | Sending data         | 0.928078 | 0.544034 |   0.056003 |            0 |             0 |  
  36. | end                  | 0.000026 | 0.000000 |   0.000000 |            0 |             0 |  
  37. | query end            | 0.000011 | 0.000000 |   0.000000 |            0 |             0 |  
  38. | closing tables       | 0.000021 | 0.000000 |   0.000000 |            0 |             0 |  
  39. | freeing items        | 4.449672 | 0.000000 |   0.000000 |            0 |             0 |  
  40. | logging slow query   | 0.000014 | 0.000000 |   0.000000 |            0 |             0 |  
  41. | cleaning up          | 0.000005 | 0.000000 |   0.000000 |            0 |             0 |  
  42. +----------------------+----------+----------+------------+--------------+---------------+  
  43. 17 rows in set (0.00 sec)  

相同的信息我们还可以从information_schema里面输出,并且我们还可以对时间进行排序
因为Profile默认是按执行顺序排序的,而实际上我们更关心的是花费了多少时间,这才能方便知道哪些开销较大

  1. mysql> SELECT STATE, FORMAT(DURATION, 6) AS DURATION  
  2.     -> FROM INFORMATION_SCHEMA.PROFILING  
  3.     -> WHERE QUERY_ID = 2 ORDER BY DURATION DESC;  
  4. +----------------------+----------+  
  5. | STATE                | DURATION |  
  6. +----------------------+----------+  
  7. | freeing items        | 4.449672 |  
  8. | Sorting result       | 2.838465 |  
  9. | Sending data         | 0.928078 |  
  10. | starting             | 0.000160 |  
  11. | statistics           | 0.000145 |  
  12. | preparing            | 0.000118 |  
  13. | Opening tables       | 0.000055 |  
  14. | init                 | 0.000050 |  
  15. | System lock          | 0.000033 |  
  16. | end                  | 0.000026 |  
  17. | optimizing           | 0.000026 |  
  18. | checking permissions | 0.000026 |  
  19. | closing tables       | 0.000021 |  
  20. | logging slow query   | 0.000014 |  
  21. | query end            | 0.000011 |  
  22. | executing            | 0.000011 |  
  23. | cleaning up          | 0.000005 |  
  24. +----------------------+----------+  
  25. 17 rows in set (0.00 sec)  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics