当前位置:网站首页>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;
END
2、 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;
END
3、 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; END
4、 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; END
3、 ... 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
边栏推荐
- Interesting talk about performance optimization thread pool: is the more threads open, the better?
- Intelligent security of the fifth space ⼤ real competition problem ----------- PNG diagram ⽚ converter
- 【语义分割】Fully Attentional Network for Semantic Segmentation
- Briefly talk about the difference between pendingintent and intent
- The difference between asyncawait and promise
- 【数据库】数据库课程设计一一疫苗接种数据库
- [database] database course design - vaccination database
- [network design] convnext:a convnet for the 2020s
- Spring, summer, autumn and winter with Miss Zhang (5)
- 【比赛网站】收集机器学习/深度学习比赛网站(持续更新)
猜你喜欢
虚假新闻检测论文阅读(四):A novel self-learning semi-supervised deep learning network to detect fake news on...
mysql 的show profiles 使用。
Is flutter being quietly abandoned? On the future of flutter
Are you sure you know the interaction problem of activity?
【Transformer】ACMix:On the Integration of Self-Attention and Convolution
Centos7 silently installs Oracle
clion+opencv+aruco+cmake配置
FFmpeg创作GIF表情包教程来了!赶紧说声多谢乌蝇哥?
性能优化之趣谈线程池:线程开的越多就越好吗?
【图像分类】如何使用 mmclassification 训练自己的分类模型
随机推荐
Android studio login registration - source code (connect to MySQL database)
[pycharm] pycharm remote connection server
并发编程学习笔记 之 原子操作类AtomicReference、AtomicStampedReference详解
Operation commands in anaconda, such as removing old environment, adding new environment, viewing environment, installing library, cleaning cache, etc
研究生新生培训第三周:ResNet+ResNeXt
【Transformer】AdaViT: Adaptive Vision Transformers for Efficient Image Recognition
torch.nn.Parameter()函数理解
[tensorrt] convert pytorch into deployable tensorrt
Process management of day02 operation
[DL] build convolutional neural network for regression prediction (detailed tutorial of data + code)
Most PHP programmers don't understand how to deploy safe code
并发编程学习笔记 之 ReentrantLock实现原理的探究
[image classification] how to use mmclassification to train your classification model
关于Flow的原理解析
虚假新闻检测论文阅读(四):A novel self-learning semi-supervised deep learning network to detect fake news on...
Flink, the mainstream real-time stream processing computing framework, is the first experience.
【Transformer】SegFormer:Simple and Efficient Design for Semantic Segmentation with Transformers
How to obtain openid of wechat applet in uni app project
【Clustrmaps】访客统计
Detailed explanation of tool classes countdownlatch and cyclicbarrier of concurrent programming learning notes