How to quickly insert 10 million records into MySQL

How to quickly insert 10 million records into MySQL

I heard that there is an interview question: How to quickly insert 10 million records into MySQL?

I tried it privately and found that it took 0.9s to insert 10,000 data, 4.7s to insert 100,000 data, and about 58s to insert 1,000,000 data. For 1,000,000 data, my laptop took 5 minutes to run and stopped by itself. I was so excited that I was thinking about it. I used the following code:

-- Enter the database use test;
--Show all tables show tables;
-- Create the majors table create table majors(id int, major varchar(255));
-- Define the end character $
delimiter "$";
-- Create a stored procedure and define a stored method create procedure batchInsert(in args int)
begin
declare i int default 1;
-- Enable transactions (important! If not, 1 million data will take days to calculate)
start transaction;
while i <= args do
insert into majors(id,major) value(i,concat("Software Engineering-",i));
set i = i + 1;
end while;
commit;
end
$

-- Call function to generate data -- Generate 100,000 records first, input $, and press Enter to execute call batchInsert(100000);
$

It took 4.44 seconds to generate 100,000 pieces of data

insert image description here

It took 58.62 seconds to generate 1 million pieces of data, which is almost 1 minute.

insert image description here

Generate 10 million pieces of data. The big guys in front of the screen can try it. Haha, I killed the process with Ctrl+C!

insert image description here

Summarize

This concludes this article on how to quickly batch insert 10 million records into MySQL. For more information on how to batch insert data into MySQL, please search previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future!

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 batch insert data script
  • Detailed explanation of MySQL batch SQL insert performance optimization
  • Solutions to MySQL batch insert and unique index problems
  • Detailed explanation of several examples of insert and batch statements in MySQL

<<:  The whole process of upgrading Angular single project to multiple projects

>>:  Web design reference firefox default style

Recommend

How to import and export Cookies and Favorites in FireFox

FireFox is a commonly used browser with many exte...

Steps for packaging and configuring SVG components in Vue projects

I just joined a new company recently. After getti...

Multiple methods to modify MySQL root password (recommended)

Method 1: Use the SET PASSWORD command MySQL -u r...

React hooks pros and cons

Table of contents Preface advantage: shortcoming:...

Implementation of new issues of CSS3 selectors

Table of contents Basic Selector Extensions Attri...

Analysis of multi-threaded programming examples under Linux

1 Introduction Thread technology was proposed as ...

React Hooks Usage Examples

Table of contents A simple component example More...

How to use MyCat to implement MySQL master-slave read-write separation in Linux

Table of contents Linux-Use MyCat to implement My...

Implementing a distributed lock using MySQL

introduce In a distributed system, distributed lo...

Linux automatic login example explanation

There are many scripts on the Internet that use e...

MySQL multi-table query detailed explanation

Time always passes surprisingly fast without us n...

Summary of the use of vue Watch and Computed

Table of contents 01. Listener watch (1) Function...

How to install ZSH terminal in CentOS 7.x

1. Install basic components First, execute the yu...