MySQL生成大量测试数据方法
0.1542018.07.15 22:25:51字数 76阅读 3580
Mysql创建测试大量测试数据
修改mysql配置
max_heap_table_size=4000M
innodb_flush_log_at_trx_commit=0
创建测试数据库
create database helloJiu charset=utf8;
use helloJiu;
创建数据表
CREATE TABLE `test_innodb` (
`id` INT (11) NOT NULL AUTO_INCREMENT,
`user_id` VARCHAR (20) NOT NULL,
`group_id` INT (11) NOT NULL,
`create_time` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `index_user_id` (`user_id`) USING HASH
) ENGINE = INNODB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;
创建随机字符串函数
delimiter $$
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 $$
delimiter ;
需要注意在创建字符串函数时可能会报错ERROR 1418
在将sql导入到mysql时候出现以下错误:
ERROR 1418 (HY000) at line 8752: This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)
经查,发现以下mysql官方文档:
http://dev.mysql.com/doc/refman/5.0/en/stored-programs-logging.html
必须设置
global log_bin_trust_function_creatorssystem variable to 1.
mysql>
SET GLOBAL log_bin_trust_function_creators = 1;
创建存储过程
delimiter $$
CREATE PROCEDURE `insert_data`(IN n int)
BEGIN
DECLARE i INT DEFAULT 1;
WHILE (i <= n ) DO
INSERT into test_innodb (user_id,group_id,create_time ) VALUEs (rand_string(20),FLOOR(RAND() * 100) ,now() );
set i=i+1;
END WHILE;
END $$
delimiter ;
调用存储过程, 插入数据
call insert_data(1000000);
插入成功后, 可以快速创建其他数据表并插入大量数据
create table test_myisam engine=myisam as select * from test_innodb;