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 quickly build an LNMP environment with Docker (latest)

Preface Tip: Here you can add the approximate con...

Interpreting MySQL client and server protocols

Table of contents MySQL Client/Server Protocol If...

Node.js returns different data according to different request paths.

Table of contents 1. Learn to return different da...

Ubuntu opens port 22

Scenario You need to use the xshell tool to conne...

Graphical tutorial on installing JDK1.8 under CentOS7.4

Linux installation JDK1.8 steps 1. Check whether ...

Do designers need to learn to code?

Often, after a web design is completed, the desig...

How to build LNMP environment on Ubuntu 20.04

Simple description Since it was built with Centos...

Solution to MySQL IFNULL judgment problem

Problem: The null type data returned by mybatis d...

How to use vs2019 for Linux remote development

Usually, there are two options when we develop Li...

Pricing table implemented with CSS3

Result: Implementation Code html <div id="...

How to use nginx to simulate blue-green deployment

This article introduces blue-green deployment and...

JavaScript operation element examples

For more information about operating elements, pl...

MySQL 8.0 Window Function Introduction and Summary

Preface Before MySQL 8.0, it was quite painful to...

Detailed explanation of nginx installation, deployment and usage on Linux

Table of contents 1. Download 2. Deployment 3. Ng...

Implementation of services in docker accessing host services

Table of contents 1. Scenario 2. Solution 3. Conc...