Detailed example code of mysql batch insert loop

Detailed example code of mysql batch insert loop

background

A few days ago, when I was doing paging on MySql, I saw a blog post saying that using limit 0,10 for paging would cause data loss, while someone else said that would not happen, so I wanted to test it myself. There was no data during the test, so I installed MySql and created a table. When I created a while loop to batch insert 100,000 test data, the execution time was unbearable, so I looked for information to find a way to optimize batch insertion. I made a note here.

Data Structure

Considering that standard columns are divided into three scenarios during paging: primary key column, index column, and common column, the test table needs to include these three scenarios. The table creation syntax is as follows:

drop table if exists `test`.`t_model`;

Create table `test`.`t_model`( 
 `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'Auto-increment primary key',
 `uid` bigint COMMENT 'Business primary key',
 `modelid` varchar(50) COMMENT 'Character primary key',
 `modelname` varchar(50) COMMENT 'name',
 `desc` varchar(50) COMMENT 'Description',
 primary key (`id`),
 UNIQUE index `uid_unique` (`uid`),
 key `modelid_index` (`modelid`) USING BTREE
)ENGINE=InnoDB charset=utf8 collate=utf8_bin;

For ease of operation, the insert operation uses a stored procedure to insert ordered data through a while loop, and the performance of other operation methods or loop methods is not verified.

Execution process

1. Use the simplest method to directly loop and insert 1W items. The syntax is as follows:

drop procedure if exists my_procedure; 
delimiter //
create procedure my_procedure()
begin
 DECLARE n int DEFAULT 1;
 WHILE n < 10001 DO
 insert into t_model (uid,modelid,modelname,`desc`) value (n,CONCAT('id20170831',n),CONCAT('name',n),'desc'); 
 set n = n + 1;
 END WHILE;
end
// 
              delimiter ;

The execution time for inserting 10,000 data items is about 6m7s. At this speed, it will take several days to insert 10 million data items.

2. So, let's consider adding a transaction commit. Can it speed up the performance? The test commits every 1000 records. The syntax is as follows:

delimiter //
create procedure u_head_and_low_pro()
begin
 DECLARE n int DEFAULT 17541;
 WHILE n < 10001 DO
   insert into t_model (uid,modelid,modelname,`desc`) value (n,CONCAT('id20170831',n),CONCAT('name',n),'desc'); 
   set n = n + 1;
   if n % 1000 = 0 
   then
    commit;
   end if;
 END WHILE;
end
//
delimiter ;

The execution time is 6 minutes and 16 seconds, which is not much different from the execution without commit. It seems that the performance of batch insert in this way is very low.

3. Use the stored procedure to generate a batch insert statement to execute batch insert to insert 10,000 records. The syntax is as follows:

drop procedure IF EXISTS u_head_and_low_pro;
delimiter $$
create procedure u_head_and_low_pro()
begin
 DECLARE n int DEFAULT 1;
 set @exesql = 'insert into t_model (uid,modelid,modelname,`desc`) values ​​';
 set @exedata = '';
 WHILE n < 10001 DO
 set @exedata = concat(@exedata,"(",n,",","'id20170831",n,"','","name",n,"','","desc'",")");
 if n % 1000 = 0 
 then
  set @exesql = concat(@exesql,@exedata,";");
  prepare stmt from @exesql;
  execute stmt;
  DEALLOCATE prepare stmt;
  commit; 
  set @exesql = 'insert into t_model (uid,modelid,modelname,`desc`) values ​​';
  set @exedata = "";
 else
  set @exedata = concat(@exedata,',');
 end if;
 set n = n + 1;
 END WHILE;
end;$$ 
delimiter ;

Execution time 3.308s.

Summarize

When inserting in batches, use the insert values ​​batch method to insert, which greatly improves the execution speed.

The above is the detailed example code of MySQL batch insert in loop introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • Examples of 4 methods for inserting large amounts of data in MySQL
  • MYSQL batch insert data implementation code
  • Tutorial on implementing batch inserts in MySQL to optimize performance
  • How to avoid MySQL batch inserts with unique indexes
  • Mysql uses insert to insert multiple records to add data in batches
  • MySQL batch insert data script
  • Detailed explanation of MySQL batch SQL insert performance optimization
  • MySql batch insert optimization Sql execution efficiency example detailed explanation
  • MySQL batch inserts data through function stored procedures

<<:  Notes on configuring multiple proxies using vue projects

>>:  Solve the black screen problem after VMware installs Linux system and starts

Recommend

Introduction and use of triggers and cursors in MySQL

Trigger Introduction A trigger is a special store...

Implement a simple search engine based on MySQL

Table of contents Implementing a search engine ba...

How to call the interrupted system in Linux

Preface Slow system calls refer to system calls t...

How to use worm replication in Mysql data table

To put it simply, MySQL worm replication is to co...

How to use iostat to view Linux hard disk IO performance

TOP Observation: The percentage of CPU time occup...

Using Openlayer in Vue to realize loading animation effect

Note: You cannot use scoped animations! ! ! ! via...

Solution to the conflict between two tabs navigation in HTML

Let's start with a description of the problem...

Summary of common MySQL commands

Set change mysqlroot password Enter the MySQL dat...

Vue implements star rating with decimal points

This article shares the specific code of Vue to i...

Windows Server 2019 IIS10.0+PHP(FastCGI)+MySQL Environment Construction Tutorial

Preparation 1. Environmental Description: Operati...

Implementing a table scrolling carousel effect through CSS animation

An application of CSS animation, with the same co...

Detailed explanation of MySQL 8.0 atomic DDL syntax

Table of contents 01 Introduction to Atomic DDL 0...

How to install Linux online software gcc online

Linux online installation related commands: yum i...