Home > Database > Mysql Tutorial > body text

How mysql automatically generates large amounts of data

藏色散人
Release: 2020-03-25 08:44:49
forward
3357 people have browsed it

mysql automatically generates a large amount of data

In order to learn and verify high-performance mysql, a large amount of data is automatically generated for testing. The content comes from the Internet.

Recommended: "mysql video tutorial"

Create random number generation

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 ;
Copy after login

Generate random string

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 ;
Copy after login

Generate random number , returns a varchar type data combination, such as mobile phone number

DELIMITER $$
CREATE DEFINER=`root`@`%` FUNCTION `random_string_phone`(n INT) RETURNS varchar(255) CHARSET latin1
BEGIN
 DECLARE chars_str VARCHAR(100) DEFAULT &#39;1234567890&#39;;
 DECLARE return_str VARCHAR(255) DEFAULT &#39;&#39;;
 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 ;
Copy after login

Create user table myisam engine

CREATE TABLE `sys_user_myisam` (
  `user_id` bigint(100) NOT NULL AUTO_INCREMENT,
  `username` varchar(100) DEFAULT NULL COMMENT &#39;用户名&#39;,
  `password` varchar(100) DEFAULT NULL COMMENT &#39;密码&#39;,
  `salt` varchar(100) DEFAULT NULL COMMENT &#39;盐&#39;,
  `email` varchar(100) DEFAULT NULL COMMENT &#39;邮箱&#39;,
  `mobile` varchar(100) DEFAULT NULL COMMENT &#39;手机号&#39;,
  `status` int(1) DEFAULT &#39;1&#39; COMMENT &#39;状态  0:禁用   1:正常&#39;,
  PRIMARY KEY (`user_id`)
) ENGINE=MyISAM AUTO_INCREMENT=1000001 DEFAULT CHARSET=utf8 COMMENT=&#39;系统用户MyISAM&#39;;
Copy after login

Create stored procedure to generate data

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 ;
Copy after login

Create table innodb engine

create table sys_user_innodb ENGINE=MyISAM AUTO_INCREMENT=1000001 DEFAULT CHARSET=utf8 as select * from sys_user_myisam;
Copy after login

The above is the detailed content of How mysql automatically generates large amounts of data. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!