当前位置:网站首页>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");
边栏推荐
- Daily--Kali opens SSH (detailed tutorial)
- Pylint检查规则中文版
- 微信小程序之小程序页面语法
- lua入门案例实战123DIY
- When can I use PushGateway
- vim的基本使用-命令模式
- Google "Cloud Developer Quick Checklist"; Tsinghua 3D Human Body Dataset; SenseTime "Universal Vision Framework" open class; Web3 Minimalist Getting Started Guide; Free Books for Efficient Deep Learni
- 【MATLAB项目实战】LDPC-BP信道编码
- Basic use of vim - bottom line mode
- [AMEX] LGBM Optuna美国运通信用卡欺诈赛 kaggle
猜你喜欢
随机推荐
Carefully summarize thirteen suggestions to help you create more suitable MySQL indexes
2022年最新重庆建筑八大员(电气施工员)模拟题库及答案
Qlib quantitative source analysis: qlib/qlib/contrib/model/GBDT py
Southern University of Science and Technology: Xiaoying Tang | AADG: Automatic Enhancement for Generalization in the Field of Retinal Image Segmentation
Xinao Learning Plan The Road to Informatics Competition (2022.07.31)
cobaltstrike
Usage of mysql having
Flutter教程之 02 Flutter 桌面程序开发入门教程运行hello world (教程含源码)
消息队列消息存储设计(架构实战营 模块八作业)
What is customer profile management?
Difference Between Stateless and Stateful
自动化机器学习pycaret: PyCaret Basic Auto Classification LightGBM
Google "Cloud Developer Quick Checklist"; Tsinghua 3D Human Body Dataset; SenseTime "Universal Vision Framework" open class; Web3 Minimalist Getting Started Guide; Free Books for Efficient Deep Learni
Compose principle - the view and the principle of two-way data binding
mysql having的用法
SQL注入 Less54(限制次数的SQL注入+union注入)
高等代数_证明_任何矩阵都相似于一个上三角矩阵
Basic use of vim - bottom line mode
Classes and Objects: Above
qlib量化源码分析:qlib/qlib/contrib/model/gbdt.py




![[Microservice] Distributed Transaction Solution - Seata](/img/a8/fc6c24e4d42dfb635bad786cc02164.png)




