MySQL implements a function similar to connect_by_isleaf MySQL method or stored procedure

MySQL implements a function similar to connect_by_isleaf MySQL method or stored procedure

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!
Now the requirement is: by specifying an ID, find out all its subset members, and modify the value of this pid to the newly specified value! !
I ran it in PHP and it took about 50 seconds+ to execute, which was painful! ! !
It is necessary to recursively find all subsets and modify their pids, which is a lot of work.

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);
Disadvantages: Limited length.

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:
  • How to generate records in batches using stored procedures in MySQL under Linux
  • Simple implementation of Mybatis calling MySQL stored procedure
  • Comparison of the differences between MySQL and Oracle: Five: Stored Procedures & Functions
  • mysql stored procedure determines duplicate data and does not insert it
  • Detailed explanation of calling MySQL stored procedures in Java
  • Implementing tree traversal through stored procedures in MySQL database
  • MySQL stored procedure optimization example
  • Example analysis of stored procedures in MySQL and how to call stored procedures
  • Detailed explanation of MySQL stored procedures, cursors, and transaction examples

<<:  Summary of the use of Vue computed properties and listeners

>>:  How to deploy ElasticSearch in Docker

Recommend

HTML+CSS merge table border sample code

When we add borders to table and td tags, double ...

Design of pop-up windows and floating layers in web design

In the trend of gradual transition from tradition...

js to achieve cool fireworks effect

This article shares the specific code for using j...

The role of nextTick in Vue and several simple usage scenarios

Purpose Understand the role of nextTick and sever...

Resolving MySQL implicit conversion issues

1. Problem Description root@mysqldb 22:12: [xucl]...

Tomcat's class loading mechanism process and source code analysis

Table of contents Preface 1. Tomcat class loader ...

Introduction to the common API usage of Vue3

Table of contents Changes in the life cycle react...

Install nodejs and yarn and configure Taobao source process record

Table of contents 1. Download nodejs 2. Double-cl...

How to optimize MySQL query speed

In the previous chapters, we introduced how to ch...

Solution for VMware Workstation Pro not running on Windows

After the National Day holiday, did any of you fi...

Application scenarios and design methods of MySQL table and database sharding

Many friends have asked in forums and message are...

Detailed explanation of angular parent-child component communication

Table of contents APIs used Simple Example person...

Convert XHTML CSS pages to printer pages

<br />In the past, creating a printer-friend...

Simple example of limit parameter of mysql paging

Two parameters of Mysql paging select * from user...