使用Mysql 存储过程,批量插入100万条数据

将要进行批量插入的sql语句:

INSERT INTO `cloud2_activity`.`c_shop_barcode_out_flow` (
    `barcode`,
    `shop_user_id`,
    `materials_id`,
    `materials_style`,
    `materials_name`,
    `point`,
    `phone`,
    `create_date`,
    `province`,
    `city`,
    `rate`
)
VALUES
    (
        '0kIsLvz9YcnuQ285',
        '2',
        '13',
        '1',
        'test',
        '40',
        '',
        '2017-01-20 17:29:28',
        '局域网',
        '其他地区',
        '0.00'
    );

python生成随机字符串:http://www.cnblogs.com/longsecond/p/6118943.html

创建mysql函数,输入为:n即字符串的长度, 输出为:长度为n的随机字符串:

CREATE FUNCTION rand_string(n INT) RETURNS VARCHAR(255)
BEGIN
DECLARE chars_str varchar(100) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
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()*62 ),1));
SET i = i +1;
END WHILE;
RETURN return_str;
END

创建mysql存储过程: (declare语句用法介绍: http://blog.csdn.net/superbfly/article/details/12612037)

delimiter //
create procedure kmind()
begin
    declare i int;  # declare语句是在复合语句中声明变量的指令
    set i=1;
 
    while i<=1000000 do
        insert into `cloud2_activity`.`c_shop_barcode_out_flow`(`barcode`,
    `shop_user_id`,
    `materials_id`,
    `materials_style`,
    `materials_name`,
    `point`,
    `phone`,
    `create_date`,
    `province`,
    `city`,
    `rate`) values(rand_string(16), # 使用函数:rand_string(16)
        '2',
        '13',
        '1',
        'test',
        '40',
        '',
        '2017-01-20 17:29:28',
        '局域网',
        '其他地区',
        '0.00');
        set i=i+1;
    end while;
end
// 
delimiter ;

CALL kmind() # call 调用存储过程
drop procedure kmind; # 删除mysql存储过程
drop function rand_string; # 删除mysql函数

JDBC 批量插入Mysql 字段值采用随机字符 100万条数据: http://blog.csdn.net/fantasy0126/article/details/41897827

你可能感兴趣的:(使用Mysql 存储过程,批量插入100万条数据)