SQL多个共享WHERE条件

mon*_*jbl 3 sql database oracle hierarchical-data

我正在尝试找到一个好的,有效的方式来运行这样的查询:

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')
Run Code Online (Sandbox Code Playgroud)

如您所见,我正在尝试使用多个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')
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我很抱歉,如果这是我可以用谷歌搜索的东西,我不知道该怎么称呼它.

Ton*_*ews 11

子查询因子可能有所帮助:

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)
Run Code Online (Sandbox Code Playgroud)


Ben*_*oit 6

你可以做:

WITH bob_subordinates 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 bob_subordinates)
    OR a.teamlead in (select id from bob_subordinates)
    or a.creator  in (select id from bob_subordinates)
Run Code Online (Sandbox Code Playgroud)

替代方案(检查DISTINCT的使用:如果表B中的id不是唯一的,那么这不是等价的):

WITH bob_subordinates AS (
(    SELECT DISTINCT id
       FROM tableB b
 CONNECT BY PRIOR b.id = b.manager_id
 START WITH b.id = 'managerBob')
SELECT DISTINCT a.*
  FROM tableA a JOIN bob_subordinates b ON b.id IN (a.manager, a.teamlead, a.creator);
Run Code Online (Sandbox Code Playgroud)