mysql自動生成大量數(shù)據(jù)
為了學習驗證高性能mysql,自動生成大量的數(shù)據(jù)做測試。內(nèi)容來源于網(wǎng)絡。
創(chuàng)建隨機數(shù)字生成
DELIMITER $$
CREATE DEFINER=`root`@`%` FUNCTION `random_num`( ) RETURNS int(5)
BEGIN
DECLARE i INT DEFAULT 0;
SET i = FLOOR(100+RAND()*10);
RETURN i;
END$$
DELIMITER ;
生成隨機字符串
DELIMITER $$
CREATE DEFINER=`root`@`%` FUNCTION `random_string`(n INT) RETURNS varchar(255) CHARSET latin1
BEGIN
DECLARE chars_str VARCHAR(100) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFJHIJKLMNOPQRSTUVWXYZ';
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()*52),1));
SET i = i + 1;
END WHILE;
RETURN return_str;
END$$
DELIMITER ;
生成隨機數(shù)字,返回varchar類型數(shù)據(jù)組合,例如手機號
DELIMITER $$
CREATE DEFINER=`root`@`%` FUNCTION `random_string_phone`(n INT) RETURNS varchar(255) CHARSET latin1
BEGIN
DECLARE chars_str VARCHAR(100) DEFAULT '1234567890';
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()*10),1));
SET i = i + 1;
END WHILE;
RETURN return_str;
END$$
DELIMITER ;
創(chuàng)建用戶表myisam引擎
CREATE TABLE `sys_user_myisam` (
`user_id` bigint(100) NOT NULL AUTO_INCREMENT,
`username` varchar(100) DEFAULT NULL COMMENT '用戶名',
`password` varchar(100) DEFAULT NULL COMMENT '密碼',
`salt` varchar(100) DEFAULT NULL COMMENT '鹽',
`email` varchar(100) DEFAULT NULL COMMENT '郵箱',
`mobile` varchar(100) DEFAULT NULL COMMENT '手機號',
`status` int(1) DEFAULT '1' COMMENT '狀態(tài) 0:禁用 1:正常',
PRIMARY KEY (`user_id`)
) ENGINE=MyISAM AUTO_INCREMENT=1000001 DEFAULT CHARSET=utf8 COMMENT='系統(tǒng)用戶MyISAM';
創(chuàng)建存儲過程生成數(shù)據(jù)
DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `insert_sys_user_myisam`(IN START INT(10),IN max_num INT(10))
BEGIN
DECLARE i INT DEFAULT 0;
SET autocommit = 0;
REPEAT
SET i = i + 1;
INSERT INTO sys_user_myisam (user_id,username,password,salt,email,mobile,status) VALUES (START+i,random_string(10),random_string(6),random_string(10),random_string(20),random_string_phone(12),1);
UNTIL i = max_num
END REPEAT;
COMMIT;
END$$
DELIMITER ;
創(chuàng)建表innodb引擎
create table sys_user_innodb ENGINE=MyISAM AUTO_INCREMENT=1000001 DEFAULT CHARSET=utf8 as select * from sys_user_myisam;
|