definition
- Show Profiles is provided by MySQL, which can analyze the resource consumption of SQL statement execution and can be used for SQL tuning.
- SQL profiling is enabled by configuring the profiling parameter, which can be set globally and at the session level.
- The global level affects the entire MySQL instance, while the session level only affects the current session.
- After this parameter is turned on, the subsequent execution of SQL statements will record its resource overhead, such as IO, context switching, CPU, Memory, etc.
- Show profiles were added after 5.0.37. To use this feature, make sure that the MySQL version is> 5.0.37.
how to use
select version(); -- 查看数据库版本
show variables like "%profiling%"; -- 查看是否开启
set profiling=1; -- 开启profile
show profiles; -- 查看最近的SQL语句
Help document
help profile; --查看帮助信息
SHOW PROFILE [type [, type] ... ]
[FOR QUERY n]
[LIMIT row_count [OFFSET offset]]
type: {
ALL --显示所有的开销信息
| BLOCK IO --显示块IO相关开销
| CONTEXT SWITCHES --上下文切换相关开销
| CPU --显示CPU相关开销信息
| IPC --显示发送和接收相关开销信息
| MEMORY --显示内存相关开销信息
| PAGE FAULTS --显示页面错误相关开销信息
| SOURCE --显示和Source_function,Source_file,Source_line相关的开销信息
| SWAPS --显示交换次数相关开销的信息
}
*Note*:
The SHOW PROFILE and SHOW PROFILES statements are deprecated and will
be removed in a future MySQL release. Use the Performance Schema
instead; see
https://dev.mysql.com/doc/refman/8.0/en/performance-schema-query-profil
ing.html
--上面描述SHOW PROFILE命令将会被移除,推荐用Performance Schema instead代替
--在Oracle数据库中,是通过autotrace来剖析单条SQL并获取真实的执行计划以及其开销信息
URL: https://dev.mysql.com/doc/refman/8.0/en/show-profile.html
Official website
Performance Schema description document: https://dev.mysql.com/doc/refman/8.0/en/performance-schema-query-profiling.html
SHOW PROFILE description document: https://dev.mysql.com/doc/refman /8.0/en/show-profile.html
show profile for query 167; -- 获取指定查询(Query_ID = 167)的开销
-- 查看特定部分的开销,如下为CPU部分的开销
show profile cpu for query 167 ;
-- 如下为MEMORY部分的开销
show profile memory for query 167 ;
-- 同时查看io和cpu
show profile block io,cpu for query 167;
-- 下面的SQL语句用于查询query_id为2的SQL开销,且按最大耗用时间倒序排列
set @query_id=167;
If the following conditions occur in Status, SQL optimization is required
Supplement (open global query log)
- Never
生产环境
turn on this feature on - Never
生产环境
turn on this feature on - Never
生产环境
turn on this feature on
Please 测试环境
open on