首页 > 解决方案 > 左连接 OR 条件的替代方案

问题描述

left join with or condition 会降低查询性能,所以什么可以替代 left join 或 condition

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

标签: sqlsql-serverperformancejoinleft-join

解决方案


我会从tableb再次加入columnb单独开始join

在其他连接上,您可以ISNULL像这样使用该功能tablec.Id = ISNULL(tableba.columnA, tablebb.columnA)

取决于您需要什么以及您可以使用UNION/UNION ALLCOALESCEtableb.

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


推荐阅读