一、編寫工具函數(shù):
# 生成n個(gè)隨機(jī)數(shù)字
DELIMITER $$
CREATE FUNCTION randNum(n int) RETURNS VARCHAR(255)
BEGIN
DECLARE chars_str varchar(20) DEFAULT '0123456789';
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()*10 ),1));
SET i = i +1;
END WHILE;
RETURN return_str;
END $$
DELIMITER;
# 生成隨機(jī)手機(jī)號(hào)碼
# 定義常用的手機(jī)頭 130 131 132 133 134 135 136 137 138 139 186 187 189 151 157
# SET starts = 1+floor(rand()*15)*4; 截取字符串的開始是從 1、5、9、13 ...開始的。floor(rand()*15)的取值范圍是0~14
# SET head = substring(bodys,starts,3);在字符串bodys中從starts位置截取三位
DELIMITER $$
CREATE FUNCTION generatePhone() RETURNS varchar(20)
BEGIN
DECLARE head char(3);
DECLARE phone varchar(20);
DECLARE bodys varchar(100) default "130 131 132 133 134 135 136 137 138 139 186 187 189 151 157";
DECLARE starts int;
SET starts = 1+floor(rand()*15)*4;
SET head = trim(substring(bodys,starts,3));
SET phone = trim(concat(head,randNum(8)));
RETURN phone;
END $$
DELIMITER ;
# 創(chuàng)建隨機(jī)字符串和隨機(jī)時(shí)間的函數(shù)
DELIMITER $$
CREATE FUNCTION `randStr`(n INT) RETURNS varchar(255) CHARSET utf8mb4
DETERMINISTIC
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$$
DELIMITER;
二、創(chuàng)鍵普通表和內(nèi)存表
# 創(chuàng)建普通表
CREATE TABLE `test_user` (
`id` int(11) NOT NULL AUTO_INCREMENT comment '主鍵id',
`user_id` varchar(36) NOT NULL comment '用戶id',
`user_name` varchar(30) NOT NULL comment '用戶名稱',
`phone` varchar(20) NOT NULL comment '手機(jī)號(hào)碼',
`lan_id` int(9) NOT NULL comment '本地網(wǎng)',
`region_id` int(9) NOT NULL comment '區(qū)域',
`create_time` datetime NOT NULL comment '創(chuàng)建時(shí)間',
PRIMARY KEY (`id`),
KEY `idx_user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
# 創(chuàng)建內(nèi)存表
CREATE TABLE `test_user_memory` (
`id` int(11) NOT NULL AUTO_INCREMENT comment '主鍵id',
`user_id` varchar(36) NOT NULL comment '用戶id',
`user_name` varchar(30) NOT NULL comment '用戶名稱',
`phone` varchar(20) NOT NULL comment '手機(jī)號(hào)碼',
`lan_id` int(9) NOT NULL comment '本地網(wǎng)',
`region_id` int(9) NOT NULL comment '區(qū)域',
`create_time` datetime NOT NULL comment '創(chuàng)建時(shí)間',
PRIMARY KEY (`id`),
KEY `idx_user_id` (`user_id`)
) ENGINE=MEMORY DEFAULT CHARSET=utf8mb4;
三、創(chuàng)建插入內(nèi)存表數(shù)據(jù)的存儲(chǔ)過程
# 創(chuàng)建插入內(nèi)存表數(shù)據(jù)存儲(chǔ)過程 入?yún)是多少就插入多少條數(shù)據(jù)
DELIMITER $$
CREATE PROCEDURE `add_test_user_memory`(IN n int)
BEGIN
DECLARE i INT DEFAULT 1;
WHILE (i <= n) DO
INSERT INTO test_user_memory (user_id, user_name, phone, lan_id,region_id, create_time) VALUES (uuid(), randStr(20), generatePhone(), FLOOR(RAND() * 1000), FLOOR(RAND() * 100), NOW());
SET i = i + 1;
END WHILE;
END $$
DELIMITER ;
四、創(chuàng)建內(nèi)存表數(shù)據(jù)插入普通表的存儲(chǔ)過程
# 循環(huán)從內(nèi)存表獲取數(shù)據(jù)插入普通表
# 參數(shù)描述 n表示循環(huán)調(diào)用幾次;count表示每次插入內(nèi)存表和普通表的數(shù)據(jù)量
DELIMITER $$
CREATE PROCEDURE `add_test_user_memory_to_outside`(IN n int, IN count int)
BEGIN
DECLARE i INT DEFAULT 1;
WHILE (i <= n) DO
#先調(diào)用存儲(chǔ)過程往內(nèi)存表插入一萬(wàn)條數(shù)據(jù),然后再把內(nèi)存表的一萬(wàn)條數(shù)據(jù)插入普通表
CALL add_test_user_memory(count);
#一次性把內(nèi)存表的數(shù)據(jù)插入到普通表,這個(gè)過程是很快的
INSERT INTO test_user SELECT * FROM test_user_memory;
#清空內(nèi)存表數(shù)據(jù)
delete from test_user_memory;
SET i = i + 1;
END WHILE;
END $$
DELIMITER ;
五、使用示例
# 插入20000條數(shù)據(jù)
CALL add_test_user_memory_to_outside(2,10000)
注意事項(xiàng):
內(nèi)存表在存儲(chǔ)數(shù)據(jù)的時(shí)候,有可能會(huì)發(fā)生內(nèi)存溢出,可以通過調(diào)整參數(shù) tmp_table_size、max_heap_table_size這兩個(gè)參數(shù)對(duì)臨時(shí)表的大小進(jìn)行控制,對(duì)應(yīng)的sql如下:
# 查看max_heap_table_size大小
show variables like "%max_heap_table_size%";
# 設(shè)置max_heap_table_size大小為2G
set max_heap_table_size=2147483648;
# 查看tmp_table_size大小
show variables like "%tmp_table_size%";
# 設(shè)置tmp_table_size大小為2G
set tmp_table_size=2147483648;