简体   繁体   English

如何使SQL查询更快?

[英]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. 我知道我可以使用“ JOIN”语法来做到这一点,但是我不知道该怎么做。

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: dbms可以通过多种方式处理此任务:

It can select id2 from t2 and then select all t1 where id1 is not in that set. 它可以从t2中选择id2,然后选择id1不在该集合中的所有t1。 You suggest this using the IN clause. 您建议使用IN子句。

It can select record by record from t1 and look for each record if it finds a match in t2. 它可以从t1中逐条记录地选择记录,并在t2中找到匹配项时查找每条记录。 You would suggest this using the EXISTS clause. 您可以使用EXISTS子句建议这样做。

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. 但是,根据dbms的工作方式,它可能会相当快,例如,在应用哈希联接技术时。

It all depends on table sizes, number of matches, indexes, etc. and on what the dbms makes of your query. 这完全取决于表的大小,匹配项的数量,索引等,以及dbms对查询的影响。 There are dbms that are able to completely re-write your query to find the best execution plan. 有些dbms能够完全重写查询以找到最佳执行计划。

Having said all this, you can just try different things: 说完这些,您可以尝试不同的方法:

  • the IN clause with (SELECT DISTINCT id2 FROM t2). IN子句带有(SELECT DISTINCT id2 FROM t2)。 DISTINCT can reduce the intermediate result significantly and really speed up your query. DISTINCT可以大大减少中间结果,并确实加快查询速度。 (But maybe your dbms does that anyhow to get a good execution plan.) (但是也许您的dbms会这样做,以获得良好的执行计划。)
  • use an EXISTS clause and see if that is faster 使用EXISTS子句,看看是否更快
  • the outer join suggested by Parado Parado建议的外部联接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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