首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >左联接查询而不是不存在

左联接查询而不是不存在
EN

Stack Overflow用户
提问于 2013-04-16 18:03:22
回答 1查看 1.8K关注 0票数 0

我有一个“不存在的查询”,表现很差。因此,我认为“左联接查询”的性能会更好,但事实并非如此。我正在VBA中运行这些查询。

这是我的“不存在的查询”

代码语言:javascript
复制
SELECT * FROM MyTable AS t1
WHERE t1.ID_person = 1960081947465
and t1.ID_company <> 68550
and t1.FullTime + 26.3 >= 37.33 
and t1.Date <= 20130101
and t1.Code not in (31,28) 
and t1.FullTime < 100          
and not exists (
                  select * from MyTable t2 where 
                  t1.ID_person = t2.ID_person 
                  and t2.ID_company <> 68550
                  and t2.FullTime + 26.3 >= 37.33 
                  and t2.Date <= 20130101 
                  and t2.Code not in (31,28) 
                   and t1.Date< t2.Date 
                 ) 

下面是我试图实现相同结果的方式,但使用“左联接查询”

代码语言:javascript
复制
SELECT * FROM MyTable AS t1
LEFT JOIN
(
 select * from MyTable t2 where
 and t2.ID_company <> 68550
 and t2.FullTime + 26.3 >= 37.33
 and t2.Date <= 20130101
 and t2.Code not in (31,28)
 ) subq
 ON t1.ID_person = subq.ID_person and t1.Date < subq.Date 
 WHERE t1.ID_person = 1960081947465
 and t1.ID_company <> 68550
 and t1.FullTime + 26.3 >= 37.33
 and t1.Date <= 20130101
 and t1.Code not in (31,28)
 and t1.FullTime < 100          
 and subq.ID_person IS NULL

为什么“左联接查询”性能较差?我以为会更快。我有同一个查询的第三个版本,但使用"not“。该查询非常类似于"not查询“,因此我认为不需要在这里发布它。

有人能建议一个更好/更快的“左连接查询”吗?

EN

回答 1

Stack Overflow用户

发布于 2013-04-17 10:06:59

我想说的是,您需要对所有参与join/where子句的列按照它们在where子句中出现的顺序进行索引。确保您有各自的索引,并且not existsjoin都应该执行类似和快速的操作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16043980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档