mysql 快速构建树形测试数据


create table t_org_tree (org_id int  , parent_id int , memo varchar(64),PRIMARY KEY (`org_id`) USING BTREE,KEY `indx_org_tree_parent_id` (`parent_id`) USING BTREE);


select * from t_org_tree;



CREATE PROCEDURE InsertDataToOrgTree0(parent int)
BEGIN
  DECLARE counter INT DEFAULT parent*10;
  DECLARE maxChild INT DEFAULT parent*10+9;
  
  WHILE counter <= maxChild and  counter < 100000 DO
    SET @memo := uuid();
    INSERT INTO t_org_tree (org_id, parent_id, memo) VALUES (counter, parent, @memo);  
    COMMIT;
    call InsertDataToOrgTree0(counter);
    SET counter = counter + 1;
  END WHILE;
end;


CREATE PROCEDURE InsertDataToOrgTree()
BEGIN
  DECLARE counter INT DEFAULT 1;
  WHILE counter <= 9 DO
    SET @parent_id := 0 ; -- 随机选择一个parent_id
    SET @memo := uuid();
    INSERT INTO t_org_tree (org_id, parent_id, memo) VALUES (counter, @parent_id , @memo);  
    COMMIT;
    call InsertDataToOrgTree0(counter);
    SET counter = counter + 1;
  END WHILE;
end;

SET max_sp_recursion_depth = 1000;

delete from t_org_tree where 1=1 ;

-- 大约需要 700 秒
CALL InsertDataToOrgTree();


select * from t_org_tree ;

-- 直接查询

with recursive rs as (select * from t_org_tree where ORG_ID in(2 , 80 ,1000 ,399)
union  all 
select t.* from t_org_tree t , rs where rs.ORG_ID = t.parent_id
)	
select *  from rs limit 200;


with recursive rs as (select * from t_org_tree where ORG_ID in(2 , 80 ,1000 ,399)
union  all 
select t.* from t_org_tree t , rs where t.ORG_ID = rs.parent_id
)	
select * from rs ;







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值