MySQL如何利用存储过程快速生成100万条数据详解

1.在测试的时候为了测试大数据量的情况下项目的抗压能力我们通常要创造一些测试数据那么现在这个方法绝对好用

其中可能会有sql空间的报错可以自己尝试解决,这里做了分批插入,每次插入30万条,所以没有遇到类似的空间问题

首先,创建要插入100万数据的表格

set names utf8mb4;
set foreign_key_checks = 0;
-- ----------------------------
-- table structure for sdb_b2c_orders
-- ----------------------------
drop table if exists `sdb_b2c_orders`;
create table `sdb_b2c_orders`  (
`order_id` varchar(50) character set utf8 collate utf8_general_ci not null default '0' comment '订单号',
`seller_order_id` varchar(50) character set utf8 collate utf8_general_ci not null default '0' comment '商户订单号',
`total_amount` decimal(20, 3) not null default 0.000 comment '商品默认货币总值',
`final_amount` decimal(20, 3) not null default 0.000 comment '订单货币总值, 包含支付价格,税等',
`pay_status` enum('0','1','2','3','4','5') character set utf8 collate utf8_general_ci not null default '0' comment '付款状态 0-未支付 1-已支付 2-已付款至担保方 3-部分付款 4-部分退款 5-全额退款',
`ship_status` enum('0','1','2','3','4') character set utf8 collate utf8_general_ci not null default '0' comment '发货状态',
`is_delivery` enum('y','n') character set utf8 collate utf8_general_ci not null default 'y' comment '是否需要发货',
`createtime` int(10) unsigned null default null comment '下单时间',
`last_modified` int(10) unsigned null default null comment '最后更新时间',
`ectools_payments` varchar(512) character set utf8 collate utf8_general_ci null default null,
`payment` varchar(100) character set utf8 collate utf8_general_ci null default null comment '支付方式',
`shipping_id` mediumint(8) unsigned null default null comment '配送方式',
`shipping` varchar(100) character set utf8 collate utf8_general_ci null default null comment '配送方式',
`member_id` mediumint(8) unsigned null default null comment '会员用户名',
`platform_member_id` varchar(20) character set utf8 collate utf8_general_ci null default null comment '平台会员id',
`store_id` mediumint(8) unsigned null default null comment '收款门店id',
`confirm_status` enum('0','1','2','3') character set utf8 collate utf8_general_ci null default '0' comment '门店接单状态',
`confirm_time` int(10) unsigned null default null comment '接单时间',
`pickself_status` enum('0','1') character set utf8 collate utf8_general_ci null default null comment '自提状态',
`pickself_time` int(10) null default null comment '自提时间',
`pickself_id` mediumint(8) unsigned null default null comment '自提门店id',
`operator_id` mediumint(8) unsigned null default null comment '收款用户id',
`weixinscan_qrcode` varchar(200) character set utf8 collate utf8_general_ci null default null comment '微信付款码',
`alipay_qrcode` varchar(200) character set utf8 collate utf8_general_ci null default null comment '支付宝付款码',
`unionpay_qrcode` varchar(200) character set utf8 collate utf8_general_ci null default null comment '银联付款码',
`qrcode` varchar(200) character set utf8 collate utf8_general_ci null default null comment '核销码',
`promotion_type` enum('normal','prepare','recharge','cashier') character set utf8 collate utf8_general_ci not null default 'normal' comment '销售类型',
`status` enum('active','dead','finish') character set utf8 collate utf8_general_ci not null default 'active' comment '订单状态',
`confirm` enum('y','n') character set utf8 collate utf8_general_ci not null default 'n' comment '确认状态',
`ship_area` varchar(255) character set utf8 collate utf8_general_ci null default null comment '收货地区',
`ship_name` varchar(50) character set utf8 collate utf8_general_ci null default null comment '收货人',
`weight` decimal(20, 3) null default null comment '订单总重量',
`tostr` longtext character set utf8 collate utf8_general_ci null comment '订单文字描述',
`itemnum` mediumint(8) unsigned null default null comment '订单子订单数量',
`ip` varchar(15) character set utf8 collate utf8_general_ci null default null comment 'ip地址',
`ship_addr` text character set utf8 collate utf8_general_ci null comment '收货地址',
`ship_zip` varchar(20) character set utf8 collate utf8_general_ci null default null comment '收货人邮编',
`ship_tel` varchar(50) character set utf8 collate utf8_general_ci null default null comment '收货电话',
`ship_email` varchar(200) character set utf8 collate utf8_general_ci null default null comment '收货人email',
`ship_time` varchar(50) character set utf8 collate utf8_general_ci null default null comment '配送时间',
`ship_mobile` varchar(50) character set utf8 collate utf8_general_ci null default null comment '收货人手机',
`cost_item` decimal(20, 3) not null default 0.000 comment '订单商品总价格',
`is_tax` enum('true','false') character set utf8 collate utf8_general_ci not null default 'false' comment '是否要开发票',
`tax_type` enum('false','personal','company') character set utf8 collate utf8_general_ci not null default 'false' comment '发票类型',
`tax_content` varchar(255) character set utf8 collate utf8_general_ci null default null comment '发票内容',
`cost_tax` decimal(20, 3) not null default 0.000 comment '订单税率',
`tax_company` varchar(255) character set utf8 collate utf8_general_ci null default null comment '发票抬头',
`is_protect` enum('true','false') character set utf8 collate utf8_general_ci not null default 'false' comment '是否还有保价费',
`cost_protect` decimal(20, 3) not null default 0.000 comment '保价费',
`cost_payment` decimal(20, 3) null default null comment '支付费用',
`currency` varchar(8) character set utf8 collate utf8_general_ci null default null comment '订单支付货币',
`cur_rate` decimal(10, 4) null default 1.0000 comment '订单支付货币汇率',
`score_u` decimal(20, 3) not null default 0.000 comment '订单使用积分',
`score_g` decimal(20, 3) not null default 0.000 comment '订单获得积分',
`discount` decimal(20, 3) not null default 0.000 comment '订单减免',
`pmt_goods` decimal(20, 3) null default null comment '商品促销优惠',
`pmt_order` decimal(20, 3) null default null comment '订单促销优惠',
`payed` decimal(20, 3) null default 0.000 comment '订单支付金额',
`memo` longtext character set utf8 collate utf8_general_ci null comment '订单附言',
`disabled` enum('true','false') character set utf8 collate utf8_general_ci null default 'false',
`displayonsite` enum('true','false') character set utf8 collate utf8_general_ci null default 'true',
`mark_type` varchar(2) character set utf8 collate utf8_general_ci not null default 'b1' comment '订单备注图标',
`mark_text` longtext character set utf8 collate utf8_general_ci null comment '订单备注',
`cost_freight` decimal(20, 3) not null default 0.000 comment '配送费用',
`extend` varchar(255) character set utf8 collate utf8_general_ci null default 'false' comment '订单扩展',
`order_refer` varchar(20) character set utf8 collate utf8_general_ci not null default 'local' comment '订单来源',
`addon` longtext character set utf8 collate utf8_general_ci null comment '订单附属信息(序列化)',
`source` enum('pc','wap','weixin','cashier','paycode','eleme','meituan') character set utf8 collate utf8_general_ci null default 'pc' comment '平台来源',
`source_name` varchar(30) character set utf8 collate utf8_general_ci null default null comment '商圈库名',
`merchant_bn` varchar(30) character set utf8 collate utf8_general_ci null default null comment '商户号',
unique index `idx_bn`(`order_id`, `merchant_bn`) using btree,
index `ind_ship_status`(`ship_status`) using btree,
index `ind_pay_status`(`pay_status`) using btree,
index `ind_promotion_type`(`promotion_type`) using btree,
index `ind_status`(`status`) using btree,
index `ind_disabled`(`disabled`) using btree,
index `ind_last_modified`(`last_modified`) using btree,
index `ind_createtime`(`createtime`) using btree,
index `idx_order_refer`(`order_refer`) using btree,
index `idx_bn_aa`(`merchant_bn`) using btree
) engine = innodb character set = utf8 collate = utf8_general_ci comment = '订单表' row_format = dynamic;
set foreign_key_checks = 1;

创建函数

随机产生字符串

delimiter $$
create function rand_string(n int) returns varchar(255)
begin
declare chars_str varchar(100) default 'abcdefghijklmnopqrstuvwxyzabcdefjhijklmnopqrstuvwxyz';
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 $$

随机产生数字

delimiter $$
create function rand_num( ) 
returns int(5)  
begin   
declare i int default 0;  
set i = floor(100+rand()*10);  
return i;  
end $$
#假如要删除
#drop function rand_num;

创建存储过程

并向表中插入数据

#执行存储过程,往sdb_b2c_order表添加随机数据
delimiter $$
create procedure insert_sdb_b2c_orders(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 sdb_b2c_orders (order_id ,seller_order_id,total_amount,final_amount,source,source_name,merchant_bn ) values
(rand_string(10) ,rand_string(10),100,100,'eleme','square',(start+i));  
until i = max_num  
end repeat;  
commit;  
end $$;
#删除
# delimiter ;
# drop procedure insert_emp;

调用存储过程进行插入数据

#插入30万条数据分几次插入
call insert_sdb_b2c_orders(700003,300000); 

结果

 

总结

百万数据搞定,到此这篇关于mysql如何利用存储过程快速生成100万条数据的文章就介绍到这了,更多相关mysql用存储过程生成100万数据内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐