MySQL生成大量测试数据方法

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;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值