mysql模拟插入数据表

由于测试或者学习需要,我们要经常向一个新建的数据表插入几百万行以上的数据来验证我们的一些想法,比如索引的合理构建,表字段类型的设计等等,下面跟大家演示如何往一个新建的数据表插入N多条数据。

1.新建一个数据表,例如:

CREATE TABLE userinfo_uuid
(
uuid varchar() NOT NULL,
name varchar() NOT NULL DEFAULT '',
email varchar() NOT NULL DEFAULT '',
password varchar() NOT NULL DEFAULT '',
dob date DEFAULT NULL,
address varchar() NOT NULL DEFAULT '',
city varchar() NOT NULL DEFAULT '',
state_id tinyint unsigned NOT NULL DEFAULT '',
zip varchar() NOT NULL DEFAULT '',
country_id smallint unsigned NOT NULL DEFAULT '',
gender enum('M','F') NOT NULL DEFAULT 'M',
account_type varchar() NOT NULL DEFAULT '',
verified tinyint NOT NULL DEFAULT '',
allow_mall tinyint unsigned NOT NULL DEFAULT '',
parrent_account int unsigned NOT NULL DEFAULT '',
closest_airport varchar() NOT NULL DEFAULT '',
PRIMARY KEY(uuid),
UNIQUE KEY email (email),
KEY country_id (country_id),
KEY state_id (state_id),
KEY state_id_2 (state_id,city,address)
)ENGINE=InnoDB;

2.因为这个表有很多varchar字段,我们需要构建一个rand_string函数可以随意插入任意字符长度的字段

set global log_bin_trust_function_creators = ;

DROP FUNCTION IF EXISTS rand_string;
DELIMITER $$
CREATE FUNCTION rand_string(n INT)
RETURNS VARCHAR()
BEGIN
DECLARE chars_str varchar() DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
DECLARE return_str varchar() DEFAULT '';
DECLARE i INT DEFAULT ;
WHILE i < n DO
SET return_str = concat(return_str,substring(chars_str , FLOOR( + RAND()* ),));
SET i = i +;
END WHILE;
RETURN return_str;
END $$
DELIMITER ;

3.创建存储过程

DELIMITER $$

     DROP PROCEDURE IF EXISTS `insert_userinfo_uuid`$$

     CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_userinfo_uuid`(IN item INTEGER)
BEGIN
DECLARE counter INT;
SET counter = item;
WHILE counter >= DO
insert into userinfo (uuid,name,email,password,dob,address,city,state_id,zip,country_id,gender,account_type,verified,allow_mall,parrent_account,closest_airport)
values(uuid(),rand_string(), rand_string(), rand_string(), '2010-10-10', rand_string(), rand_string(), ceil(rand() * ), rand_string(),
ceil(rand() * ), 'M', rand_string(), , , , rand_string());
SET counter = counter - ;
END WHILE;
END$$ DELIMITER ;

4.最后调用N多次达到插入N条记录

call `insert_userinfo_uuid`();
上一篇:MyEclipse忽略js报错


下一篇:adb怎么判断是否有root权限,并更改system/app内容