我有一个执行不佳的“不存在查询”。所以我认为“左连接查询”会执行得更好,但事实并非如此。我在 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
为什么“左连接查询”的性能较差?我以为它会更快。我有相同查询的第三个版本,但使用“不在”。该查询与“不存在查询”非常相似,我认为我不需要在这里发布它。
任何人都可以提出更好/更快的“左连接查询”吗???