Day45 MySql 索引优化实战二

2022-05-20  本文已影响0人  小周爱吃瓜

关联字段加索引。

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

上一篇 下一篇

猜你喜欢

热点阅读