当前位置:网站首页>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分钟左右。感觉比较慢
边栏推荐
- Okaleido tiger logged into binance NFT on July 27, and has achieved good results in the first round
- Laravel service container (Application of context binding)
- Crypto giants all in metauniverse, and platofarm may break through
- DAO赛道异军突起,M-DAO的优势在哪里?
- Dao race track is booming. What are the advantages of m-dao?
- Sports health is deeply rooted in the hearts of the people, and move protocol leads quality life
- iSCSI vs iSER vs NVMe-TCP vs NVMe-RDMA
- SQL repair duplicate data
- Gluster cluster management analysis
- The Platonic metauniverse advocated by musk has long been verified by platofarm
猜你喜欢

Research and implementation of flash loan DAPP

突破硬件瓶颈(一):Intel体系架构的发展与瓶颈挖掘

7 月 28 日 ENS/USD 价值预测:ENS 吸引巨额利润

Laravel service container (inheritance and events)

Training log 7 of the project "construction of Shandong University mobile Internet development technology teaching website"

File permissions of day02 operation

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

centos7 静默安装oracle

MySql统计函数COUNT详解

“山东大学移动互联网开发技术教学网站建设”项目实训日志七
随机推荐
如何零代码制作深度学习的趣味app(适合新手)
Some opportunities for young people in rural brand building
Starfish OS:以现实为纽带,打造元宇宙新范式
SSM integration
熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊
完全去中心化的编程模式,不需要服务器,也不需要ip,就像一张漫无目的的网络、四处延伸
Simple optimization of interesting apps for deep learning (suitable for novices)
Ribbon学习笔记一
主流实时流处理计算框架Flink初体验。
Move protocol global health declaration, carry out the health campaign to the end
Training log III of "Shandong University mobile Internet development technology teaching website construction" project
Crypto巨头们ALL IN元宇宙,PlatoFarm或能突围
微信内置浏览器禁止缓存的问题
day02作业之进程管理
Win10+opencv3.2+vs2015 configuration
Get the number of daffodils
Changed crying, and finally solved cannot read properties of undefined (reading 'parsecomponent')
xSAN高可用—XDFS与SAN融合焕发新生命力
在uni-app项目中,如何实现微信小程序openid的获取
华为2020校招笔试编程题 看这篇就够了(上)