【发布时间】:2023-03-25 01:08:01
【问题描述】:
我有一个执行不佳的“不存在查询”。所以我认为“左连接查询”会执行得更好,但事实并非如此。我在 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
为什么“左连接查询”的性能较差?我以为它会更快。我有相同查询的第三个版本,但使用“不在”。该查询与“不存在查询”非常相似,我认为我不需要在此处发布。
谁能建议一个更好/更快的“左连接查询”???
【问题讨论】:
-
这是哪个 RDBMS?
Left join ... where null通常在 MySQL 中表现更好,而not exists通常在其他一些 RDBMS(如 Oracle 和 SQLServer)中表现更好。 -
Aaron Bertrand 的必读文章(仅限 Sql Server):Should I use NOT IN, OUTER APPLY, LEFT OUTER JOIN, EXCEPT, or NOT EXISTS?。
-
这是 Jet,我正在从 vba/access 运行查询
标签: sql left-join max not-exists