Recently, there is a particularly abnormal business demand, there is a table CREATE TABLE `demo` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `tid` int(11) DEFAULT '0', `pid` int(11) DEFAULT '1', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3000124 DEFAULT CHARSET=utf8; That's about it. There are more than 3 million rows of records. Each pid records the top-level category of this ID, and tid is its parent category! There is a method in Oracle called connect_by_isleaf, which can easily find all subsets, but I am using MySQL... So I will simply write here about my experience using MySQL methods or stored procedures to implement First: MySQL method CREATE DEFINER=`root`@`localhost` FUNCTION `lvtao_demo_a`(rootId int) RETURNS text CHARSET utf8 READS SQL DATA COMMENT 'demo' BEGIN DECLARE sTemp text; DECLARE sTempChd text; SET sTempChd =cast(rootId as CHAR); SET sTemp = ''; WHILE sTempChd is not null DO SET sTemp = concat(sTemp,',',sTempChd); SELECT group_concat(id) INTO sTempChd FROM demo where FIND_IN_SET(tid,sTempChd)>0; END WHILE; RETURN sTemp; END; The method of use is select lvtao_demo_a(5); But when I was testing it, the 3 million pieces of data basically crashed! ! ! Data too long for column 'sTemp' at row 1 Advantages: simple, convenient, no limit on the depth of recursive call levels (max_sp_recursion_depth, maximum 255); The second type: stored procedure + intermediate table DELIMITER ;; CREATE DEFINER=`root`@`localhost` PROCEDURE `sss`(IN pid int, IN rootid int) BEGIN DECLARE done INT DEFAULT FALSE; DECLARE id TEXT; DECLARE cur1 CURSOR FOR SELECT id FROM demo WHERE tid=pid; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; set max_sp_recursion_depth = 200; OPEN cur1; read_loop: LOOP FETCH cur1 INTO id; IF done THEN LEAVE read_loop; END IF; INSERT INTO temp (rootid,zid) values (rootid, id); call sss(id,rootid); END LOOP; CLOSE cur1; END;; DELIMITER ; Haha, 3 million data is also stuck into a ball~~~ Let's think of another way~~~~ I won't bother with this anymore You may also be interested in:
|
<<: Summary of the use of Vue computed properties and listeners
>>: How to deploy ElasticSearch in Docker
When we add borders to table and td tags, double ...
In the trend of gradual transition from tradition...
This article shares the specific code for using j...
Purpose Understand the role of nextTick and sever...
1. Problem Description root@mysqldb 22:12: [xucl]...
Table of contents Preface 1. Tomcat class loader ...
Table of contents Changes in the life cycle react...
Table of contents 1. Download nodejs 2. Double-cl...
In the previous chapters, we introduced how to ch...
After the National Day holiday, did any of you fi...
Many friends have asked in forums and message are...
question After the company migrated the server, t...
Table of contents APIs used Simple Example person...
<br />In the past, creating a printer-friend...
Two parameters of Mysql paging select * from user...