当前位置:网站首页>MYSQL-Batch insert data
MYSQL-Batch insert data
2022-08-01 01:11:00 【The immortality of the march】
Bulk insert data
Stored procedures also have return values, stored procedures have one or more return values, and functions have one and only one return value
Insert 1000w data into the table
1. Build a table
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. Set the parameter log_bin_trust_function_creators
Since mysql itself will generate an error when inserting large data, a parameter, an enabled function module such as binary log, must be set.
Create a function, if an error is reported: This function has none of-DETERMINISTIC....#Because the slow query log is turned on, because we turned on bin-log, we must specify a function for our functionshow variables like 'log_bin_trust_function_creators';set global log_bin_trust_function_creators=1;#After adding parameters in this way, if mysqld restarts, the above parameters will disappear again, permanent method:My.ini[mysqld] under windows plus log_bin_trust_function_creators=1My.cnf[mysqld] under letc/my.cnf under linux plus log_bin_trust_function_creators=13. Create a random function to ensure that each data is different
- Randomly generated string
- Randomly generated department number
//Function: Randomly generate stringsdelimiter $$create function ran_string(n int) returns varchar(255)begindeclare chars_str varchar(100) default 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';declare return_str varchar(255) default '';declare i int default 0;while i < n doset return_str = concat(return_str,substring(chars_str,floor(1+rand()*52),1));set i=i+1;end while;return return_str;end $$//Function: Randomly generate department numberdelimiter $$create function rand_num() returns int(5)begindeclare i int default 0;set i=floor(100+rand()*10);return i;end $$# If you want to delete the function, execute: drop function rand_str4. Create a stored procedure
- Create a stored procedure to insert data into the emp table
- Create a stored procedure to insert data into the dept table
#Stored procedure: create a stored procedure that inserts data into the emp tabledelimiter $$ :create procedure insert_emp(in start int(10),in max_num int(10))begindeclare i int default 0;set autocommit = 0; #Set autocommit to 0, so there is no need to commit every time a piece of data is inserted, and repeated commits affect execution efficiencyrepeatset 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_numend repeat;commit; #Automatic one-time commitend $$#delete# DELIMITER;# drop PROCEDURE insert_emp;#Stored procedure: Create a stored procedure that inserts data into the dept tabledelimiter $$create procedure insert_dept(in start int(10),in max_num int(10))begindeclare i int default 0;set autocommit = 0;repeatset i = i+1;insert into dept(deptno,dname,loc) values((start+i),ran_string(10),ran_string(8));until i=max_numend repeat;commit;end $$#delete#DELIMITER;#drop PROCEDURE insert_dept;5. Call the stored procedure
delimiter ; #Restore to; endCALL insert_dept(100,10);CALL insert_emp(100,5);
When there is a large amount of data, it takes a lot of time.The select may take a long time. At this time, if the slow query log is enabled, the time-out sql statement will be recorded in the log.
Delete indexes on a table in batches
DELIMITER $$CREATE PROCEDURE `proc_drop_index`(dbname VARCHAR(200), tablename VARCHAR(200))BEGINDECLARE 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 WHEREtable_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<>'' DOSET @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 $$#implementCALL proc_drop_index("dbname","tablename");边栏推荐
- RTL8762DK RTC(五)
- 七月集训(第31天) —— 状态压缩
- 机器学习应该如何入门?
- Force buckle 2326, 197
- You need to know the TCP wave four times
- Daily practice of LeetCode - Circular linked list question (interview four consecutive questions)
- Exam preparation plan
- Kyoto University: Masaki Waga | Dynamic Masking for Reinforcement Learning in Black Box Environments
- What is the meaning of JS timestamp?Know SQL will consider to add a timestamp, JS timestamp for the scene?
- OSD读取SAP CRM One Order应用日志的优化方式
猜你喜欢

RTL8762DK 点灯/LED(三)

Kyoto University: Masaki Waga | Dynamic Masking for Reinforcement Learning in Black Box Environments

南方科技大学:Xiaoying Tang | AADG:视网膜图像分割领域泛化的自动增强

从零造键盘的键盘超级喜欢,IT人最爱
![[AMEX] LGBM Optuna American Express Credit Card Fraud Contest kaggle](/img/64/55af53a3d9dc1162490d613fe8a436.png)
[AMEX] LGBM Optuna American Express Credit Card Fraud Contest kaggle

机器学习初学者可以学哪些实战项目?
![ROS2 series of knowledge (4): understand the concept of [service]](/img/14/8de92a89d9c4b6476ac37408bc7788.png)
ROS2 series of knowledge (4): understand the concept of [service]
![[Microservice] Distributed Transaction Solution - Seata](/img/a8/fc6c24e4d42dfb635bad786cc02164.png)
[Microservice] Distributed Transaction Solution - Seata

MYSQL-批量插入数据

Matlab/Arcgis processing nc data
随机推荐
WebApi 打个Attribute 统一处理异常
精心总结十三条建议,帮你创建更合适的MySQL索引
STK8321 I2C (Shengjia-accelerometer) example
数据中台建设(七):数据资产管理
RTL8762DK uses DebugAnalyzer (four)
LeetCode--The problem of robbery
RTL8762DK PWM(七)
Force buckle 2326, 197
Introduction to machine learning how to?
二叉树遍历非递归程序 -- 使用栈模拟系统栈
Item 36: Specify std::launch::async if asynchronicity is essential.
Rasa 3.x 学习系列- Rasa - Issues 4898 学习笔记
MYSQL逻辑架构
Detailed explanation of TCP protocol
2022年最新重庆建筑八大员(电气施工员)模拟题库及答案
Matlab / Arcgis处理nc数据
Cmake introductory study notes
leetcode: 1562. Find latest grouping of size M [simulation + endpoint record + range merge]
Classes and Objects: Above
MYSQL主从复制