1.in和not in子查询优化
not in 是不能命中索引的,所以以下子查询性能很低。
如果是确定且有限的集合时,可以使用。如 IN (0,1,2)。
用 exists或 notexists代替
select * from test1 where EXISTS (select * from test2 where id2 = id1 ) select * FROM test1 where NOT EXISTS (select * from test2 where id2 = id1 )
用join代替
select id1 from test1 INNER JOIN test2 ON id2 = id1 select id1 from test1 LEFT JOIN test2 ON id2 = id1 where id2 IS NULL