Mysql uses stored procedures to quickly add millions of data sample code

Mysql uses stored procedures to quickly add millions of data sample code

Preface

In order to reflect the difference between adding and not adding indexes, we need to use millions of data. However, if we add millions of data to a table one by one, it is very cumbersome and troublesome. Here we use stored procedures to quickly add data, which takes about 4 hours.
Create a user table

CREATE TABLE `t_sales` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(32) COLLATE utf8_bin DEFAULT NULL COMMENT 'Username',
  `password` varchar(64) COLLATE utf8_bin DEFAULT NULL COMMENT 'Password MD5 storage',
  `register_time` timestamp NULL DEFAULT NULL COMMENT 'Registration time',
  `type` int(1) DEFAULT NULL COMMENT 'User type 1,2,3,4 random',
  PRIMARY KEY (`id`),
  KEY `idx_username` (`username`) USING BTREE
)

Then create a stored procedure to add data in batches.

  • Username is a combination of constants and numbers
  • The password is an MD5 password
  • The registration time is a random number of days forward from the current time.
  • type is a random value in the range of 1-4
create procedure salesAdd()
begin 
 declare i int default 11;
   while i <= 4000000 do
         insert into blog.t_sales
         (`username`,`password`,`register_time`,type) values
         (concat("jack",i),MD5(concat("psswe",i)),from_unixtime(unix_timestamp(now()) - floor(rand() * 800000)),floor(1 + rand() * 4)); 
         set i = i + 1; 
   end while; 
end

Then call the stored procedure

call salesAdd()

Improved version

Although using stored procedures to add data is more convenient and faster than adding data one by one, it takes several hours to add millions of data. Later, I found a lot of information on the Internet and discovered that MySQL automatically commits each time it executes a statement. This operation is very time-consuming, so I added and removed the automatic commit. SET AUTOCOMMIT = 0;

create procedure salesAdd()
begin 
 declare i int default 1;
 set autocommit = 0;   
   while i <= 4000000 do
         insert into blog.t_sales
         (`username`,`password`,`register_time`,type) values
         (concat("jack",i),MD5(concat("psswe",i)),from_unixtime(unix_timestamp(now()) - floor(rand() * 800000)),floor(1 + rand() * 4)); 
         set i = i + 1; 
   end while;
 set autocommit = 1;     
end

The execution time is 387 seconds, which is about six minutes, and half of the time is used for md5 and random number calculations.

[SQL]
call salesAdd();
Affected rows: 0
Time: 387.691s

This is the end of this article about how to quickly add millions of data using stored procedures in MySQL. For more information about how to add millions of data in MySQL, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySQL spatial data storage and functions
  • Comparison of storage engines supported by MySQL database
  • How to use stored procedures in MySQL to quickly generate 1 million records
  • In MySQL database, datetime, bigint, and timestamp are used to represent time selection. Which one is the most efficient for storing time?
  • MySQL Series 7 MySQL Storage Engine
  • MySQL series five views, stored functions, stored procedures, triggers
  • Detailed explanation of MYSQL stored procedure comments

<<:  Detailed explanation of the process of deploying the distributed configuration center Apollo with one click using docker compose

>>:  Negative distance (empathy) - iterative process of mutual influence

Recommend

A simple method to implement scheduled backup of MySQL database in Linux

Here are the detailed steps: 1. Check the disk sp...

Linux MySQL root password forgotten solution

When using the MySQL database, if you have not lo...

How to use async and await in JS

Table of contents 1. async 2. await: 3. Comprehen...

IDEA complete code to connect to MySQL database and perform query operations

1. Write a Mysql link setting page first package ...

HTML set as homepage and add to favorites_Powernode Java Academy

How to implement the "Set as homepage" ...

How to use html css to control div or table to be fixed in a specified position

CSS CodeCopy content to clipboard .bottomTable{ b...

How to build a standardized vmware image for kubernetes under rancher

When learning kubernetes, we need to practice in ...

MySQL 8.0.18 installation and configuration method graphic tutorial (linux)

This article records the installation and configu...

Kill a bunch of MySQL databases with just a shell script like this (recommended)

I was woken up by a phone call early in the morni...

Promise encapsulation wx.request method

The previous article introduced the implementatio...

MySQL free installation version configuration tutorial

This article shares the MySQL free installation c...