当前位置:网站首页>mysql插入百万数据(使用函数和存储过程)
mysql插入百万数据(使用函数和存储过程)
2022-07-29 05:20:00 【麻辣隔壁4】
案例:利用存储过程向mysql插入百万条数据。
一、新建俩张表:dept,emp表:
1、建立dept表:
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、建立emp表:
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;二、建立函数和存储过程,需要建立以下函数:

1、建立rand_str()函数。此函数产生随机字符串:
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、建立随机数函数: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、建立存储过程:insert_dept:插入dept表的存储过程:
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、建立存储过程insert_tmp:插入emp表的存储过程:
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三:进行测试:
1、先测试插入dept:
CALL insert_dept(1, 10);结果如下:

由此看出,是成功的进行了插入。
接着我们对emp进行上百万条数据插入,看看需要花多长时间。
CALL insert_tmp(1000000, 5000000)结果为:

由此可看到,插入50W条数据需要4分钟左右。感觉比较慢
边栏推荐
- 剑指核心-TaoCloud全闪SDS助力构建高性能云服务
- 与张小姐的春夏秋冬(2)
- Some opportunities for young people in rural brand building
- DAO赛道异军突起,M-DAO的优势在哪里?
- Crypto giants all in metauniverse, and platofarm may break through
- Super simple integration of HMS ml kit to realize parent control
- D3.JS 纵向关系图(加箭头,连接线文字描述)
- 【go】defer的使用
- 并发编程学习笔记 之 ReentrantLock实现原理的探究
- The Platonic metauniverse advocated by musk has long been verified by platofarm
猜你喜欢

IDEA使用JDBC连接MySQL数据库个人详细教程

性能对比|FASS iSCSI vs NVMe/TCP

Okaleido tiger logged into binance NFT on July 27, and has achieved good results in the first round

"Shandong University mobile Internet development technology teaching website construction" project training log V

How to survive in the bear market of encryption market?

Gluster集群管理小分析

Process management of day02 operation

完全去中心化的编程模式,不需要服务器,也不需要ip,就像一张漫无目的的网络、四处延伸

Android Studio 实现登录注册-源代码 (连接MySql数据库)

运动健康深入人心,MOVE PROTOCOL引领品质生活
随机推荐
C# 判断用户是手机访问还是电脑访问
“山东大学移动互联网开发技术教学网站建设”项目实训日志三
Fantom (FTM) 价格将在未来几天飙升 20%
打印出1-100之间的所有质数
Breaking through the hardware bottleneck (I): the development of Intel Architecture and bottleneck mining
Read and understand move2earn project - move
iSCSI vs iSER vs NVMe-TCP vs NVMe-RDMA
全闪分布式,如何深度性能POC?
Okaleido tiger logged into binance NFT on July 27, and has achieved good results in the first round
华为2020校招笔试编程题 看这篇就够了(上)
Win10+opencv3.2+vs2015 configuration
Intelligent security of the fifth space ⼤ real competition problem ----------- PNG diagram ⽚ converter
并发编程学习笔记 之 ReentrantLock实现原理的探究
The difference between link and @import importing external styles
性能对比|FASS iSCSI vs NVMe/TCP
Gluster集群管理小分析
Performance comparison | FASS iSCSI vs nvme/tcp
centos7 静默安装oracle
Sports health is deeply rooted in the hearts of the people, and move protocol leads quality life
How to survive in the bear market of encryption market?