当前位置:网站首页>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
边栏推荐
- 关于Flow的原理解析
- 【语义分割】Mapillary 数据集简介
- 【图像分类】如何使用 mmclassification 训练自己的分类模型
- Spring, summer, autumn and winter with Miss Zhang (4)
- [competition website] collect machine learning / deep learning competition website (continuously updated)
- 【Attention】Visual Attention Network
- isAccessible()方法:使用反射技巧让你的性能提升数倍
- 【TensorRT】将 PyTorch 转化为可部署的 TensorRT
- [clustmaps] visitor statistics
- Thinkphp6 pipeline mode pipeline use
猜你喜欢

并发编程学习笔记 之 ReentrantLock实现原理的探究

Is flutter being quietly abandoned? On the future of flutter

Detailed explanation of MySQL statistical function count

Thinkphp6 pipeline mode pipeline use

Most PHP programmers don't understand how to deploy safe code

微信小程序源码获取(附工具的下载)

nacos外置数据库的配置与使用

主流实时流处理计算框架Flink初体验。

Process management of day02 operation

【Transformer】SOFT: Softmax-free Transformer with Linear Complexity
随机推荐
Centos7 silently installs Oracle
备份谷歌或其他浏览器插件
Android studio login registration - source code (connect to MySQL database)
Flink connector Oracle CDC synchronizes data to MySQL in real time (oracle19c)
Personal learning website
Ribbon学习笔记二
【语义分割】SETR_Rethinking Semantic Segmentation from a Sequence-to-Sequence Perspective with Transformer
yum本地源制作
Detailed explanation of atomic operation classes atomicreference and atomicstampedreference in learning notes of concurrent programming
IDEA中设置自动build-改动代码,不用重启工程,刷新页面即可
How to obtain openid of wechat applet in uni app project
mysql在查询字符串类型的时候带单引号和不带的区别和原因
fastText学习——文本分类
[CV] what are the specific numbers of convolution kernels (filters) 3*3, 5*5, 7*7 and 11*11?
Spring, summer, autumn and winter with Miss Zhang (2)
day02作业之进程管理
【Transformer】TransMix: Attend to Mix for Vision Transformers
PyTorch中的模型构建
并发编程学习笔记 之 工具类Semaphore(信号量)
主流实时流处理计算框架Flink初体验。