##########################存储过程没有办法跟其他语句一起一句一句运行,要么全部运行,要么单独运行存储过程!!
drop procedure if exists salesAdd0;
CREATE TABLE t_sales12
(
id
int(11) NOT NULL AUTO_INCREMENT,
username
varchar(32) COLLATE utf8_bin DEFAULT NULL COMMENT ‘用户名’,
password
varchar(64) COLLATE utf8_bin DEFAULT NULL COMMENT ‘密码 MD5存储’,
register_time
timestamp NULL DEFAULT NULL COMMENT ‘注册时间’,
type
int(1) DEFAULT NULL COMMENT ‘用户类型 1,2,3,4 随机’,
PRIMARY KEY (id
),
KEY idx_username
(username
) USING BTREE
);
DELIMITER //
create procedure salesAdd0()
begin
declare i int default 11;
while i <= 4000 do
insert into testdb.t_sales12
(username
,password
,register_time
,type) values
(concat(“jack”,i),MD5(concat(“psswe”,i)),from_unixtime(unix_timestamp(now()) - floor(rand() * 800000)),floor(1 + rand() * 4));
set i = i + 1;
end while;
end //
DELIMITER ;
call salesAdd0();
改进版
虽然使用存储过程添加数据相对一个个添加更加便捷,快速,但是添加几百万数据要花几个小时时间也是很久的,后面在网上找到不少资料,发现mysql每次执行一条语句都默认自动提交,这个操作非常耗时,所以在在添加去掉自动提交。设置 SET
drop procedure if exists salesAdd2;
CREATE TABLE t_sales
(
id
int(11) NOT NULL AUTO_INCREMENT,
username
varchar(32) COLLATE utf8_bin DEFAULT NULL COMMENT ‘用户名’,
password
varchar(64) COLLATE utf8_bin DEFAULT NULL COMMENT ‘密码 MD5存储’,
register_time
timestamp NULL DEFAULT NULL COMMENT ‘注册时间’,
type
int(1) DEFAULT NULL COMMENT ‘用户类型 1,2,3,4 随机’,
PRIMARY KEY (id
),
KEY idx_username
(username
) USING BTREE
)
DELIMITER //
create procedure salesAdd2()
begin
declare i int default 1;
set autocommit = 0;
while i <= 400 do
insert into testdb.t_sales
(username
,password
,register_time
,type) values
(concat(“jack”,i),MD5(concat(“psswe”,i)),from_unixtime(unix_timestamp(now()) - floor(rand() * 800000)),floor(1 + rand() * 4));
set i = i + 1;
end while;
set autocommit = 1;
end //
DELIMITER ;
call salesAdd2();