我正试图找到一种好的、高效的方式来运行这样的查询:
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')
顺便说一句,很抱歉,如果这是我可以用 Google 搜索的内容,我不确定该怎么调用它。
最佳答案
Subquery factoring可能有帮助:
WITH people AS
( SELECT id
FROM tableB b
CONNECT BY PRIOR b.id = b.manager_id
START WITH b.id = 'managerBob'
)
SELECT *
FROM tableA a
WHERE a.manager IN (SELECT id FROM people)
OR a.teamLead IN (SELECT id FROM people)
OR a.creator IN (SELECT id FROM people)
关于SQL 多个共享 WHERE 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7675585/