当前位置:网站首页>MySQL inserts millions of data (using functions and stored procedures)
MySQL inserts millions of data (using functions and stored procedures)
2022-07-29 06:05:00 【Spicy next door 4】
Case study : Using stored procedures to mysql Insert millions of data .
One 、 Create two new tables :dept,emp surface :
1、 establish dept surface :
CREATE TABLE `dept` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`deptno` mediumint unsigned NOT NULL DEFAULT '0',
`dname` varchar(20) NOT NULL DEFAULT '',
`loc` varchar(20) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=gbk;2、 establish emp surface :
CREATE TABLE `emp` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`empno` mediumint unsigned NOT NULL DEFAULT '0',
`empname` varchar(20) NOT NULL DEFAULT '',
`job` varchar(20) NOT NULL DEFAULT '',
`sal` decimal(7,2) DEFAULT NULL,
`deptno` int DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=500001 DEFAULT CHARSET=gbk;Two 、 Establish functions and stored procedures , The following functions need to be established :

1、 establish rand_str() function . This function generates a random string :
CREATE DEFINER=`root`@`%` FUNCTION `rand_str`(n int) RETURNS varchar(255) CHARSET utf8mb4
BEGIN
DECLARE
chars_str VARCHAR ( 100 ) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
DECLARE
returns_str VARCHAR ( 255 ) DEFAULT '';
DECLARE
i INT DEFAULT 0;
WHILE
i < n DO
SET returns_str = CONCAT(returns_str,SUBSTRING( chars_str, FLOOR( 1+ RAND( ) * 52 ) , 1));
SET i = i + 1;
END WHILE;
RETURN returns_str;
END2、 Establish random number function :rand_num():
CREATE DEFINER=`root`@`%` FUNCTION `rand_num`( ) RETURNS int
BEGIN
DECLARE
i INT DEFAULT 0;
SET i = FLOOR( 100+ RAND( ) * 10 );
RETURN i;
END3、 Create stored procedures :insert_dept: Insert dept Table stored procedures :
CREATE DEFINER=`root`@`%` PROCEDURE `insert_dept`( IN START INT ( 10 ), IN max_num INT ( 10 ) )
BEGIN
DECLARE i INT DEFAULT 0;
SET autocommit = 0;
REPEAT
SET i = i + 1;
INSERT into dept ( deptno, dname,loc )VALUES((START + i), rand_str(10),rand_str(6));
UNTIL i = max_num
END REPEAT;
COMMIT; END4、 Create stored procedures insert_tmp: Insert emp Table stored procedures :
CREATE DEFINER=`root`@`%` PROCEDURE `insert_tmp`( IN START INT ( 10 ), IN max_num INT ( 10 ) )
BEGIN
DECLARE i INT DEFAULT 0;
SET autocommit = 0;
REPEAT
SET i = i + 1;
INSERT into emp ( empno, empname,sal,deptno )VALUES((START + i), rand_str(6),100,rand_num());
UNTIL i = max_num
END REPEAT;
COMMIT; END3、 ... and : To test :
1、 Test insertion first dept:
CALL insert_dept(1, 10);give the result as follows :

From that , Was successfully inserted .
Then we are right emp Insert millions of data , See how long it takes .
CALL insert_tmp(1000000, 5000000)The result is :

From this we can see that , Insert 50W Data need 4 About minutes . It feels quite slow
边栏推荐
- Android studio login registration - source code (connect to MySQL database)
- 【DL】搭建卷积神经网络用于回归预测(数据+代码详细教程)
- 【CV】请问卷积核(滤波器)3*3、5*5、7*7、11*11 都是具体什么数?
- Analysis on the principle of flow
- 【DL】关于tensor(张量)的介绍和理解
- Ribbon learning notes II
- [DL] introduction and understanding of tensor
- MySql统计函数COUNT详解
- 主流实时流处理计算框架Flink初体验。
- DCAT batch operation popup and parameter transfer
猜你喜欢

Windos下安装pyspider报错:Please specify --curl-dir=/path/to/built/libcurl解决办法

Operation commands in anaconda, such as removing old environment, adding new environment, viewing environment, installing library, cleaning cache, etc

Process management of day02 operation

Synchronous development with open source projects & codereview & pull request & Fork how to pull the original warehouse

【数据库】数据库课程设计一一疫苗接种数据库

My ideal job, the absolute freedom of coder farmers is the most important - the pursuit of entrepreneurship in the future

Flink, the mainstream real-time stream processing computing framework, is the first experience.

Thinkphp6 output QR code image format to solve the conflict with debug

通过简单的脚本在Linux环境实现Mysql数据库的定时备份(Mysqldump命令备份)

SSM integration
随机推荐
[image classification] how to use mmclassification to train your classification model
【语义分割】语义分割综述
【Transformer】ATS: Adaptive Token Sampling For Efficient Vision Transformers
Isaccessible() method: use reflection techniques to improve your performance several times
GAN:生成对抗网络 Generative Adversarial Networks
Personal learning website
[database] database course design - vaccination database
ASM piling: after learning ASM tree API, you don't have to be afraid of hook anymore
FFmpeg创作GIF表情包教程来了!赶紧说声多谢乌蝇哥?
Technology that deeply understands the principle of MMAP and makes big manufacturers love it
【综述】图像分类网络
初探fastJson的AutoType
简单聊聊 PendingIntent 与 Intent 的区别
Flutter正在被悄悄放弃?浅析Flutter的未来
Detailed explanation of atomic operation class atomicinteger in learning notes of concurrent programming
ASM插桩:学完ASM Tree api,再也不用怕hook了
torch.nn.Parameter()函数理解
Operation commands in anaconda, such as removing old environment, adding new environment, viewing environment, installing library, cleaning cache, etc
Realize the scheduled backup of MySQL database in Linux environment through simple script (mysqldump command backup)
【pycharm】pycharm远程连接服务器