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 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.
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 versionAlthough 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.
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:
|
>>: Negative distance (empathy) - iterative process of mutual influence
margin:auto; + position: absolute; up, down, left...
Here are the detailed steps: 1. Check the disk sp...
When using the MySQL database, if you have not lo...
Table of contents 1. async 2. await: 3. Comprehen...
1. Write a Mysql link setting page first package ...
A brief description of environment variable confi...
How to implement the "Set as homepage" ...
CSS CodeCopy content to clipboard .bottomTable{ b...
When learning kubernetes, we need to practice in ...
Problem Description When using Windows Server 201...
This article records the installation and configu...
I was woken up by a phone call early in the morni...
The previous article introduced the implementatio...
Recently, I have used React Hooks in combination ...
This article shares the MySQL free installation c...