information_schema.profiling可以用来分析每一条SQL在它执行的各个阶段的用时,注意这个表是session 级的,也就是说如果session1 开启了它;session2没有开启
这个情况下session2 去查询只会返回一个空表
1、set @@ssesion.profiling=1 可以开启information_schema.profiling相关监测
2、information_schema.profiling 表的常用列
1、query_id :查询id 用于标记不同的查询
2、seq :一个查询内部执行的步骤
3、state :各个步骤的状态
4、duration :各个步骤持续的时间
5、cpu_user :用户空间的cpu 使用量
6、cpu_system :内核空间的cpu 使用量
7、swaps :swap 发生的次数
3、例子:
1、开启监测
set @@ssesion.profiling=1;
2、执行查询
select * from tempdb.t;
3、关闭监测
set @@ssesion.profiling=0;
4、查询监测到的数据
select -> query_id, -- 查询id 它用于标识一个查询 -> seq, -- 显示序号 -> (select sum(duration) from information_schema.profiling as innert where innert.query_id = outert.query_id) as total_cost , -- 总用时in seconds -> state,-- 状态 -> duration, -- 持续时间 -> cpu_user, -- 用户空间的cpu 使用量 -> cpu_system, -- 内核空间的cpu 使用量 -> -- context_voluntary, -- 自愿上下文切换 -> -- context_involuntary, -- 非自愿上下文切换 -> block_ops_in, -- 块调入次数 -> block_ops_out, -- 块调出次数 -> swaps -- 发生swap 的次数 -> from -> information_schema.profiling as outert -> -> order by -> seq; +----------+-----+------------+----------------------+----------+----------+------------+--------------+---------------+-------+ | query_id | seq | total_cost | state | duration | cpu_user | cpu_system | block_ops_in | block_ops_out | swaps | +----------+-----+------------+----------------------+----------+----------+------------+--------------+---------------+-------+ | 1 | 2 | 0.001984 | starting | 0.000058 | 0.000000 | 0.000000 | 0 | 0 | 0 | | 1 | 3 | 0.001984 | checking permissions | 0.000010 | 0.000000 | 0.000000 | 0 | 0 | 0 | | 1 | 4 | 0.001984 | Opening tables | 0.000033 | 0.000000 | 0.000000 | 0 | 0 | 0 | | 1 | 5 | 0.001984 | init | 0.000018 | 0.000000 | 0.000000 | 0 | 0 | 0 | | 1 | 6 | 0.001984 | System lock | 0.000012 | 0.000000 | 0.000000 | 0 | 0 | 0 | | 1 | 7 | 0.001984 | optimizing | 0.000006 | 0.000000 | 0.000000 | 0 | 0 | 0 | | 1 | 8 | 0.001984 | statistics | 0.000014 | 0.000000 | 0.000000 | 0 | 0 | 0 | | 1 | 9 | 0.001984 | preparing | 0.000013 | 0.000000 | 0.000000 | 0 | 0 | 0 | | 1 | 10 | 0.001984 | executing | 0.000003 | 0.000000 | 0.000000 | 0 | 0 | 0 | | 1 | 11 | 0.001984 | Sending data | 0.000130 | 0.000000 | 0.000000 | 0 | 0 | 0 | | 1 | 12 | 0.001984 | end | 0.000009 | 0.000000 | 0.000000 | 0 | 0 | 0 | | 1 | 13 | 0.001984 | query end | 0.000008 | 0.000000 | 0.000000 | 0 | 0 | 0 | | 1 | 14 | 0.001984 | closing tables | 0.001613 | 0.002000 | 0.000000 | 0 | 0 | 0 | | 1 | 15 | 0.001984 | freeing items | 0.000037 | 0.000000 | 0.000000 | 0 | 0 | 0 | | 1 | 16 | 0.001984 | cleaning up | 0.000020 | 0.000000 | 0.000000 | 0 | 0 | 0 | +----------+-----+------------+----------------------+----------+----------+------------+--------------+---------------+-------+
时间: 2024-10-13 05:46:57