设为首页 收藏本站
查看: 907|回复: 0

[经验分享] show profiles 查看MySQL语句的执行时间

[复制链接]

尚未签到

发表于 2018-10-6 10:53:56 | 显示全部楼层 |阅读模式
  show profiles
  作用范围
  这个命令只是在本会话内起作用,即无法分析本会话外的语句。
  开启分析功能后,所有本会话中的语句都被分析(甚至包括执行错误的语句),除了SHOW PROFILE和SHOW PROFILES两句本身。
  方法一:profiling
  mysql>  show variables like "profiling";
  +---------------+-------+
  | Variable_name | Value |
  +---------------+-------+
  | profiling     | OFF   |
  +---------------+-------+
  1 row in set (0.01 sec)
  mysql> set profiling = 1;
  Query OK, 0 rows affected, 1 warning (0.00 sec)
  mysql> select user,authentication_string,host from mysql.user;
  5 rows in set (0.00 sec)
  mysql> show profile for query 1;
  +----------------------+----------+
  | Status               | Duration |
  +----------------------+----------+
  | starting             | 0.000069 |
  | checking permissions | 0.000008 |
  | Opening tables       | 0.000109 |
  | init                 | 0.000018 |
  | System lock          | 0.000009 |
  | optimizing           | 0.000003 |
  | statistics           | 0.000014 |
  | preparing            | 0.000010 |
  | executing            | 0.000003 |
  | Sending data         | 0.000038 |
  | end                  | 0.000004 |
  | query end            | 0.000005 |
  | closing tables       | 0.000007 |
  | freeing items        | 0.000011 |
  | cleaning up          | 0.000013 |
  +----------------------+----------+
  15 rows in set, 1 warning (0.01 sec)
  mysql> show profile cpu, block io, memory,swaps,context switches,source for query 1;
  +----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+-------+-----------------------+----------------------+-------------+
  | Status               | Duration | CPU_user | CPU_system | Context_voluntary | Context_involuntary | Block_ops_in | Block_ops_out | Swaps | Source_function       | Source_file          | Source_line |
  +----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+-------+-----------------------+----------------------+-------------+
  | starting             | 0.000069 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |     0 | NULL                  | NULL                 |        NULL |
  | checking permissions | 0.000008 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |     0 | check_access          | sql_authorization.cc |         810 |
  | Opening tables       | 0.000109 | 0.001000 |   0.000000 |                 0 |                   0 |            0 |             0 |     0 | open_tables           | sql_base.cc          |        5650 |
  | init                 | 0.000018 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |     0 | handle_query          | sql_select.cc        |         121 |
  | System lock          | 0.000009 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |     0 | mysql_lock_tables     | lock.cc              |         323 |
  | optimizing           | 0.000003 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |     0 | optimize              | sql_optimizer.cc     |         151 |
  | statistics           | 0.000014 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |     0 | optimize              | sql_optimizer.cc     |         367 |
  | preparing            | 0.000010 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |     0 | optimize              | sql_optimizer.cc     |         475 |
  | executing            | 0.000003 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |     0 | exec                  | sql_executor.cc      |         119 |
  | Sending data         | 0.000038 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |     0 | exec                  | sql_executor.cc      |         195 |
  | end                  | 0.000004 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |     0 | handle_query          | sql_select.cc        |         199 |
  | query end            | 0.000005 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |     0 | mysql_execute_command | sql_parse.cc         |        4968 |
  | closing tables       | 0.000007 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |     0 | mysql_execute_command | sql_parse.cc         |        5020 |
  | freeing items        | 0.000011 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |     0 | mysql_parse           | sql_parse.cc         |        5596 |
  | cleaning up          | 0.000013 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |     0 | dispatch_command      | sql_parse.cc         |        1902 |
  +----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+-------+-----------------------+----------------------+-------------+
  15 rows in set, 1 warning (0.00 sec)
  mysql>  select * from employees.employees where emp_no=10001;
  +--------+------------+------------+-----------+--------+------------+
  | emp_no | birth_date | first_name | last_name | gender | hire_date  |
  +--------+------------+------------+-----------+--------+------------+

  |  10001 | 1953-09-02 | Georgi     |>  +--------+------------+------------+-----------+--------+------------+
  1 row in set (0.00 sec)
  mysql> show profile cpu, block io, memory,swaps,context switches,source for query 2;
  +---------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+-------+------------------+--------------+-------------+
  | Status        | Duration | CPU_user | CPU_system | Context_voluntary | Context_involuntary | Block_ops_in | Block_ops_out | Swaps | Source_function  | Source_file  | Source_line |
  +---------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+-------+------------------+--------------+-------------+
  | starting      | 0.000083 | 0.000000 |   0.001000 |                 0 |                   0 |            0 |             0 |     0 | NULL             | NULL         |        NULL |
  | freeing items | 0.000016 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |     0 | mysql_parse      | sql_parse.cc |        5596 |
  | cleaning up   | 0.000005 | 0.000000 |   0.000000 |                 0 |                   0 |            0 |             0 |     0 | dispatch_command | sql_parse.cc |        1902 |
  +---------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+-------+------------------+--------------+-------------+
  3 rows in set, 1 warning (0.00 sec)
  mysql> show profile for query 2;
  +---------------+----------+
  | Status        | Duration |
  +---------------+----------+
  | starting      | 0.000083 |
  | freeing items | 0.000016 |
  | cleaning up   | 0.000005 |
  +---------------+----------+
  3 rows in set, 1 warning (0.01 sec)
  mysql>  set profiling=0 ;
  Query OK, 0 rows affected, 1 warning (0.00 sec)
  方法二:timestampdiff来查看执行时间。
  mysql>
  set @d=now();
  select * from employees.employees where emp_no=10001;
  select timestampdiff(second,@d,now());
  --回车
  +--------------------------------+
  | timestampdiff(second,@d,now()) |
  +--------------------------------+
  |                             21 |
  +--------------------------------+
  1 row in set (0.00 sec)
  参考:http://blog.csdn.net/mydriverc2/article/details/53837435


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-613601-1-1.html 上篇帖子: XtraBackup物理备份MySQL的流程 下篇帖子: MYSQL SQL模式 (未完成)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表