mysql 单标递归_MySql整理篇之递归

问题:Mysql递归实现

方式一:存储过程,方式二:函数

本文再其基础之上考研效率问题,只在乎实现的同学参考大侠的足矣,想继续提高效率的可继续阅读。再参考博文中,大侠已说明,推介方式二(函数)实现,下面也在方式二的基础之上进行考研。

1、创建表:t_temp_node

CREATE TABLE `t_temp_node` (

`id` decimal(9,0) NOT NULL,

`fid` decimal(9,0) DEFAULT NULL,

`node_id` varchar(50) DEFAULT NULL,

`node_code` varchar(50) DEFAULT NULL,

`node_name` varchar(50) DEFAULT NULL,

`son_node_type` varchar(50) DEFAULT NULL,

`son_node_content` varchar(4000) DEFAULT NULL,

KEY `ind_t_temp_node` (`id`,`fid`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

select n.* from t_temp_node n,(select (@nodes:= queryChildrenTempNode(14593)) as pids) t

where FIND_IN_SET(n.id,t.pids) order by id;

2、随便插入多条数据

insert into `t_temp_node`(`id`,`fid`,`node_id`,`node_code`,`node_name`,`son_node_type`,`son_node_content`) values (14593,null,'28362','PlatformType','PlatformType',null,null);

insert into `t_temp_node`(`id`,`fid`,`node_id`,`node_code`,`node_name`,`son_node_type`,`son_node_content`) values (14594,14593,'28362','PlatformType','PlatformType',null,null);

insert into `t_temp_node`(`id`,`fid`,`node_id`,`node_code`,`node_name`,`son_node_type`,`son_node_content`) values (14595,14594,'28362','PlatformType','PlatformType',null,null);

insert into `t_temp_node`(`id`,`fid`,`node_id`,`node_code`,`node_name`,`son_node_type`,`son_node_content`) values (14596,14594,'28362','PlatformType','PlatformType',null,null);

insert into `t_temp_node`(`id`,`fid`,`node_id`,`node_code`,`node_name`,`son_node_type`,`son_node_content`) values (14597,14594,'28362','PlatformType','PlatformType',null,null);

insert into `t_temp_node`(`id`,`fid`,`node_id`,`node_code`,`node_name`,`son_node_type`,`son_node_content`) values (14598,14595,'28362','PlatformType','PlatformType',null,null);

insert into `t_temp_node`(`id`,`fid`,`node_id`,`node_code`,`node_name`,`son_node_type`,`son_node_content`) values (14599,14595,'28362','PlatformType','PlatformType',null,null);

insert into `t_temp_node`(`id`,`fid`,`node_id`,`node_code`,`node_name`,`son_node_type`,`son_node_content`) values (14600,14595,'28362','PlatformType','PlatformType',null,null);

insert into `t_temp_node`(`id`,`fid`,`node_id`,`node_code`,`node_name`,`son_node_type`,`son_node_content`) values (14601,14595,'28362','PlatformType','PlatformType',null,null);

insert into `t_temp_node`(`id`,`fid`,`node_id`,`node_code`,`node_name`,`son_node_type`,`son_node_content`) values (14602,14595,'28362','PlatformType','PlatformType',null,null);

insert into `t_temp_node`(`id`,`fid`,`node_id`,`node_code`,`node_name`,`son_node_type`,`son_node_content`) values (14603,14600,'28362','PlatformType','PlatformType',null,null);

insert into `t_temp_node`(`id`,`fid`,`node_id`,`node_code`,`node_name`,`son_node_type`,`son_node_content`) values (14604,14600,'28362','PlatformType','PlatformType',null,null);

insert into `t_temp_node`(`id`,`fid`,`node_id`,`node_code`,`node_name`,`son_node_type`,`son_node_content`) values (14605,14600,'28362','PlatformType','PlatformType',null,null);

3、创建函数:queryChildrenTempNode

DROP FUNCTION IF EXISTS queryChildrenTempNode;

CREATE FUNCTION ciim.`queryChildrenTempNode`(p_id int) RETURNS varchar(20000) CHARSET utf8

BEGIN

DECLARE sTemp VARCHAR(20000);

DECLARE sTempChd VARCHAR(5000);

SET sTemp='$';

SET sTempChd = CAST(p_id AS CHAR);

WHILE sTempChd IS NOT NULL DO

SET sTemp = CONCAT(sTemp,',',sTempChd);

SELECT GROUP_CONCAT(distinct id) INTO sTempChd FROM t_temp_node WHERE FIND_IN_SET(fid,sTempChd) > 0;

END WHILE;

RETURN sTemp;

END;

4、验证

4.1 引用查询

select queryChildrenTempNode(14593); -- 0.215秒

4.2 嵌套查询

select * from t_temp_node where FIND_IN_SET(id,queryChildrenTempNode(14593));--  1.659秒

4.3 临时参数查询

select n.* from t_temp_node n,(select (@nodes:= queryChildrenTempNode(14593)) as pids) t

where FIND_IN_SET(n.id,t.pids);-- 0.215秒

5、得出结论

采用临时参数的方式大大提高了效率,数据量大的时候尤为明显。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值