锁
目的:解决客户端的并发访问的冲突问题
锁的分类
1、按锁类型分
读锁(select)/共享锁:加读锁后,其他用户只能查询,不能修改
select * from …… lock in share mode
写锁(增删改)/互斥锁、排他锁:其他用户不能做任何操作
select * from .......... for update
2、按锁粒度分
表级锁:加读锁或者写锁
行级锁:读锁或者写锁
MySQL调优
目的:加快数据的检索速度
1、选择合适的存储引擎
2、创建索引:在select 、where、order by 常涉及的字段建立索引
3、SQL语句优化
- where条件判断尽量不使用!=,否则放弃索引,全表扫描
-
示例: 尽量避免NULL值判断,否则全表扫描 优化前 select id from t1 where number is null; 优化后 可以在number设置默认值为0,确保number无null值,查询如下: select id from t1 where number=0;
- 尽量避免用or来连接条件,否则放弃索引全表扫描
-
示例: 优化前 select id from t1 where id=10 or id=20; 优化后 select id from t1 where id =10 union all select id from t1 where id=20;
- union all 会重复出现,使用union即可
- 模糊查询尽量避免使用前置%,否则放弃索引全表扫描
- 尽量避免使用in 和not in ,否则放弃索引,全表扫描