(fine) replace not in query with table join

I have written several pages, but the speed cannot go up. The bottleneck lies in the SQL query. Too many tables, too many not ins, always sifting out a little bit of data from a large table and data. After reading a lot of articles on SQL optimization, it is strongly recommended not to use too many not in queries, and it is better to replace it with table joins. Such as:

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

Oh, this sentence is the most classic not in query. Change to the table join code as follows:

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
or:
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

After trial, the effect is immediate, huh :)

 

 

 

REFS:http://blog.csdn.net/terryhuang/article/details/2517004

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326176813&siteId=291194637
Recommended