mysql在windows创建大量测试数据

107 阅读1分钟

要创建的的表是:

CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `c_user_id` varchar(36) NOT NULL DEFAULT '',
  `c_name` varchar(22) NOT NULL DEFAULT '',
  `c_province_id` int(11) NOT NULL,
  `c_city_id` int(11) NOT NULL,
  `create_time` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_user_id` (`c_user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

1.创建临时表

CREATE TABLE tmp_table (
	id INT,
	PRIMARY KEY (id)
);

2.生成数据文件

因为是windows,没法运行shell脚本(sh结尾的文件),所以可以写完shell,然后用git的bash运行。
shell脚本内容: 在git 的bash运行下面的 qq.sh,执行sh C:\Users\zhong\Desktop\qq.sh

i=1;while [ $i -le 2000000 ];do echo $i ;let i+=1; done >base.txt

3.将文件中数据插入到临时表中,在mysql中执行

load data infile 'C:\Users\zhong\Desktop\base.txt' replace into table tmp_table;

mysql报错:1290 - The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

show variables like '%secure%'

需要再my.ini中设置secure_file_priv=空,需要再mysqld节点下,而且my.ini要放到mysql解压的根目录下,不要放到bin下
4.200万记录大概17s时间
5.插入目标表随机值

INSERT INTO t_user SELECT
id,
uuid( ),
CONCAT( 'userNickName', id ),
FLOOR( Rand( ) * 1000 ),
FLOOR( Rand( ) * 100 ),
NOW( ) 
FROM
	tmp_table;

200万数据大概用时78s