ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] # show profile 查看`profiling`系统变量 ~~~ruby mysql> show variables like '%profil%'; +------------------------+-------+ | Variable_name | Value | +------------------------+-------+ | have_profiling | YES | | profiling | OFF | | profiling_history_size | 15 | +------------------------+-------+ ~~~ * have\_profiling:当前版本是否支持`profiling`功能 * profiling:是否开启`profiling`功能 * profiling\_history\_size:保留profiling的数目,默认是15,范围为0~100,为0时代表禁用profiling 开启profiling需要设置profiling变量为1,该变量的默认值是0 ~~~mysql mysql> SET profiling = 1; ~~~ 然后运行sql,查看下 ~~~ mysql> select sleep(3); +----------+ | sleep(3) | +----------+ | 0 | +----------+ 1 row in set (3.00 sec) mysql> show profiles; +----------+------------+-----------------+ | Query_ID | Duration | Query | +----------+------------+-----------------+ | 1 | 3.00183550 | select sleep(3) | +----------+------------+-----------------+ 1 row in set, 1 warning (0.00 sec) ~~~ 1. Query\_ID 表示执行SQL的唯一标识。 2. Duration 表示持续时间,默认单位为秒。 3. Query 就是我们所执行的SQL语句。 --- 1. show profiles 语句 默认显示的是服务端接收到的最新的15条语句。 我们可以通过以下语句进行修改默认值: set profiling\_history\_size =20; profiling\_history\_size最大取值取值范围为\[0,100\]。 2. 当超过100时,则会设置自动设置为最大值100。 3. 当小于0时,则会自动设置最小值为0。 4. 当其等于0时,其效果等同于`set profiling=0`,关闭性能分析模式。 ## 查sql 可以根据查询的id去查询 可以显示更详细的,指定资源查询 ~~~ 语法:Show profile type , block type for query 问题sql数字号码 ~~~ 其中type: * ALL: 显示所有的开销信息 * BLOCK IO : 显示块IO相关开销 * CONTEXT SWITCHS: 上下文切换相关开销 * CPU : 显示cpu 相关开销 * IPC: 显示发送和接收相关开销 * MEMORY: 显示内存相关开销 * PAGE FAULTS:显示页面错误相关开销信息 * SOURCE : 显示和Source\_function ,Source\_file,Source\_line 相关的开销信息 * SWAPS:显示交换次数相关的开销信息 * Status : sql 语句执行的状态 * Duration: sql 执行过程中每一个步骤的耗时 * CPU\_user: 当前用户占有的cpu * CPU\_system: 系统占有的cpu * Block\_ops\_in : I/O 输入 * Block\_ops\_out : I/O 输出 ~~~ show profile block io,cpu for query 1; show profile cpu,block io,memory,swaps for query 1; show profile cpu,block io,memory,swaps,context switches,source for query 1; ~~~ ~~~ mysql> show profile for query 1; +----------------------+----------+ | Status | Duration | +----------------------+----------+ | starting | 0.000055 | | checking permissions | 0.000005 | | Opening tables | 0.000004 | | init | 0.000008 | | optimizing | 0.000004 | | executing | 0.000006 | | User sleep | 3.001711 | | end | 0.000010 | | query end | 0.000004 | | closing tables | 0.000003 | | freeing items | 0.000019 | | cleaning up | 0.000008 | +----------------------+----------+ 12 rows in set, 1 warning (0.00 sec) mysql> show profile block io,cpu for query 1; +----------------------+----------+----------+------------+--------------+---------------+ | Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out | +----------------------+----------+----------+------------+--------------+---------------+ | starting | 0.000055 | 0.000013 | 0.000035 | 0 | 0 | | checking permissions | 0.000005 | 0.000001 | 0.000004 | 0 | 0 | | Opening tables | 0.000004 | 0.000001 | 0.000002 | 0 | 0 | | init | 0.000008 | 0.000002 | 0.000006 | 0 | 0 | | optimizing | 0.000004 | 0.000001 | 0.000002 | 0 | 0 | | executing | 0.000006 | 0.000001 | 0.000005 | 0 | 0 | | User sleep | 3.001711 | 0.000126 | 0.000343 | 0 | 0 | | end | 0.000010 | 0.000002 | 0.000005 | 0 | 0 | | query end | 0.000004 | 0.000001 | 0.000003 | 0 | 0 | | closing tables | 0.000003 | 0.000001 | 0.000002 | 0 | 0 | | freeing items | 0.000019 | 0.000005 | 0.000014 | 0 | 0 | | cleaning up | 0.000008 | 0.000002 | 0.000006 | 0 | 0 | +----------------------+----------+----------+------------+--------------+---------------+ 12 rows in set, 1 warning (0.00 sec) ~~~ ## 各个字段含义 ~~~ show profile for query 1; ~~~ 1. all: 展示所有信息。 2. block io: 展示io的输入输出信息。 3. context switches: 展示线程的上线文切换信息。 4. cpu :显示SQL 占用的CPU信息。 5. ipc: 显示统计消息的发送与接收计数信息。 6. page faults:显示主要与次要的页面错误。 7. memory:本意是显示内存信息,但目前还未实现。 8. swaps: 显示交换次数。 9. sources:显示源代码中的函数名称,以及函数发生的文件的名称和行。 --- **一条query每个阶段的资源开销可以从`information_schema.profiling`表查询** * state : 当前query所在的阶段 * CPU_user : CPU用户 * CPU_system : CPU系统 * Context_voluntary : 上下文主动切换 * Context_involuntary : 上下文被动切换 * Block_ops_in : 阻塞的输入操作 * Block_ops_out : 阻塞的输出操作 * Messages_sent : 消息发出 * Messages_received : 消息接受 * Page_faults_major : 主分页错误 * Page_faults_minor : 次分页错误 * Swaps : 交换次数 * Source_function : 源功能 * Source_file : 源文件 * Source_line : 源代码行 --- 纵向(可以看到sql完整的生命周期): optimizing: 智能优化器 sending data: 发送数据 mysql文档中字段含义: [https://dev.mysql.com/doc/refman/5.7/en/general-thread-states.html](https://dev.mysql.com/doc/refman/5.7/en/general-thread-states.html) ## 日常开发需要注意的 里面查出是这个要注意了 * converting HEAP to MyISAM : 查询结果太大,内存都不够用了,往磁盘上搬了; * creating tmp table :创建临时表,拷贝数据到临时表,然后再删除; * copying to tmp table on disk :把内存中临时表复制到磁盘,危险!!! * locked 注:以上四个中若出现一个或多个,表示sql 语句 必须优化。