【问题标题】:left join query instead of not exists左连接查询而不是不存在
【发布时间】: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

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

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

【问题讨论】:

标签: sql left-join max not-exists


【解决方案1】:

我想说,您需要对参与 join/where 子句的所有列的索引以相同的顺序它们出现在 where 子句中。确保您拥有各自的索引,并且 not existsjoin 的性能应该相似且快速。

【讨论】:

  • 是的,我在 where 子句的所有列上都有索引。但正如前面提到的,我离开 join 的方式没有使用 indeces。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-30
  • 1970-01-01
相关资源
最近更新 更多