1. Create a test table CREATE TABLE `mysql_genarate` ( `id` int(11) NOT NULL AUTO_INCREMENT, `uuid` varchar(50) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5999001 DEFAULT CHARSET=utf8; 2. Create a stored procedure for loop insertion CREATE DEFINER=`root`@`localhost` PROCEDURE `test_two1`() BEGIN DECLARE i INT DEFAULT 0; WHILE i < 3000 DO INSERT INTO mysql_genarate ( uuid ) VALUES( UUID() ); SET i = i + 1; END WHILE; END Calling the test 3. Optimize stored procedures Use batch insert sql statement CREATE DEFINER=`root`@`localhost` PROCEDURE `insertPro`( IN sum INT ) BEGIN DECLARE count INT DEFAULT 0; DECLARE i INT DEFAULT 0; SET @exesql = concat( "insert into mysql_genarate(uuid) values" ); SET @exedata = ""; SET count = 0; SET i = 0; WHILE count < sum DO SET @exedata = concat( @exedata, ",(UUID())" ); SET count = count + 1; SET i = i + 1; IF i % 1000 = 0 THEN SET @exedata = SUBSTRING( @exedata, 2 ); SET @exesql = concat( "insert into mysql_genarate(uuid) values ", @exedata ); PREPARE stmt FROM @exesql; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET @exedata = ""; END IF; END WHILE; IF length( @exedata ) > 0 THEN SET @exedata = SUBSTRING( @exedata, 2 ); SET @exesql = concat( "insert into mysql_genarate(uuid) values ", @exedata ); PREPARE stmt FROM @exesql; EXECUTE stmt; DEALLOCATE PREPARE stmt; END IF; END Calling insertPro(10000) takes a few tenths of a second, which is acceptable. The above is the details of MySQL loop insertion of tens of millions of data. For more information about MySQL loop insertion, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: JS implements multiple tab switching carousel
>>: Use of Linux bzip2 command
<br />It has been no more than two years sin...
It took me three hours to install MySQL myself. E...
Table of contents Why do we need garbage collecti...
Introduction The module that limits the number of...
Vue stores storage with Boolean values I encounte...
Table of contents Standards for smooth animation ...
Table of contents Canal Maxwell Databus Alibaba C...
Basic syntax The use of text-overflow requires th...
Problem phenomenon: [root@localhost ~]# docker im...
Table of contents Question: Case (1) fork before ...
This article introduces the content related to gi...
The ".zip" format is used to compress f...
Table of contents Vue monitor properties What is ...
Preface Recently, when I was building a project, ...
K8s k8s is a cluster. There are multiple Namespac...