zoukankan      html  css  js  c++  java
  • MySQL性能分析工具之PROFILE

    http://blog.itpub.net/20893244/viewspace-2134879/
     
    http://blog.itpub.net/29371470/viewspace-1355948/
     
     

    分类: MySQL

    分析SQL执行带来的开销是优化SQL的常用手段,在MySQL数据库中,可以通过配置profiling参数来启用SQL剖析。
    它只能在session级别来设置,设置后影响当前session;当它开启后,后续执行的SQL语句都将记录其资源开销,诸如IO,上下文,CPU,MEMORY等。

    实验环境:

    1. mysql> select version();
    2. +------------+
    3. | version() |
    4. +------------+
    5. | 5.7.17-log |
    6. +------------+
    7. 1 row in set (0.00 sec)

    与profile相关的三个参数:

    1. mysql> show variables like '%profil%';
    2. +------------------------+-------+
    3. | Variable_name | Value |
    4. +------------------------+-------+
    5. | have_profiling | YES |                  ---用于控制是否由系统变量开启或禁用profiling
    6. | profiling | OFF |                       ---开启SQL语句剖析功能
    7. | profiling_history_size | 15 |           ---设置保留profiling的数目,缺省为15,范围为0至100,为0时将禁用profiling
    8. +------------------------+-------+
    9. 3 rows in set (0.01 sec)

    开启profiling,有个警告,这个参数在以后会被删除,用information_scheam.PROFILING替代。

    1. mysql> set profiling=1;
    2. Query OK, 0 rows affected, 1 warning (0.00 sec)

    3. mysql> show warnings;
      +---------+------+----------------------------------------------------------------------+
      | Level   | Code | Message                                                              |
      +---------+------+----------------------------------------------------------------------+
      | Warning | 1287 | '@@profiling' is deprecated and will be removed in a future release. |
      +---------+------+----------------------------------------------------------------------+

    执行一条sql测试一下:

    1. select * from oms3.customers where `type` = 1 AND `status` < 7 AND `isarea` = 6 AND `into_time`>='2016-12-01'AND `into_time`<='2017-01-02 23:59:59' order by score desc limit 40,20

    sql执行很慢,执行完成之后查看:

    1. mysql> show profiles;
    2. +----------+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    3. | Query_ID | Duration | Query |
    4. +----------+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    5. | 7 | 0.00022275 | SELECT * FROM setup_actors |
    6. | 8 | 0.00016050 | SELECT DATABASE() |
    7. | 9 | 0.00032350 | show databases |
    8. | 10 | 0.00024050 | show tables |
    9. | 11 | 0.00019250 | SELECT * FROM setup_actors |
    10. | 12 | 0.00183950 | show variables like "profiling_hist%" |
    11. | 13 | 0.00192500 | show variables like '%profil%' |
    12. | 14 | 0.00011550 | show warnings |
    13. | 15 | 0.00044725 | help 'show profile' |
    14. | 16 | 0.00013875 | set profiling=1 |
    15. | 17 | 0.00011550 | show warnings |
    16. | 18 | 0.00025075 | select * from customers where `type` = 1 AND `status` < 7 AND `isarea` = 6 AND `into_time`>='2016-12-01'AND `into_time`<='2017-01-02 23:59:59' order by score desc limit 40,20 |
    17. | 19 | 333.19133875 | select * from oms3.customers where `type` = 1 AND `status` < 7 AND `isarea` = 6 AND `into_time`>='2016-12-01'AND `into_time`<='2017-01-02 23:59:59' order by score desc limit 40,20 |
    18. | 20 | 0.00011250 | show profilings |
    19. | 21 | 0.00010975 | show profilings |
    20. +----------+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    21. 15 rows in set, 1 warning (0.00 sec)

    22. mysql> set @query_id=19;                ---上面显示是query_id为19
    23. Query OK, 0 rows affected (0.00 sec)

    24. mysql> select STATE,sum(duration) as Total_R,          ---用这条sql查询具体是哪里出问题,这条sql引用于《High Performance MySQL,Third Edition》
    25.     -> ROUND(
    26.     -> 100*SUM(DURATION)/
    27.     -> (SELECT SUM(DURATION) FROM INFORMATiON_SCHEMA.PROFILING WHERE QUERY_ID=@query_id),2) as Pct_R,
    28.     -> count(*) as calls,
    29.     -> sum(duration)/count(*) as "R/Call"
    30.     -> from information_schema.profiling
    31.     -> where query_id=@query_id
    32.     -> group by state
    33.     -> order by Total_R desc;
    34. +----------------------+------------+-------+-------+----------------+
    35. | STATE | Total_R | Pct_R | calls | R/Call |
    36. +----------------------+------------+-------+-------+----------------+
    37. | Sending data | 332.162424 | 99.69 | 1 | 332.1624240000 |         ---这个state基本占用了全部的资源,那么对这条sql的优化就着重于减少io上。
    38. | statistics | 1.027729 | 0.31 | 1 | 1.0277290000 |
    39. | Opening tables | 0.000519 | 0.00 | 1 | 0.0005190000 |
    40. | freeing items | 0.000157 | 0.00 | 1 | 0.0001570000 |
    41. | starting | 0.000147 | 0.00 | 1 | 0.0001470000 |
    42. | init | 0.000123 | 0.00 | 1 | 0.0001230000 |
    43. | logging slow query | 0.000096 | 0.00 | 1 | 0.0000960000 |
    44. | preparing | 0.000035 | 0.00 | 1 | 0.0000350000 |
    45. | cleaning up | 0.000019 | 0.00 | 1 | 0.0000190000 |
    46. | optimizing | 0.000016 | 0.00 | 1 | 0.0000160000 |
    47. | end | 0.000014 | 0.00 | 1 | 0.0000140000 |
    48. | System lock | 0.000014 | 0.00 | 1 | 0.0000140000 |
    49. | closing tables | 0.000013 | 0.00 | 1 | 0.0000130000 |
    50. | query end | 0.000013 | 0.00 | 1 | 0.0000130000 |
    51. | Sorting result | 0.000010 | 0.00 | 1 | 0.0000100000 |
    52. | checking permissions | 0.000009 | 0.00 | 1 | 0.0000090000 |
    53. | executing | 0.000003 | 0.00 | 1 | 0.0000030000 |
    54. +----------------------+------------+-------+-------+----------------+
    55. 17 rows in set, 18 warnings (0.00 sec)

    其他用法:

    1. mysql> SHOW PROFILE CPU FOR QUERY 2;
    2. +----------------------+----------+----------+------------+
    3. | Status | Duration | CPU_user | CPU_system |
    4. +----------------------+----------+----------+------------+
    5. | checking permissions | 0.000040 | 0.000038 | 0.000002 |
    6. | creating table | 0.000056 | 0.000028 | 0.000028 |
    7. | After create | 0.011363 | 0.000217 | 0.001571 |
    8. | query end | 0.000375 | 0.000013 | 0.000028 |
    9. | freeing items | 0.000089 | 0.000010 | 0.000014 |
    10. | logging slow query | 0.000019 | 0.000009 | 0.000010 |
    11. | cleaning up | 0.000005 | 0.000003 | 0.000002 |
    12. +----------------------+----------+----------+------------+

    更多用法详见:

    1. help show profiles;

    总结:

    上面说到这个参数以后会被废弃,但是我觉得任重而道远,因为现在information_scheam.PROFILING表里连具体执行的sql都没记录......

    PS:关于上面那条慢SQL的优化,请看我另一篇博客:http://blog.itpub.net/20893244/viewspace-2134636/

  • 相关阅读:
    CUDA Error
    yolo v3 loss=nan, Avg loss=nan的一种原因
    C++ LinearRegression代码实现
    C++ 常用数学运算(加减乘除)代码实现 Utils.h, Utils.cpp(有疑问欢迎留言)
    C++ 彩色图像(RGB)三通道直方图计算和绘制,图像逆时针旋转90° 实现代码
    Leetcode 1005. Maximize Sum Of Array After K Negations
    Leetcode 1006. Clumsy Factorial
    Leetcode 617. Merge Two Binary Trees
    Leetcode 477. Total Hamming Distance
    python进制转换
  • 原文地址:https://www.cnblogs.com/zengkefu/p/6519010.html
Copyright © 2011-2022 走看看