场景复现,一个索引提高600倍查询速度? 首先准备一张books表 create table books( id int not null primary key auto_increment, name ) not null, author ) not null, created_at datetime not null default current_timestamp, updated_at datetime not null default current_timestamp on up
使用高效的查询 使用 EXISTS 代替 IN -- 查询A表中同时存在B表的数据 -- 慢 SELECT * FROM Class_A WHERE id IN (SELECT id FROM Class_B); -- 快 SELECT * FROM Class_A A WHERE EXISTS (SELECT * FROM Class_B B WHERE A.id = B.id); EXISTS更快的原因: 如果连接列(id )上建立了索引,那么查询 Class_B 时不用查 实际的表,只需查