-
分页查询优化
-
扫描整个索引并没有查找到没索引的行比扫全表的成本要高,所以优化器放弃了索引.
关联字段加索引。
- in和exists:
小表驱动大表.
- in和exists执行过程:
in (遍历的是B表)
select * from A where id in (select id from B)
等价于
for( select id from B){
select * from A where A.id = B.id
}
所以遍历的是B表,查询的是A表
exists 遍历的是A表
select * from A where exists (select 1 from B where B.id = A.id)
等价于
for (select * from A){
select * from B where B.id = A.id
}
结论: 所以说in适合B表数据不是很大的情况.
单独维护一份统计行
尽量使用not null
tinyint,int,bigint上的选择
尽量使用unsigned
date,datetime,timestamp
网友评论