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

[经验分享] mysql profile使用

[复制链接]

尚未签到

发表于 2018-9-28 11:33:49 | 显示全部楼层 |阅读模式
  转载自:http://hi.baidu.com/%C2%ED%B3%A4%D5%F72008/blog/item/3f8ec3fd9afe348fb801a0c5.html
  mysql的sql语句优化都使用explain,但是这个没有办法知道详细的Memory/CPU等使用量
  MySQL Query Profiler, 可以查询到此 SQL 语句会执行多少, 并看出 CPU/Memory 使用
  量, 执行过程 System lock, Table lock 花多少时间等等.
  mysql> show variables like 'profiling%';
  +------------------------+-------+
  | Variable_name           | Value |
  +------------------------+-------+
  | profiling               | OFF    |
  | profiling_history_size | 15     |
  +------------------------+-------+
  开启此功能
  mysql>set profiling=1;
  mysql> show variables like 'profiling%';
  # 此命令会让mysql在 information_schema 的 database 建立一個 PROFILING 的
  table 来记录.
  +------------------------+-------+
  | Variable_name           | Value |
  +------------------------+-------+
  | profiling               | ON     |
  | profiling_history_size | 15     |
  +------------------------+-------+
  profiling_history_size记录多少次查询
  mysql> show profiles;
  +----------+------------+------------------------------------+
  | Query_ID | Duration    | Query                               |
  +----------+------------+------------------------------------+
  |         1 | 0.00018100 | show variables like 'profiling%'    |
  |         2 | 0.00020400 | show variables like 'profiling%'    |
  |         3 | 0.00007800 | set profiling=1                     |
  |         4 | 0.00011000 | show variables like 'profiling%'    |
  |         5 | 0.00002400 | select count(1) from `mrhao_stats` |
  |         6 | 1.52181400 | select count(*) from `mrhao_stats` |
  |         7 | 0.00026900 | show variables like 'profiling%'    |
  mysql> show profile for query 6;
  +--------------------------------+----------+
  | Status                          | Duration |
  +--------------------------------+----------+
  | (initialization)                | 0.000003 |
  | checking query cache for query | 0.000042 |
  | Opening tables                  | 0.00001 |
  | System lock                     | 0.000004 |
  | Table lock                      | 0.000025 |
  | init                            | 0.000009 |
  | optimizing                      | 0.000003 |
  | statistics                      | 0.000007 |
  | preparing                       | 0.000007 |
  | executing                       | 0.000004 |
  | Sending data                    | 1.521676 |
  | end                             | 0.000007 |
  | query end                       | 0.000003 |
  | storing result in query cache | 0.000002 |
  | freeing items                   | 0.000006 |
  | closing tables                  | 0.000004 |
  | logging slow query              | 0.000002 |
  +--------------------------------+----------+
  17 rows in set (0.00 sec)
  mysql> show profile cpu for query 6;
  +--------------------------------+----------+----------+------------+
  | Status                          | Duration | CPU_user | CPU_system |
  +--------------------------------+----------+----------+------------+
  | (initialization)                | 0.000003 | 0         | 0           |
  | checking query cache for query | 0.000042 | 0.001     | 0           |
  | Opening tables                  | 0.00001 | 0         | 0           |
  | System lock                     | 0.000004 | 0         | 0           |
  | Table lock                      | 0.000025 | 0         | 0           |
  | init                            | 0.000009 | 0         | 0           |
  | optimizing                      | 0.000003 | 0         | 0           |
  | statistics                      | 0.000007 | 0         | 0           |
  | preparing                       | 0.000007 | 0         | 0           |
  | executing                       | 0.000004 | 0         | 0           |
  | Sending data                    | 1.521676 | 1.631752 | 0.036995    |
  | end                             | 0.000007 | 0         | 0           |
  | query end                       | 0.000003 | 0         | 0           |
  | storing result in query cache | 0.000002 | 0         | 0           |
  | freeing items                   | 0.000006 | 0         | 0           |
  | closing tables                  | 0.000004 | 0         | 0           |
  | logging slow query              | 0.000002 | 0         | 0           |
  +--------------------------------+----------+----------+------------+
  17 rows in set (0.00 sec)
  * ALL - displays all information
  * BLOCK IO - displays counts for block input and output operations
  * CONTEXT SWITCHES - displays counts for voluntary and involuntary
  context switches
  * IPC - displays counts for messages sent and received
  * MEMORY - is not currently implemented
  * PAGE FAULTS - displays counts for major and minor page faults
  * SOURCE - displays the names of functions from the source code, together
  with the name and line number of the file in which the function occurs
  * SWAPS - displays swap counts
  查询时间跟cpu的使用
  mysql> select min(seq) seq,state,count(*) numb_ops,
  round(sum(duration),5) sum_dur, round(avg(duration),5) avg_dur,
  round(sum(cpu_user),5) sum_cpu, round(avg(cpu_user),5) avg_cpu
  from information_schema.profiling
  where query_id = 7
  group by state
  order by seq;
  关闭此功能
  mysql> set profiling=0;
  mysql> show variables like 'profiling%';
  +------------------------+-------+
  | Variable_name           | Value |
  +------------------------+-------+
  | profiling               | OFF    |
  | profiling_history_size | 15     |
  +------------------------+-------+


运维网声明 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-603232-1-1.html 上篇帖子: MySQL Split String Function 下篇帖子: mysql除法精度
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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