索引的作用:
检索 + 排序
索引建立原则:
1: 频繁作为查询条件的
2: 作为外键关联的
3: 经常会更新的字段不适合建立索引
4: 排序字段(排序字段若通过索引去访问,将大大提高排序速度)
5: 查询中 统计/分组字段
explain 之 type
1. sytstem > const > eq_ref > ref > range > index > all
2. 一般来说查询至少要达到range级别
exists & in
原则: 小表驱动大表
1. 当 table A 数据 < table B 数据量 使用 select * from A where id in (select id from B)
2.当 table A 数据 > table B 数据量 使用 select * from A where exists ( select 1 from B where B.id = A.id)

排序 order by

读写锁: 读锁会阻塞写,但不会阻塞读; 而写锁会阻塞读写
1. 看那些表被锁了: show open tables;
2. 分析表锁定: 可以通过检查 table_locks_watied (出现表级锁定争用而发生等待的次数) 和 table_locks_immediate(产生表级锁定的次数)状态变量; SQL : show status like 'table%';
浙公网安备 33010602011771号