当前位置:网站首页>MYSQL-批量插入数据
MYSQL-批量插入数据
2022-08-01 00:16:00 【三月不灭】
批量插入数据
存储过程也有返回值,存储过程有一个或多个返回值,函数有且只有一个返回值
往表里插入1000w数据
1.建表
create database bigData;
use bigData;
create table dept(
id int unsigned primary key auto_increment,
deptno mediumint unsigned not null default 0,
dname varchar(20) not null default "",
loc varchar(13) not null default ""
)engine=innodb default charset=GBK;
CREATE TABLE emp(
id int unsigned primary key auto_increment,
empno mediumint unsigned not null default 0,
ename varchar(20) not null default "",
job varchar(9) not null default "",
mgr mediumint unsigned not null default 0,
hiredate date not null,
sal decimal(7,2) not null,
comm decimal(7,2) not null,
deptno mediumint unsigned not null default 0
)ENGINE=INNODB DEFAULT CHARSET=GBK;
2.设置参数log_bin_trust_function_creators
由于在做大数据插入时,mysql自身会产生一个错,要设置一个参数,二进制日志这样的一个开启的功能模块
创建函数,假如报错:This function has none of-DETERMINISTIC....
#由于开启过慢查询日志,因为我们开启了bin-log,我们就必须为我们的function指定一个
show variables like 'log_bin_trust_function_creators';
set global log_bin_trust_function_creators=1;
#这样添加了参数以后,如果mysqld重启,上述参数又会消失,永久方法:
windows下my.ini[mysqld]加上 log_bin_trust_function_creators=1
linux下letc/my.cnf下my.cnf[mysqld]加上 log_bin_trust_function_creators=1
3.创建随机函数,保证每条数据都不同
- 随机产生字符串
- 随机产生部门编号
//函数:随机产生字符串
delimiter $$
create function ran_string(n int) returns varchar(255)
begin
declare chars_str varchar(100) default 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
declare return_str varchar(255) default '';
declare i int default 0;
while i < n do
set return_str = concat(return_str,substring(chars_str,floor(1+rand()*52),1));
set i=i+1;
end while;
return return_str;
end $$
//函数:随机产生部门编号
delimiter $$
create function rand_num() returns int(5)
begin
declare i int default 0;
set i=floor(100+rand()*10);
return i;
end $$
# 如果要删除函数,则执行:drop function rand_str
4.创建存储过程
- 创建往emp表中插入数据的存储过程
- 创建往dept表中插入数据的存储过程
#存储过程:创建往emp表中插入数据的存储过程
delimiter $$ :
create procedure insert_emp(in start int(10),in max_num int(10))
begin
declare i int default 0;
set autocommit = 0; #设置自动提交为0,就不用每插入一条数据就commit,反复commit影响执行效率
repeat
set i = i+1;
insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values((start+i),ran_string(6),'salesman',0001,curdate(),2000,400,rand_num());
until i=max_num
end repeat;
commit; #自动一次性提交
end $$
#删除
# DELIMITER;
# drop PROCEDURE insert_emp;
#存储过程:创建往dept表中插入数据的存储过程
delimiter $$
create 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),ran_string(10),ran_string(8));
until i=max_num
end repeat;
commit;
end $$
#删除
#DELIMITER;
#drop PROCEDURE insert_dept;
5.调用存储过程
delimiter ; #先恢复成以;结束
CALL insert_dept(100,10);
CALL insert_emp(100,5);

当初如大量数据时,用时就会很多。select可能时间就比较长,这时候如果开了慢查询日志,超时的sql语句就会被记录到日志中
批量删除某个表上的索引
DELIMITER $$
CREATE PROCEDURE `proc_drop_index`(dbname VARCHAR(200),tablename VARCHAR(200))
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE ct INT DEFAULT 0;
DECLARE _index VARCHAR(200) DEFAULT '';
DECLARE _cur CURSOR FOR SELECT index_name FROM information_schema.STATISTICS WHERE
table_schema=dbname AND table_name=tablename AND seq_in_index=1 AND index_name <>'PRIMARY' ;
DECLARE CONTINUE HANDLER FOR NOT FOUND set done=2 ;
OPEN _cur;
FETCH _cur INTO _index;
WHILE _index<>'' DO
SET @str = CONCAT("drop index ",_index," on ",tablename );
PREPARE sql_str FROM @str ;
EXECUTE sql_str;
DEALLOCATE PREPARE sql_str;
SET _index='';
FETCH _cur INTO _index;
END WHILE;
CLOSE _cur;
END $$
#执行
CALL proc_drop_index("dbname","tablename");
边栏推荐
猜你喜欢

面试突击69:TCP 可靠吗?为什么?

虹科分享|如何用移动目标防御技术防范未知因素

TFC CTF 2022 WEB Diamand WriteUp
![[AMEX] LGBM Optuna美国运通信用卡欺诈赛 kaggle](/img/64/55af53a3d9dc1162490d613fe8a436.png)
[AMEX] LGBM Optuna美国运通信用卡欺诈赛 kaggle

如何设计高可用高性能中间件 - 作业

谷歌『云开发者速查表』;清华3D人体数据集;商汤『通用视觉框架』公开课;Web3极简入门指南;高效深度学习免费书;前沿论文 | ShowMeAI资讯日报

Network security - crack WiFi through handshake packets (detailed tutorial)

力扣二叉树

【读书笔记->数据分析】02 数据分析准备

新产品如何进行网络推广?
随机推荐
自动化机器学习pycaret: PyCaret Basic Auto Classification LightGBM
Matlab / ArcGIS 处理GPM全球月均降水数据
Weekly Summary
C# Rectangle basic usage and picture cutting
Handwritten a simple web server (B/S architecture)
信奥学习规划 信息学竞赛之路(2022.07.31)
Mysql environment installation under Linux (centos)
Binary tree traversal non-recursive program -- using stack to simulate system stack
类和对象:中
[MATLAB project combat] LDPC-BP channel coding
Compose principle - the view and the principle of two-way data binding
编程语言是什么
编译型语言和解释型语言的区别
date命令
类和对象:上
NgRx 里 first 和 take(1) 操作符的区别
The role of /etc/resolv.conf
Interview Question: Implementing Deadlocks
Flutter教程之 02 Flutter 桌面程序开发入门教程运行hello world (教程含源码)
The principle of virtual inheritance