研究MariaDB, 需要mock up一些假数据:
生成n个长度整型数的函数rand_num:
CREATE DEFINER=`root`@`localhost` FUNCTION `rand_num`(n INT) RETURNS int(5) begin DECLARE i INT DEFAULT 0; DECLARE result INT DEFAULT 0; WHILE i < n DO SET result = result*10 + FLOOR(RAND()*10); SET i = i +1; END WHILE; RETURN result; end
生成n个长度字符串的函数rand_string:
CREATE DEFINER=`root`@`localhost` FUNCTION `rand_string`(n INT) RETURNS varchar(255) CHARSET latin1 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
往表里批量插入数据的存储过程:
CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_users`(IN countNum INT(10)) BEGIN DECLARE i INT DEFAULT 0; SET autocommit = 0;/*把自动提交设为0*/ REPEAT SET i = i +1; INSERT INTO users(ID,DELETED) VALUES(rand_string(36), rand_num(1)); UNTIL i=countNum END REPEAT; COMMIT; end
最后调用存储过程: 批量插入10条数据
CALL insert_users(10)