mysql中profile的使用方法教程

作者:水木清华_f221 时间:2024-01-16 02:28:52 

profile是什么

当我们要对某一条sql的性能进行分析时,可以使用它。

Profiling是从 mysql5.0.3版本以后才开放的。

启动profile之后,所有查询包括错误的语句都会记录在内。

关闭会话或者set profiling=0 就关闭了。(如果将profiling_history_size参数设置为0,同样具有关闭MySQL的profiling效果。)

此工具可用来查询SQL执行状态,System lock和Table lock 花多少时间等等,

对定位一条语句的I/O消耗和CPU消耗 非常重要。(SQL 语句执行所消耗的最大两部分资源就是IO和CPU)

--在mysql5.7之后,profile信息将逐渐被废弃,mysql推荐使用performance schema

mysql官网定义

The SHOW PROFILE and SHOW PROFILES statements display profiling information that indicates resource usage for statements executed during the course of the current session.

简单的说,当前会话资源的消耗情况。

注意:show profile和show Profiles都是不建议使用的,在mysql后期的版本中可能会被删除;官网建议使用Performance Schema

怎么使用

profile默认关闭,生产环境中也建议关闭。

查看当前环境的profile设置


mysql> show variables like '%profiling%';
+------------------------+-------+
| Variable_name   | Value |
+------------------------+-------+
| have_profiling   | YES |
| profiling    | OFF |
| profiling_history_size | 15 |
+------------------------+-------+

profiling off表示profile关闭,profiling_history_size 15表示保存最近15条SQL的资源消耗情况。

开启profile功能,可以使用命令


set global profiling = 1;

然后就可以使用下面命令


show profiles;

查看最近15条SQL的情况;

如果要查看某一条的具体情况,SQL格式为:


SHOW PROFILE [type [, type] ... ]
[FOR QUERY n]
[LIMIT row_count [OFFSET offset]]

type: {
ALL
| BLOCK IO
| CONTEXT SWITCHES
| CPU
| IPC
| MEMORY
| PAGE FAULTS
| SOURCE
| SWAPS
}

官网对type中各个字段的解释为:

    ALL displays all information

    BLOCK IO displays counts for block input and output operations

    CONTEXT SWITCHES displays counts for voluntary and involuntary context switches

    CPU displays user and system CPU usage times

    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

profiling 对每个会话有效,当会话结束后,当前的profiling信息就会丢失。

使用案例


mysql> show profiles;
+----------+------------+----------------------------+
| Query_ID | Duration | Query      |
+----------+------------+----------------------------+
|  1 | 0.00060275 | select * from customers |
|  2 | 0.00222450 | show tables    |
|  3 | 0.00567425 | select * from offices  |
|  4 | 0.00052050 | show tables    |
|  5 | 0.01123300 | select * from payments  |
|  6 | 0.00111675 | show tables    |
|  7 | 0.02049625 | select * from productlines |
+----------+------------+----------------------------+

在排查SQL执行情况,或者是哪条SQL执行非常慢,慢在哪里;profile都是非常的辅助工具。

显示一条SQL的具体花销在哪里


mysql> show profile for query 7;
+----------------------+----------+
| Status    | Duration |
+----------------------+----------+
| starting    | 0.000043 |
| checking permissions | 0.000005 |
| Opening tables  | 0.014552 |
| init     | 0.000025 |
| System lock   | 0.000009 |
| optimizing   | 0.000004 |
| statistics   | 0.000011 |
| preparing   | 0.000010 |
| executing   | 0.000003 |
| Sending data   | 0.005653 |
| end     | 0.000010 |
| query end   | 0.000009 |
| closing tables  | 0.000020 |
| freeing items  | 0.000121 |
| cleaning up   | 0.000023 |
+----------------------+----------+

信息一目了然,这样我就能对SQL执行情况有个大概的了解。

来源:https://www.jianshu.com/p/8f12163337e8

标签:mysql,profile,命令
0
投稿

猜你喜欢

  • 如何利用Pandas删除某列指定值所在的行

    2023-10-29 11:49:39
  • Python实现统计图像连通域的示例详解

    2022-12-05 09:13:05
  • Python中三种条件语句示例介绍

    2022-09-17 17:28:30
  • Micropython固件使用Pico刷固件并配置VsCode开发环境的方法

    2022-09-21 16:22:14
  • opencv3/C++实现视频读取、视频写入

    2021-12-17 10:01:16
  • 实例:arguments.callee的应用

    2009-04-20 12:38:00
  • Flash如何连接Mysql

    2010-11-11 11:57:00
  • SaaS中的用户体验设计

    2009-05-20 12:28:00
  • DW MX新功能试用:嵌套模板

    2008-02-03 11:35:00
  • 用MSXML2.ServerXMLHTTP的setTimeouts属性解决并死问题

    2010-03-02 20:21:00
  • Python的进制转换和ASCLL转换你了解吗

    2021-09-18 05:33:59
  • python实现单线程多任务非阻塞TCP服务端

    2021-05-29 07:26:52
  • 列出SQL Server中具有默认值的所有字段的语句

    2024-01-16 20:17:27
  • 教你解决往mysql数据库中存入汉字报错的方法

    2024-01-15 02:32:06
  • Vue.js自定义事件的表单输入组件方法

    2024-05-05 09:12:24
  • 五个影响mysql性能的关键配置

    2010-08-08 09:13:00
  • 跟老齐学Python之关于循环的小伎俩

    2022-07-20 07:03:36
  • python肯德尔系数相关性数据分析示例

    2023-02-20 21:42:35
  • JavaScript实现网页计算器功能

    2024-04-17 10:03:41
  • 教你如何利用python3爬虫爬取漫画岛-非人哉漫画

    2021-10-01 23:06:57
  • asp之家 网络编程 m.aspxhome.com