左联接OR条件的替代

时间:2019-10-17 06:42:35

标签: sql sql-server performance join left-join

带有或条件的左连接会降低查询性能,因此可以替代左连接或条件

select tableb.columnA, tableb.columnB,tablec.id,tabled.id,tablee.id
from tableA tableA
LEFT JOIN tableb tableb ON ((tableb.columnA = tableA.id)
                         OR (tableb.columnB = tableA.id))

LEFT JOIN tablec tablec ON tablec.Id = tableb.columnA 
LEFT JOIN tabled tabled ON tabled.id = tableb.columnB
LEFT JOIN tablee tablee ON tablee.Id= tableb .id

1 个答案:

答案 0 :(得分:0)

我将从在单独的tableb的{​​{1}}上再次加入columnb开始。

在其他联接上,您可以像join一样使用ISNULL函数。

根据您的需求和数据,可以使用tablec.Id = ISNULL(tableba.columnA, tablebb.columnA) / UNIONUNION ALLCOALESCE的适当实例中获取数据。

并确保tableb中的两列都被索引覆盖。