0

我有一个执行不佳的“不存在查询”。所以我认为“左连接查询”会执行得更好,但事实并非如此。我在 VBA 中运行这些查询。

这是我的“不存在查询”

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 
                 ) 

这是我试图达到相同结果但使用“左连接查询”的方式

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

为什么“左连接查询”的性能较差?我以为它会更快。我有相同查询的第三个版本,但使用“不在”。该查询与“不存在查询”非常相似,我认为我不需要在这里发布它。

任何人都可以提出更好/更快的“左连接查询”吗???

4

1 回答 1

0

我会说您需要以与它们在子句中出现的顺序相同的顺序对参与 join/where 子句的where所有列进行索引。确保您拥有各自的索引,并且 thenot exists和 thejoin都应该执行类似且快速的操作。

于 2013-04-17T10:06:59.170 回答