1.打开masql慢查询记录:
show variables like '%query%'; -- 可以用这个查询所有的变量
set global long_query_time=2; -- 设置超过2秒的查询为慢查询
set global slow_query_log='ON';-- 打开慢日志记录
slow_query_log_file :慢查询的sql语句存放的位置,一般在mysql的data目录下
2.慢查询日志分析工具:mysqldumpslow 这里为.pl文件需要借助 ActivePerl 软件打开
ActivePerl 不要去官网下很可能下载不下来,去腾讯管家的软件中心去下载。
3.在mysql的bin目录下进入powershell (按住shift右键)输入命令
perl mysqldumpslow.pl -s t -t 5 D:\\softwareInstall\MySQL\Data\DESKTOP-OEUGGEP-slow.log 查询所有慢查询的数据按照时间排序取出前五行
s 排序
C 总次数
t 总时间
l 锁的时间
r 总数据行
at ,al,ar :t,l,r 平均数 如: at=总时间/总次数
-t top 指定取前面作为输出结果
-v verbose
-d debug
-s ORDER what to sort by (al, at, ar, c, l, r, t), 'at' is default
al: average lock time
ar: average rows sent
at: average query time
c: count
l: lock time
r: rows sent
t: query time
-r reverse the sort order (largest last instead of first)
-t NUM just show the top n queries
-a don't abstract all numbers to N and strings to 'S'
-n NUM abstract numbers with at least n digits within names
-g PATTERN grep: only consider stmts that include this string
-h HOSTNAME hostname of db server for *-slow.log filename (can be wildcard),
default is '*', i.e. match all
-i NAME name of server instance (if using mysql.server startup script)
-l don't subtract lock time from total time
1. -s,排序,c,t,l,r以及ac,at,al,ar分别是按照query次数,时间,lock时间,返回记录排序。加a就是倒序。
2. -t,top n,跟上数字就是算出top多少条
3. -g,跟正则表达式。
查看sql 的执行情况:
show profiles 查询sql 执行的ID
show profile cpu,block io for QUERY 175 查询对应sql ID的cpu、io执行情况
如果出现下列情况表示需要优化
开启全局查询日志:
1、记录到表中
set global general_log =1 //开启全局日志,开启后会记录所有sql
set global log_output='table' //记录sql到系统表general_log中
select * from mysql.general_log
2、记录到本地日志文件中
set global general_log =1
set global general_log_file="C:\\Users\\pu\\Desktop\\general.log"
set global log_output='file'