前景:面试被问到了慢查询,感觉自己似懂非懂吧,找了很多文章看看,总结下前人的知识精华,自己消化下
总结:
1、是不是慢查询的决定条件:sql执行时间 > long_query_time配置
2、慢查询原因:
(1)索引的过滤性好不好
参考文章:https://mp.weixin.qq.com/s/uE3kAKK8hLnbchxWcqb5IQ
(2)sql语句本身问题
where语句使用函数
group by 语句不要排序order by null
limit分页查询的offset在where中用主键索引的值
参考文章:https://mp.weixin.qq.com/s/UviAY3tKj8BYkuh8ZPZq2g
https://mp.weixin.qq.com/s/lAezwktI_lQngYO6I5Ez8A
3、查看慢查询日志和sql执行时间
sql执行时间查看:使用属性profiles
属性开关:set profiling = 1;
参考文章:https://mp.weixin.qq.com/s/lLJIodj0qrH5XHT8kcPuFg
4、分析慢查询日志工具
https://mp.weixin.qq.com/s/_SWewX-8nFam20Wcg6No1Q