赞
踩
创建随机字符串函数 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
创建随机数字函数 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
创建插入员工表存储过程,在存储过程中引用
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 $$
创建插入部门表存储过程
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 $$
调用存储过程,先来个100w的
CALL insert_emp(100001,1000000);
芜湖起飞,插入一百万条记录一共花了三分多钟,合约190秒。
敬人生中的第一个一百万
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。