-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_批量新增存储过程.sql
40 lines (39 loc) · 2.04 KB
/
01_批量新增存储过程.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
-- for mysql
-- 通过生成动态SQL
-- account表结构参考 01_批量新增存储过程.sql里面的备注
-- SQL模板: insert into [table_name] ([columns]) ([values1])([values2])([values3])
-- 经过测试,动态sql里面的生成随机值的函数直接放在SQL里面,批次执行效率高,差距大概2s内。批次执行在2000-3000范围内效率最高,这个值可能会受实际SQL长度影响。
delimiter $$
CREATE PROCEDURE proc_insert_account_batch(in batch_num int(12))
BEGIN
DECLARE tmp_sql LONGTEXT;
-- 设置批次提交的记录数,批次提交能减少与数据库的交互,提高性能
SET @tmp_batch = 3000;
-- 定义批次执行SQL语言的前面相同部分,可以复用
SET @tmp_base_sql = 'insert into account(`id`, `name`, pwd, salt, gender, mobile, `state`, email, create_time, update_time, `uid`, last_login_time,last_login_ip,login_count) values ';
-- 定义批次执行的SQL
SET @tmp_sql = @tmp_base_sql;
SET @tmp_index = 0;
-- 设置事务自动提交
SET autocommit = 1;
SET @i = 0;
-- 循环生成批次SQL的字段取值
while @i < batch_num do
SET @tmp_sql = concat(@tmp_sql, '(NULL,\'', rand_chinese_name(), '\',\'', rand_str(8), '\',\'', rand_str(6), '\',', rand_dict('0,1,2'), ',\'', rand_mobile(), '\',\'', rand_dict('0,1'),'\',\'', concat(rand_str(6), '@qq.com'), '\',\'', rand_date('2010-01-01 00:00:00', now()), '\',NULL,replace(uuid(), \'-\', \'\'),NULL,\'', rand_ip(), '\',', rand_range_num(0,1000), '),');
SET @i = @i + 1;
IF mod(@i, @tmp_batch) = 0 THEN
SET @tmp_sql = LEFT(@tmp_sql, CHAR_LENGTH(@tmp_sql) - 1);
-- 编译SQL
prepare stmt from @tmp_sql;
-- 执行SQL
execute stmt;
-- 生成下一个批次SQL
SET @tmp_sql = @tmp_base_sql;
END IF;
END while;
IF length(@tmp_sql) > length(@tmp_base_sql) THEN
SET @tmp_sql = LEFT(@tmp_sql, CHAR_LENGTH(@tmp_sql) - 1);
prepare stmt from @tmp_sql;
execute stmt;
END IF;
END $$