Mysql Stored Procedure Acceleration

Source: Internet
Author: User

First, let's look at a stored procedure, which is used to construct data. Of course, the stored procedure simplifies some unnecessary table structures.

CREATE PROCEDURE modifyRootEntry()BEGIN  DECLARE done INT DEFAULT FALSE;  DECLARE userId INT;    DECLARE userIdIter CURSOR FOR SELECT DISTINCT user_id from entries;   OPEN userIdIter;  read_loop: LOOP    FETCH userIdIter INTO userId;       IF done THEN      LEAVE read_loop;    END IF;INSERT INTO entries (id, name, user_id, parent_id) VALUES(0, 'root_parent', userId, 0);UPDATE entries SET parent_id=0 where user_id=userId AND name='file_root' AND parent_id is NULL;  END LOOP;  CLOSE userIdIter;END;
CREATE PROCEDURE modifyRootEntry()BEGIN  DECLARE done INT DEFAULT FALSE;  DECLARE userId INT;    DECLARE userIdIter CURSOR FOR SELECT DISTINCT user_id from entries;   START TRANSACTION; // here !  OPEN userIdIter;     read_loop: LOOP    FETCH userIdIter INTO userId;       IF done THEN      LEAVE read_loop;    END IF;INSERT INTO entries (id, name, user_id, parent_id) VALUES(0, 'root_parent', userId, 0);UPDATE entries SET parent_id=0 where user_id=userId AND name='file_root' AND parent_id is NULL;  END LOOP;  CLOSE userIdIter;  COMMIT;// here !END;
UPDATE entries SET parent_id=0 where user_id=userId AND name='file_root' AND parent_id is NULL;
CREATE PROCEDURE modifyRootEntry()BEGIN  DECLARE done INT DEFAULT FALSE;  DECLARE userId INT;    DECLARE userIdIter CURSOR FOR SELECT DISTINCT user_id from entries;   START TRANSACTION; // here !  OPEN userIdIter;     read_loop: LOOP    FETCH userIdIter INTO userId;       IF done THEN      LEAVE read_loop;    END IF;INSERT INTO entries (id, name, user_id, parent_id) VALUES(0, 'root_parent', userId, 0);  END LOOP;  UPDATE entries SET parent_id=0 where user_id=userId AND name='file_root' AND parent_id is NULL; // here!  CLOSE userIdIter;  COMMIT;// here !END;
 CREATE PROCEDURE pFastCreateNums (cnt INT UNSIGNED)BEGINDECLARE s INT UNSIGNED DEFAULT 1;TRUNCATE TABLE Nums;INSERT INTO Nums SELECT s;WHILE s*2 <= cnt DOBEGININSERT INTO Nums SELECT a+s FROM Nums;SET s = s*2;END;END WHILE;END;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.