快速生成mysql上百万条测试数据
方案:编写一个存储过程循环添加数据
1. 创建表index_test
DROP TABLE IF EXISTS index_test; CREATE TABLE index_test( id BIGINT(20) PRIMARY KEY NOT NULL AUTO_INCREMENT, USER VARCHAR(16) DEFAULT NULL, psd varchar(64) default null /*psd mediumint DEFAULT 0 存储随机数据*/ )ENGINE=MyISAM DEFAULT CHARSET=utf8;
2.创建存储过程
DELIMITER $$ DROP PROCEDURE IF EXISTS `insert_data`$$ CREATE PROCEDURE `insert_data`(IN tableName varchar(500),IN num INT) BEGIN DECLARE n INT DEFAULT 1;/*定义一个变量,存储当前执行的次数*/ WHILE n <= num DO SET @sqlStr = CONCAT("INSERT INTO ",tableName,"(USER,psd) VALUES(CONCAT(",n,",'用户'),password(",n,"))"); PREPARE stmt FROM @sqlStr; EXECUTE stmt; set n=n+1; end while; END $$
3.执行存储过程插入数据:
call insert_data('index_test',1000000);
所花时间