oracle 树形结构经常要通过parent_id生成parent_ids(当前节点路径)

//根据parent_id生成parent_ids            
update  sys_office
set 
  parent_ids = (   
    SELECT parent_ids from
    (
      SELECT tt.id,  TRIM(LEADING ',' FROM  SYS_CONNECT_BY_PATH(tt.id, ','))  as parent_ids  from SYS_OFFICE  tt
      START WITH tt.parent_id='0'
      CONNECT BY PRIOR tt.id  =  tt.parent_id
    ) t
    where t.id = sys_office.id
  );

 

在实际项目中数据量大时可能出现执行慢的情况,就分开处理,先生成一张中间表,再更新组织机构表。我在实际项目中遇到的情况是处理4w多条数据,不生成中间表执行了20分钟还没完成,于是停止执行,创建中间表,再更新,几分钟搞定。

//根据parent_id生成parent_ids,数据量大的时候创建中间表        
  create table t_test as( SELECT id,parent_ids from
    (
      SELECT tt.id,  TRIM(LEADING ',' FROM  SYS_CONNECT_BY_PATH(tt.id, ','))  as parent_ids  from SYS_OFFICE  tt
      START WITH tt.parent_id='0'
      CONNECT BY PRIOR tt.id  =  tt.parent_id
    ) t )

//更新组织表
    update sys_office o set o.parent_ids = (select t.parent_ids from t_test t where o.id=t.id)

相关文章:

  • 2021-07-13
  • 2021-07-28
  • 2022-03-07
  • 2022-12-23
  • 2021-06-05
  • 2022-02-05
  • 2021-07-11
猜你喜欢
  • 2022-12-23
  • 2021-10-06
  • 2022-02-02
  • 2022-12-23
  • 2021-10-24
  • 2022-12-23
相关资源
相似解决方案