SQL 多个共享 WHERE 条件
我正在尝试找到一种良好、有效的方法来运行这样的查询:
SELECT *
FROM tableA a
WHERE a.manager IN ( SELECT id
FROM tableB b
CONNECT BY PRIOR b.id = b.manager_id
START WITH b.id = 'managerBob')
OR a.teamLead IN ( SELECT ID
FROM tableB b
CONNECT BY PRIOR b.ID = b.manager_id
START WITH b.ID = 'managerBob')
OR a.creator IN ( SELECT id
FROM tableB b
CONNECT BY PRIOR b.id = b.manager_id
START WITH b.id = 'managerBob')
如您所见,我正在尝试使用多个 WHERE 子句,但每个子句在等式右侧使用相同的数据集。如果我使用多个子句,它似乎运行得很慢,而且我很确定这是因为 Oracle 正在运行每个子查询。有没有办法让这样的事情发挥作用?
SELECT *
FROM tableA a
WHERE a.manager,
a.teamLead,
a.creator in ( SELECT id
FROM tableB b
CONNECT BY PRIOR b.id = b.manager_id
START WITH b.id = 'managerBob')
顺便说一句,如果我可以在谷歌上搜索到这个内容,我很抱歉,我不知道该怎么称呼它。
I'm trying to find a good, efficient way to run a query like this:
SELECT *
FROM tableA a
WHERE a.manager IN ( SELECT id
FROM tableB b
CONNECT BY PRIOR b.id = b.manager_id
START WITH b.id = 'managerBob')
OR a.teamLead IN ( SELECT ID
FROM tableB b
CONNECT BY PRIOR b.ID = b.manager_id
START WITH b.ID = 'managerBob')
OR a.creator IN ( SELECT id
FROM tableB b
CONNECT BY PRIOR b.id = b.manager_id
START WITH b.id = 'managerBob')
As you can see, I'm trying to use multiple WHERE clauses, but each clause is using the same dataset on the right-hand side of the equation. It seems to run very slowly if I use more than one clause, and I'm pretty sure that it's because Oracle is running each subquery. Is there a way to make something like this work?
SELECT *
FROM tableA a
WHERE a.manager,
a.teamLead,
a.creator in ( SELECT id
FROM tableB b
CONNECT BY PRIOR b.id = b.manager_id
START WITH b.id = 'managerBob')
By the way, I'm sorry if this is something I could have Googled, I'm not sure what to call this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
子查询分解可能会有所帮助:
Subquery factoring may help:
您可以执行以下操作:
替代方案(检查 DISTINCT 的使用:如果 id 在表 B 中不唯一,则这不等效):
You can do:
Alternative (check the use of DISTINCT: if ids are not unique in table B then this is not equivalent):
根据评论更新 - 尝试
UPDATE as per comments - try