https://blog.youkuaiyun.com/weixin_33694620/article/details/92399062
-- 创建1kw 数据量的数据库
create database bigData;
-- 部门表
DROP TABLE IF EXISTS `dept`;
CREATE TABLE `dept` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '部门id',
`deptno` mediumint(9) NOT NULL DEFAULT '0' COMMENT '部门编号',
`dname` varchar(20) NOT NULL DEFAULT '' COMMENT '部门名称',
`loc` varchar(14) NOT NULL DEFAULT '' COMMENT '楼层',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=600001 DEFAULT CHARSET=utf8;
-- 员工表
DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '员工主键id',
`empno` mediumint(11) NOT NULL DEFAULT '0' COMMENT '员工编号',
`ename` varchar(20) NOT NULL COMMENT '员工姓名',
`job` varchar(9) NOT NULL DEFAULT '' COMMENT '工作',
`mgr` mediumint(9) NOT NULL DEFAULT '0' COMMENT '上级编号',
`hiredate` date NOT NULL COMMENT '入职时间',
`sal` decimal(7,2) DEFAULT NULL COMMENT '薪水',
`comm` mediumint(9) NOT NULL DEFAULT '0' COMMENT '红利',
`deptno` mediumint(9) NOT NULL DEFAULT '0' COMMENT '部门编号',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12387608 DEFAULT CHARSET=utf8;
创建存储函数
--创建一个指定长度的随机产生字符串的函数
delimiter $$
create function rand_string(n INT) returns VARCHAR(255)
BEGIN
DECLARE chares_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(chares_str,FLOOR(1+rand()*52),1));
set i = i+1;
end WHILE;
RETURN return_str;
end $$
select rand_string(5);
-- 创建一个产生一个大于100的随机数的函数
delimiter $$
create function rand_num() returns int(5)
BEGIN
DECLARE i int DEFAULT 0;
set i = FLOOR(100+rand()*10);
RETURN i;
end $$
select rand_num();
创建存储过程
-- 定义存储过程 用来像 emp表中插入大量数据
-- start 开始数, max_length 最大长度
delimiter $$
create PROCEDURE insert_emp(in start int(10),in max_length int(10))
BEGIN
DECLARE i int DEFAULT 0;
# set autocommit = 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(),rand_num(),400,rand_num());
UNTIL i = max_length
END REPEAT;
COMMIT; # 提交
end $$
-- 向表中插入1000万条数据
call insert_emp(1000,10000000);
-- 定义存储过程 用来像 dept表中插入大量数据
-- start 开始数, max_length 最大长度
delimiter $$
create PROCEDURE insert_dept(in start int(10),in max_length int(10))
BEGIN
DECLARE i int DEFAULT 0;
# set autocommit = 0 把自动提交关闭掉
set autocommit = 0;
REPEAT
set i = i + 1;
INSERT INTO dept(deptno,dname,loc) VALUES (rand_num(),rand_string(10),rand_string(8));
UNTIL i = max_length
END REPEAT;
COMMIT; # 提交
end $$
-- 向表中插入10万条数据
call insert_dept(1000,100000);