[转]用表连接取代not in查询

转自:http://www.cnblogs.com/gzaaron/archive/2006/02/27/338908.html

写了好几个页面,速度都上不去,瓶颈在于SQL查询。太多的表,太多的not in,总是从一大推表和数据中筛选出一点数据。看了很多关于SQL优化的文章,都强烈要求不要太多使用not in查询,最好用表连接来取代它。如:

select ID,name from Table_A where ID not in (select ID from Table_B)

呵呵,这句是最经典的not in查询了。改为表连接代码如下:

select Table_A.ID,Table_A.name from Table_A left join Table_B on Table_A.ID=Table_B.ID and Table_B.ID is null
或者:
select Table_A.ID,Table_A.name from Table_A left join Table_B on Table_A.ID=Table_B.ID where Table_B.ID is null

经试用,效果立竿见影,呵呵:)   

原文地址:https://www.cnblogs.com/abinxm/p/1923262.html