需求:最近需要在mysql数据库中造大量数据进行测试,而且要求要在某段时间内,本来想通过存储过程写,不过觉得麻烦,所以想到直接通过sql写
前提条件:业务表(sys_user_action_log )有大量的数据,你能批量写的数据不能超过业务表的数据
INSERT INTO sys_user_action_log (
seq,
ip,
url,
domain,
title,
referrer,
lang,
useragent,
sh,
sw,
cd,
account,
`action`,
`time`,
usercode
)
SELECT
REPLACE(UUID(), '-', ''),
'ip',
'http://127.0.0.1/login.do',
'domain',
'登录',
'referrer',
'zh-cn',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.2 Safari/605.1.15',
'1440',
'2560',
'24',
'System',
'基础数据_用户登录',
FROM_UNIXTIME(
UNIX_TIMESTAMP('2020-01-01 12:00:00') + FLOOR(0 + (RAND() * 31536000))
),
'admin' FROM t_user_action_log LIMIT 0,1000;
FROM_UNIXTIME( UNIX_TIMESTAMP('2020-01-01 12:00:00') + FLOOR(0 + (RAND() * 31536000)) )
:
UNIX_TIMESTAMP
函数以一个时间为基准,在0到1年的基础日期中添加随机的秒数,并转为DATETIME
31536000 = 60*60*24*365
sure , 新建存储过程也是可以的,在sqlyog,选中数据库,右键->Create->Stored Procedure…
DELIMITER $$
USE `t_base`$$
DROP PROCEDURE IF EXISTS `proc_batch_insert`$$
CREATE DEFINER=`root`@`%` PROCEDURE `proc_batch_insert`()
BEGIN
DECLARE i INT ;
SET i = 0;
WHILE
i < 1000 DO INSERT INTO t_user_action_log (
seq,
ip,
url,
domain,
title,
referrer,
lang,
useragent,
sh,
sw,
cd,
account,
`action`,
`time`,
usercode
)
VALUES
(
REPLACE(UUID(), '-', ''),
'ip',
'http://127.0.0.1/login.do',
'domain',
'登录',
'referrer',
'zh-cn',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.2 Safari/605.1.15',
'1440',
'2560',
'24',
'System',
'基础数据_用户登录',
FROM_UNIXTIME(
UNIX_TIMESTAMP('2020-01-01 12:00:00') + FLOOR(0 + (RAND() * 31536000))
),
'admin'
) ;
SET i = i+1;
END WHILE ;
END$$
DELIMITER ;
调用存储过程:
CALL proc_batch_insert();