MySQL loop inserts tens of millions of data

MySQL loop inserts tens of millions of data

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 call test_two1() , it takes several minutes to test 10,000 data. If it is tens of millions of data, this speed will be unbearable.

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:
  • Steps for importing tens of millions of data into MySQL using .Net Core
  • .Net Core imports tens of millions of data into MySQL database
  • Detailed explanation of MySQL database tens of millions of data query and storage
  • Optimizing query speed of MySQL with tens of millions of data using indexes
  • How to quickly paginate MySQL data volumes of tens of millions
  • Optimizing the performance of paging query for MySQL with tens of millions of data
  • How to optimize MySQL tables with tens of millions of data?
  • Sharing ideas on processing tens of millions of data in a single MySQL table

<<:  JS implements multiple tab switching carousel

>>:  Use of Linux bzip2 command

Recommend

How to use JS WebSocket to implement simple chat

Table of contents Short Polling Long-Polling WebS...

Detailed explanation of :key in VUE v-for

When key is not added to the v-for tag. <!DOCT...

Some useful meta setting methods (must read)

<meta name="viewport" content="...

Example of creating table statements for user Scott in MySQL version of Oracle

Overview: Oracle scott user has four tables, whic...

A brief discussion on the understanding of TypeScript index signatures

Table of contents 1. What is an index signature? ...

Solution to MySQL server login error ERROR 1820 (HY000)

Fault site: Log in to the MySQL server and get th...

Detailed explanation of Mysql logical architecture

1. Overall architecture diagram Compared to other...

Vue implements accordion effect

This article example shares the specific code of ...

Commonly used JavaScript array methods

Table of contents 1. filter() 2. forEach() 3. som...

How to set up Windows Server 2019 (with pictures and text)

1. Windows Server 2019 Installation Install Windo...

Detailed steps to install JDK and Tomcat in Linux environment

Table of contents 1. Install JDK Manual Installat...

How to use .htaccess to prohibit a certain IP from accessing the website

Preface For cost considerations, most webmasters ...

MySQL intercepts the sql statement of the string function

1. left(name,4) intercepts the 4 characters on th...

Negative distance (empathy) - iterative process of mutual influence

Negative distance refers to empathy. Preface (rai...