在我的previous question,我可以解决如何获取位置层次 struct .但当我try 与系统集成时,似乎有另一个问题
所以我有两张桌子,像
位置(约20,000条记录)
id | Name | is_active | level1 | level2 | level3 | level4 |
---|---|---|---|---|---|---|
1 | A | true | A | null | null | null |
2 | A>B | true | A | B | null | null |
3 | A>B>C | true | A | B | C | null |
4 | A>B>C>D | true | A | B | C | D |
5 | X | true | X | null | null | null |
日志(log)(约500万条记录)
id | Name | location_id |
---|---|---|
1 | log 001 | 3 |
2 | log 002 | 5 |
因此,目标是我想获取的位置层次 struct ,其中只有日志(log). 问题是,当我try 连接日志(log)表时,它有一个性能问题.查询似乎需要10多分钟
下面是我的查询看起来像demo 我正在try 这个查询
SELECT *
FROM location
START WITH id IN (SELECT DISTINCT location_id FROM log_response)
CONNECT BY PRIOR name
= name || '>' || COALESCE(PRIOR level4, PRIOR level3, PRIOR level2)
ORDER BY NAME
或
WITH cte(location_id) AS (
SELECT DISTINCT location_id FROM log_response
)
SELECT *
FROM location
START WITH id IN (SELECT location_id FROM cte)
CONNECT BY PRIOR name
= name || '>' || COALESCE(PRIOR level4, PRIOR level3, PRIOR level2)
ORDER BY NAME
P/S以上示例的查询预期应返回(1,2,3,5)中的位置HAD ID
id | Name | is_active | level1 | level2 | level3 | level4 |
---|---|---|---|---|---|---|
1 | A | true | A | null | null | null |
2 | A>B | true | A | B | null | null |
3 | A>B>C | true | A | B | C | null |
5 | X | true | X | null | null | null |
非常感谢!