Use 'Left Join' instead 'in'/'not in' for performance consideration
Posted on 2012-05-20 09:56 oceanlzhang 阅读(188) 评论(0) 收藏 举报When we use in and not in, it will take a long time to get the results. because it scans all the records of the table. we can use left join instead of in or not in like the following:
1. select * from table1 where tabel1.name not in (select name from table2) is equals to
select a.* from table1 a left join table2 b on a.name = b.name where b.name is null.
2. select * from table1 where tabel1.name in (select name from table2) is equals to
select a.* from table1 a left join table2 b on a.name = b.name where b.name is not null.