我有一个“不存在的查询”,表现很差。因此,我认为“左联接查询”的性能会更好,但事实并非如此。我正在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为什么“左联接查询”性能较差?我以为会更快。我有同一个查询的第三个版本,但使用"not“。该查询非常类似于"not查询“,因此我认为不需要在这里发布它。
有人能建议一个更好/更快的“左连接查询”吗?
发布于 2013-04-17 10:06:59
我想说的是,您需要对所有参与join/where子句的列按照它们在where子句中出现的顺序进行索引。确保您有各自的索引,并且not exists和join都应该执行类似和快速的操作。
https://stackoverflow.com/questions/16043980
复制相似问题