sql - 左连接 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
解决方案
我会从tableb
再次加入columnb
单独开始join
。
在其他连接上,您可以ISNULL
像这样使用该功能tablec.Id = ISNULL(tableba.columnA, tablebb.columnA)
。
取决于您需要什么以及您可以使用UNION
/UNION ALL
或COALESCE
从tableb
.
并确保其中的两列tableb
都被索引覆盖。
推荐阅读
- event-handling - 为什么在基于编排的 Sagas 中需要命令?
- ios - 等待 ios speechSynthesizer.speak 完成话语
- wpf - DotNetCore 3.1 WPF 网格布局管理器问题
- javascript - 使用 React 钩子制作列表
- pandas - DataFrame where 和 query 的区别
- python - Django - 如何从与不同值匹配的视图中呈现值?
- javascript - 调用 ASP.Net WebAPI 端点时,JavaScript 获取调用返回空字符串
- flutter - 从 API 中的字符串中删除特殊字符
- xamarin.forms - 将 DataTemplate 添加到 Xamarin 表单中的 ContentPage 时内容消失
- postgresql - 如何终止长时间运行的 postgres 查询(在 plv8 扩展中)?