查看慢配置查询的
show variables like '%query%' ; show variables like 'long_query_time' ; //可以显示当前慢查询时间 set long_query_time=1 ; //慢查询的时间默认为10秒 可以修改慢查询时间也可mysql5.7 可以直接在配置文件my.ini 中写配置
log_slow_admin_statements = ON log_slow_slave_statements = ON slow_query_log = ON //开启慢查询 slow_query_log_file = E:\mysql-5.7.13-winx64\data\pc-PC-slow.log //设置路径 long_query_time =1 //设置慢查询时间 超过一秒的记录 /**end***/重启 mysql service mysql restart /etc/init.d/mysql restart systemctl restart mysqld 以在my.ini种配置
慢查询日志文件的信息格式:
Time: 130905 14:15:59 时间是2013年9月5日 14:15:59(前面部分容易看错哦,乍看以为是时间戳) User@Host: root[root] @ [183.239.28.174] 请求mysql服务器的客户端ip Query_time: 0.735883 Lock_time: 0.000078 Rows_sent: 262 Rows_examined: 262 这里表示执行用时多少秒,0.735883秒,1秒等于1000毫秒 Lock_time 锁的时间
SET timestamp=1378361759; 执行sql的准确时间 时间戳 show tables from test_db; 指明了当时执行的是这条语句
慢查询命令 set global slow_query_log=ON; 打开慢查询 set global slow_query_log=OFF; 关闭慢查询 set global slow_launch_time=5; 设置慢查询时间 show variables like “%slow%”; 查看配置 mysql 5.1.6版本起,slow_query_log 和 slow_launch_time 支持写文件或写数据库表两种方式,并且日志的开启,输出方式的修改,都可以在global级别动态修改。 只需简单通过set global slow_query_log=ON;即可开启慢查询,而不需要重启数据库!
/********************************************************/
Show variables like ‘profiling’; 查看sql语句 性能分析 看profile是否开启 5.7的 mysql> show variables like ‘%profiling%’; +————————+——-+ | Variable_name | Value | +————————+——-+ | have_profiling | YES | | profiling | ON | | profiling_history_size | 15 | +————————+——-+ set profiling=on; 开启profile 查看执行过的sql 的执行时间 show profiles;
mysql> show profiles; +———-+————+——————————————————————————-+ | Query_ID | Duration | Query | +———-+————+——————————————————————————-+ | 1 | 0.00081975 | explain select * from t_log_login where id=’46702e55f23911e49d5cac162d8aadd4’ | | 2 | 0.00436950 | select * from t_log_login where id=’46702e55f23911e49d5cac162d8aadd4’ | +———-+————+——————————————————————————-+ 查询所花费的时间 query_id=1 的语句执行时间
select sum(duration) from information_schema.profiling where query_id=1; # Query ID = 1
查看 # Query ID = 1的执行时间 mysql> show profile for query 1;