简体   繁体   English

SQL查询改进 - MSAccess 2007

[英]SQL query improvement - MSAccess 2007

I have a need to Select all records based on the following SQL: 我需要根据以下SQL选择所有记录:

Select ID, ID2 From Table1 Where ID2 NOT IN (Select ID2 From Table2 Where ID3 IN (151,157))

There are 171k records in Table1 and 70k records in Table2 where 'ID3 IN (151,157)'. 表1中有171k记录,表2中有70k记录,其中'ID3 IN(151,157)'。

Unfortunately, that query takes forever; 不幸的是,该查询需要永远; in fact, I have never seen it complete on a system with 32GB memory and quad I7 processors. 事实上,我从未在具有32GB内存和四核I7处理器的系统上看到它完成。 I give up and cancel after 30 minutes. 30分钟后我放弃并取消。

I figure there is an SQL guru or two here that can tell me how to improve this query and get it to complete in under a minute. 我想这里有一个或两个SQL大师可以告诉我如何改进这个查询并让它在一分钟内完成。

You could try joining the subquery: 您可以尝试加入子查询:

Select ID, ID2
From Table1
LEFT JOIN 
(Select ID2
 From Table2
 Where ID3 IN (151,157))
WHERE ID2 IS NULL

If that doesn't work, I would consider creating the subquery as temp table and referencing that. 如果这不起作用,我会考虑将子查询创建为临时表并引用它。

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

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