mysql stored procedure inserts millions of data

Insert millions of data into mysql

1. Create a test table

Create department tables and employee tables:

  • Department table:
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 
  • Employee table:
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. Enable binary logging

  • View binary log status
show variables like 'log_bin_trust_function_creators'  

You can see the default binary log when it is turned off

Variable_name                   |Value |  
--------------------------------|------|  
log_bin_trust_function_creators |OFF   |  

Make a temporary setup with the following command:

set global log_bin_trust_function_creators=1 
 
Variable_name                   |Value |  
--------------------------------|------|  
log_bin_trust_function_creators |ON    |  

3. Create a function that generates a random string :

DELIMITER $$  
create FUNCTION rand_string(n int) returns varchar(255)  
BEGIN  
    declare chars_str varchar(100) default 'abcdefghijklmnoprstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';  
    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 $$  

Use the terminator of the DELIMITERdefinition statement, because there are semicolons in many places in the function. If the default terminator is not modified, the function will generate an error.mysql

Then, create a function that generates random numbers :

delimiter $$  
create function rand_num() RETURNS int(5)  
begin   
    declare i int default 0;  
    set i=floor(100+rand()*10);  
    return i;  
end $$  
  • Insert employee table function:
delimiter $$  
create procedure insert_emp(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,ename,job,mgr,hiredate,sal,comm,deptno) values((start+i),rand_string(6),'SALESMAN',0001,CURDATE(),2000,400,rand_num());  
    until i=max_num  
    end repeat;  
    commit;  
end $$  
  • Function to insert department:
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),rand_string(10),rand_string(8));  
    until i=max_num  
    end repeat;  
    commit;  
end $$  

4. Call the two insertion functions above

# 在部门表中插入数据
call insert_dept(100,10);  
+----+--------+------------+----------+  
| id | deptno | dname      | loc      |  
+----+--------+------------+----------+  
|  1 |    101 | OVnDatOMsA | BbGYWOaO |  
|  2 |    102 | PHQffkLYGl | mEgXmxza |  
|  3 |    103 | IljlEhcRXn | xvJjUSGz |  
|  4 |    104 | EwuFUElxBk | zNrtSdVl |  
|  5 |    105 | vtHaksNIb  | mdGUBVar |  
|  6 |    106 | FamifbRZyr | ljmJDQso |  
|  7 |    107 | tYLKrAAbHd | ziuuQRKZ |  
|  8 |    108 | SpXNXzEvmc | ByJZUzNo |  
|  9 |    109 | hXlpSoXPfj | HDUNcbT  |  
| 10 |    110 | sfBoRlLUlA | OtCCdPIU |  
+----+--------+------------+----------+  
# 在员工表中插入50W条数据,
mysql> call insert_emp(100001,500000);  

This enables the insertion of large amounts of data.

Query stored procedures and functions in the database

select `name` from mysql.proc where db = 'xx' and `type` = 'PROCEDURE' //存储过程
select `name` from mysql.proc where db = 'xx' and `type` = 'FUNCTION' //函数

View the creation code of the stored procedure or function

show create procedure proc_name;
show create function func_name;

Guess you like

Origin blog.csdn.net/qq_45925197/article/details/128117378