mysql优化-其他分析方法

SQL排查-慢查询日志:

MySQL提供的一种日志记录,用于记录MySQL种响应时间超过阀值的SQL语句(long_query_time,默认10秒)
慢查询日志默认是关闭的;建议:开发调优是打开,而最终部署时关闭。

检查是否开启了慢查询日志:showvariableslike'%slow_query_log%';

临时开启:
setglobalslow_query_log=1;--在内存种开启
exit
servicemysqlrestart

永久开启:
/etc/my.cnf中追加配置:
vi/etc/my.cnf
[mysqld]
slow_query_log=1
slow_query_log_file=/var/lib/mysql/localhost-slow.log

慢查询阀值:
showvariableslike'%long_query_time%';

临时设置阀值:
setgloballong_query_time=5;--设置完毕后,重新登陆后起效(不需要重启服务)

永久设置阀值:

/etc/my.cnf中追加配置:
vi/etc/my.cnf
[mysqld]
long_query_time=3

selectsleep(4);
selectsleep(5);
selectsleep(3);
selectsleep(3);
--查询超过阀值的SQL:showglobalstatuslike'%slow_queries%';

(1)慢查询的sql被记录在了日志中,因此可以通过日志查看具体的慢SQL。
cat/var/lib/mysql/localhost-slow.log

(2)通过mysqldumpslow工具查看慢SQL,可以通过一些过滤条件快速查找出需要定位的慢SQL
mysqldumpslow--help
s:排序方式
r:逆序
l:锁定时间
g:正则匹配模式

--获取返回记录最多的3个SQL
mysqldumpslow-sr-t3/var/lib/mysql/localhost-slow.log

--获取访问次数最多的3个SQL
mysqldumpslow-sc-t3/var/lib/mysql/localhost-slow.log

--按照时间排序,前10条包含leftjoin查询语句的SQL
mysqldumpslow-st-t10-g"leftjoin"/var/lib/mysql/localhost-slow.log

语法:
mysqldumpslow各种参数慢查询日志的文件

分析海量数据

a.模拟海量数据存储过程(无return)/存储函数(有return)
createdatabasetestdata;
usetestdata
createtabledept
(
dnoint(5)primarykeydefault0,
dnamevarchar(20)notnulldefault'',
locvarchar(30)default''
)engine=innodbdefaultcharset=utf8;

createtableemp
(
eidint(5)primarykey,
enamevarchar(20)notnulldefault'',
jobvarchar(20)notnulldefault'',
deptnoint(5)notnulldefault0
)engine=innodbdefaultcharset=utf8;
通过存储函数插入海量数据:
创建存储函数:
randstring(6)->aXiayx用于模拟员工名称

delimiter$
createfunctionrandstring(nint)returnsvarchar(255)
begin
declareall_strvarchar(100)default'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
declarereturn_strvarchar(255)default'';
declareiintdefault0;
whilei do
setreturn_str=concat(return_str,substring(all_str,FLOOR(1+rand()*52),1));
seti=i+1;
endwhile;
returnreturn_str;

end$

--如果报错:YouhaveanerrorinyourSQLsyntax,说明SQL语句语法有错,需要修改SQL语句;

如果报错ThisfunctionhasnoneofDETERMINISTIC,NOSQL,orREADSSQLDATAinitsdeclarationandbinaryloggingisenabled(youmightwanttousethelesssafelog_bin_trust_function_creatorsvariable)
是因为存储过程/存储函数在创建时与之前的开启慢查询日志冲突了
解决冲突:
临时解决(开启log_bin_trust_function_creators)
showvariableslike'%log_bin_trust_function_creators%';
setgloballog_bin_trust_function_creators=1;
永久解决:
/etc/my.cnf
[mysqld]
log_bin_trust_function_creators=1

--产生随机整数
createfunctionran_num()returnsint(5)
begin
declareiintdefault0;
seti=floor(rand()*100);
returni;

end$

--通过存储过程插入海量数据:emp表中,10000,100000
createprocedureinsert_emp(ineid_startint(10),indata_timesint(10))
begin
declareiintdefault0;
setautocommit=0;

repeat

insertintoempvalues(eid_start+i,randstring(5),'other',ran_num());
seti=i+1;
untili=data_times
endrepeat;
commit;
end$

--通过存储过程插入海量数据:dept表中
createprocedureinsert_dept(indno_startint(10),indata_timesint(10))
begin
declareiintdefault0;
setautocommit=0;
repeat

insertintodeptvalues(dno_start+i,randstring(6),randstring(8));
seti=i+1;
untili=data_times
endrepeat;
commit;

end$

--插入数据
delimiter;
callinsert_emp(1000,800000);
callinsert_dept(10,30);

b.分析海量数据:
(1)profiles
showprofiles;--默认关闭
showvariableslike'%profiling%';
setprofiling=on;
showprofiles:会记录所有profiling打开之后的全部SQL查询语句所花费的时间。缺点:不够精确,只能看到总共消费的时间,不能看到各个硬件消费的时间(cpuio)

(2)--精确分析:sql诊断
showprofileallforquery上一步查询的的Query_Id
showprofilecpu,blockioforquery上一步查询的的Query_Id

(3)全局查询日志:记录开启之后的全部SQL语句。(这次全局的记录操作仅仅在调优、开发过程中打开即可,在最终的部署实施时一定关闭)
showvariableslike'%general_log%';

--执行的所有SQL记录在表中
setglobalgeneral_log=1;--开启全局日志
setgloballog_output='table';--设置将全部的SQL记录在表中

--执行的所有SQL记录在文件中
setgloballog_output='file';
setglobalgeneral_log=on;
setglobalgeneral_log_file='/tmp/general.log';

开启后,会记录所有SQL:会被记录mysql.general_log表中。
select*frommysql.general_log;

你可能感兴趣的:(mysql优化-其他分析方法)