mysql学习记录
1.mysql查询慢的原因:
(1)查询语句写的烂
(2)索引失效:单值、复合
(3)关联查询太多join(设计缺陷或不得已的需求)
(4)服务器调优及各个参数设置(缓冲、线程数)
2.mysql join:
3.索引是排好序的用于快速查找数据的数据结构。
4.索引优势:
索引劣势:
5.需要建立索引的场景:
不需要建立索引的场景:
l
6.性能分析
(1)常见瓶颈
(2)explain命令
解释mysql怎么执行的
使用:explain + sql语句
explain 表头:
(1)id 查询的序列号
id相同,从上往下依次执行;
如果是子查询,id的序号递增,id越大越先被执行;
id有相同有不同:大的先执行,相同的话从前到后依次执行
(2)select type:simple、primary、subquery、derived(衍生)、union、union result
(3)table:表明数据是属于哪一个表的
(4)type:all、index、range、ref、eq_ref、const,system、null
好到差:system>const>eq_ref>ref>range>index>all
至少到range,最好到ref
7. 分析mysql
(1)观察,至少跑一天,看看生产的慢sql情况。
(2)开启慢查询日志,设置阈值,比如超过5秒就是慢sql,抓取出来。
(3)explain + 慢sql分析
(4)show profile
(5)运维经理 or DBA,进行SQL数据库服务器的参数调优。
8.B表更小,in更加优:
select * from A where id in ( select id from B)
A表更小,exit更加优:
select * from A where exist ( select 1 from B where B.id = A.id)