mysql tutorialIntroduction to slow query analysis and tuning tools
Recommended (free):mysql tutorial(Video)
Following the explanation of the mysqldumpslow tool in the previous article, Today let’s talk about show profile. It is also the analysis and tuning tool that comes with the MySQL service, but this one is more advanced and closer to the tuning of underlying hardware parameters.
View show profile settings
show variables like 'profiling%';//默认关闭,保存近15次的运行结果
Open
set profiling = on;
View the results of the last 15 runs
show profiles; 备注: show warnings;//可以显示警告和报错的信息
Diagnosis Running SQL
命令:show profile cpu,block io for query query_id; 例子: show profile cpu,block io for query 3; 通过Status一列,可以看到整条SQL的运行过程 1. starting //开始 2. checking permissions //检查权限 3. Opening tables //打开数据表 4. init //初始化 5. System lock //锁机制 6. optimizing //优化器 7. statistics //分析语法树 8. prepareing //预准备 9. executing //引擎执行开始 10. end //引擎执行结束 11. query end //查询结束 ...... 12. closing tables //释放数据表 13. freeing items //释放内存 14. cleaning up //彻底清理
Type: ALL //显示索引的开销信息 BLOCK IO //显示块IO相关开销 CONTEXT SWITCHES //上下文切换相关开销 CPU //显示CPU相关开销信息 IPC //显示发送和接收相关开销信息 MEMORY //显示内存相关开销信息 PAGE FAULTS //显示页面错误相关开销信息 SOURCE //显示和source_function,source_file,source_line相关的开销信息 SWAPS //显示交换次数相关开销的信息
如出现以下一种或者几种情况,说明SQL执行性能极其低下,亟需优化 * converting HEAP to MyISAM //查询结果太大,内存都不够用了往磁盘上搬了 * Creating tmp table //创建临时表:拷贝数据到临时表,用完再删 * Copying to tmp table on disk //把内存中临时表复制到磁盘,危险 * locked //出现死锁
Diagnose SQL by querying the data table (second query method)
select * from information_schema.profiling;
Global query log (the second SQL diagnostic method)
此方式诊断较简单(参数少,适合定位有问题的SQL),记录到数据库(建议只在测试库环境进行)
Settings
方式1:命令行 1. set global general_log = 1; 2. set global log_output = 'TABLE';
方式2:配置文件 * vim my.cnf general_log =1 general_log_file = /path/logfile log_output = FILE * 重启MySQL服务
Diagnosis SQL
select * from mysql.general_log;
The above is the detailed content of mysql slow query analysis and tuning tool show profile. For more information, please follow other related articles on the PHP Chinese website!