|
本帖最后由 chengccy2010 于 2023-9-13 14:02 编辑
现在有一个表: 节点组,节点,节点层级,父节点, 节点名称现在需要增加一个全路径名称,但是这个表非常大,有几百万。 递归一次非常久,而且这个字段高频使用,做成函数效率也不高。想问问大佬们有什么解决方案没有。
demo:- create table t_test (group_id varchar2(100), node_id varchar2(100), p_node_id varchar2(100), node_level number,node_name varchar2(100));
- INSERT INTO T_TEST
- (GROUP_ID, NODE_ID, P_NODE_ID, NODE_LEVEL, NODE_NAME)
- SELECT 'A', LPAD(LEVEL, 4, '0') AS NODE_ID, 'root' AS P_NODE_ID,
- 1 AS NODE_LEVEL, LEVEL NODE_NAME
- FROM DUAL
- CONNECT BY LEVEL <= 10;
- INSERT INTO T_TEST
- (GROUP_ID, NODE_ID, P_NODE_ID, NODE_LEVEL, NODE_NAME)
- SELECT A.GROUP_ID, A.NODE_ID || B.NODE_ID, A.NODE_ID, 2 AS NODE_LEVEL,
- B.NODE_NAME
- FROM T_TEST A,
- (SELECT LPAD(LEVEL, 4, '0') AS NODE_ID, LEVEL NODE_NAME
- FROM DUAL
- CONNECT BY LEVEL <= 100) B
- WHERE A.NODE_LEVEL = 1;
- INSERT INTO T_TEST
- (GROUP_ID, NODE_ID, P_NODE_ID, NODE_LEVEL, NODE_NAME)
- SELECT A.GROUP_ID, A.NODE_ID || B.NODE_ID, A.NODE_ID, 3 AS NODE_LEVEL,
- B.NODE_NAME
- FROM T_TEST A,
- (SELECT LPAD(LEVEL, 4, '0') AS NODE_ID, LEVEL NODE_NAME
- FROM DUAL
- CONNECT BY LEVEL <= 1000) B
- WHERE A.NODE_LEVEL = 2;
- INSERT INTO T_TEST
- (GROUP_ID, NODE_ID, P_NODE_ID, NODE_LEVEL, NODE_NAME)
- SELECT A.GROUP_ID, B.NODE_ID, B.NODE_ID, B.NODE_LEVEL, B.NODE_NAME
- FROM (SELECT CHR(65 + LEVEL) AS GROUP_ID FROM DUAL CONNECT BY LEVEL <= 10) A,
- T_TEST B;
- COMMIT;
- alter table T_TEST add path VARCHAR2(1000);
复制代码
现在的SQL: 如果记录数在百万以下效率还可以, 而后直线下降。
MERGE INTO T_TEST A
USING (SELECT T.ROWID AS RD, SYS_CONNECT_BY_PATH(NODE_NAME, '/') AS PATH
FROM T_TEST T
START WITH T.P_NODE_ID = 'root'
CONNECT BY PRIOR NODE_ID = P_NODE_ID
AND PRIOR GROUP_ID = GROUP_ID) B
ON (A.ROWID = B.RD)
WHEN MATCHED THEN
UPDATE SET A.PATH = B.PATH
|
|