Simply record how to insert a large amount of data into the database
Create random string function rand_str
DELIMITER $$
CREATE FUNCTION rand_str(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,SUBSTR(chars_str,FLOOR(1+RAND()*52),1));
SET i = i + 1;
END WHILE;
RETURN return_str;
END
Create random number function rand_num
DELIMITER $$
CREATE FUNCTION rand_num ( ) RETURNS INT(5)
BEGIN
DECLARE i INT DEFAULT 0;
SET i = FLOOR(100+RAND()*10);
RETURN i;
END
Create a stored procedure to insert the employee table, reference in the stored procedure
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_str(6),'SALESMAN',0001,CURDATE(),2000,400,rand_num());
UNTIL i = max_num
END REPEAT;
COMMIT;
END $$
Create a stored procedure to insert the department table
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_str(10),rand_str(8));
UNTIL i = max_num
END REPEAT;
COMMIT;
END $$
Call the stored procedure, first come with a 100w
CALL insert_emp(100001,1000000);
Wuhu took off, and it took more than three minutes to insert one million records, and the contract was 190 seconds.
To the first million in my life