简体   繁体   中英

How to make SQL query faster?

I have big DB. It's about 1 mln strings. I need to do something like this:

select * from t1 WHERE id1 NOT IN (SELECT id2 FROM t2)

But it works very slow. I know that I can do it using "JOIN" syntax, but I can't understand how.

Try this way:

select * 
from t1
left join t2 on t1.id1 = t2.id
where t2.id is null

首先,您应该优化两个表中的索引,然后使用join

There are different ways a dbms can deal with this task:

It can select id2 from t2 and then select all t1 where id1 is not in that set. You suggest this using the IN clause.

It can select record by record from t1 and look for each record if it finds a match in t2. You would suggest this using the EXISTS clause.

You can outer join the table then throw away all matches and stay with the non-matching entries. This may look like a bad way, especially when there are many matches, because you would get big intermediate data and then throw most of it away. However, depending on how the dbms works, it can be rather fast, for example when it applies hash join techniques.

It all depends on table sizes, number of matches, indexes, etc. and on what the dbms makes of your query. There are dbms that are able to completely re-write your query to find the best execution plan.

Having said all this, you can just try different things:

  • the IN clause with (SELECT DISTINCT id2 FROM t2). DISTINCT can reduce the intermediate result significantly and really speed up your query. (But maybe your dbms does that anyhow to get a good execution plan.)
  • use an EXISTS clause and see if that is faster
  • the outer join suggested by Parado

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM