一、使用show profile
该命令是在MySQL5.1之后版本引入,默认是禁用的,需手动开启,通过服务器变量在会话级别动态修改,修改语句如下:
mysql> select @@profiling;
mysql> SET profiling = 1;
使profile选项生效
mysql> show profiles;
之后在服务器上执行的所有语句都会测量消耗时间和其他一些查询执行状态变更相关数据,语句执行期间解析服务器具体工作。
例如查询一条语句:
SELECT * FROM activity_pay_spread_record;
输出是按照执行顺序排序,无法使用orderby语句。假如不使用show profiling命令,直接查询information_schema中对应的表,则可以按照需要格式输出
SELECT STATE, SUM(DURATION) AS Total_R,
ROUND(
100*SUM(DURATION) /
(SELECT SUM(DURATION)
FROM information_schema.PROFILING
WHERE QUERY_ID = @query_id),2
) AS Pct_R,
COUNT(*) AS Calls,
SUM(DURATION)/COUNT(*) AS "R/Call"
FROM information_schema.profiling
WHERE QUERY_ID = @query_id
GROUP BY STATE
ORDER BY Total_R DESC;
二、使用show status
mysql> flush status;
mysql> SELECT * FROM activity_pay_spread_record;
mysql> show status where Variable_name LIKE 'Handler%'
or Variable_name LIKE 'Created%';