Prepare test data with insert into select in MySql

        The requirement is that I want to repeatedly insert a large amount of data, so the first thing that comes to my mind is to use the insert into select statement. First, back up the data of the current table.

create table table_name_bak as select * from table_name;

        Then, add the data from the standby table to the original table, and write a stored procedure as follows:

drop procedure if exists initTestData;

DELIMITER //
create procedure initTestData()
begin
    declare i int default 1;
    while(i<=100) do
		insert into table_name
		select
		   replace(uuid(), '-',''),
		   CONCAT(FLOOR(2010 + (RAND() * 5)),'-',LPAD(FLOOR(1 + (RAND() * 12)),2,0),'-',LPAD(FLOOR(1 + (RAND() * 28)),2,0), ' ', LPAD(FLOOR(0 + (RAND() * 24)),2,0),':',LPAD(FLOOR(0 + (RAND() * 60)),2,0),':',LPAD(FLOOR(0 + (RAND() * 60)),2,0)),
		   update_time
		from table_name_bak;
	set i=i+1;
    END while;
END  //

        Call this stored procedure.

## call this stored procedure
call initTestData();

 

Special Note:

        1. The primary key conflict is resolved with replace(uuid(), '-', '') to generate a unique value in the "table".

        2. Because the time required for business test points is random, at least not the same, in order to simulate the data of the production environment, special use CONCAT(FLOOR(2010 + (RAND() * 5)),'-',LPAD(FLOOR (1 + (RAND() * 12)),2,0),'-',LPAD(FLOOR(1 + (RAND() * 28)),2,0), ' ', LPAD(FLOOR(0 + (RAND() * 24)),2,0),':',LPAD(FLOOR(0 + (RAND() * 60)),2,0),':',LPAD(FLOOR(0 + (RAND () * 60)), 2, 0)) Randomly generate date data between 2010 and 2015.

 

Attachment: mySql takes random date FLOOR and LPAD within a certain range

# Take random integers from 7 to 12, including 7 to 12
SELECT FLOOR(7 + (RAND() * 6));

# Take random integers from 12 to 24, including 12 to 24
SELECT FLOOR(12+(rand()*13));

# Take a random number between 0-23, including 0 to 23
SELECT FLOOR(0 + (RAND() * 23));

#LPAD(str,len,padstr), returns the string str, and pads the left with the string padstr to the length of len characters. If str is longer than len, the return value is shortened to len characters (ie, cannot exceed len)
SELECT LPAD('HI', 4 , '?'); #Running result: ??HI
SELECT LPAD('HELLO', 4 , '?'); #Running result: HELL

# Take a random number between 0-24, if it is not enough for 2 digits, add 0
SELECT LPAD(FLOOR(0 + (RAND() * 23)),2,0);

#Get a random date, the year is 2000-2015
select CONCAT(FLOOR(2000 + (RAND() * 16)),'-',LPAD(FLOOR(1 + (RAND() * 12)),2,0),'-',LPAD(FLOOR(3 + (RAND() * 28)),2,0));

# get a random time
select CONCAT(LPAD(FLOOR(0 + (RAND() * 24)),2,0),':',LPAD(FLOOR(0 + (RAND() * 60)),2,0),':',LPAD(FLOOR(0 + (RAND() * 60)),2,0));

#Get a random time with a year between 2000-2016, the format is: yyyy-MM-dd hh24:mi:ss
select CONCAT(FLOOR(2000 + (RAND() * 16)),'-',LPAD(FLOOR(1 + (RAND() * 12)),2,0),'-',LPAD(FLOOR(3 + (RAND() * 28)),2,0), ' ', LPAD(FLOOR(0 + (RAND() * 24)),2,0),':',LPAD(FLOOR(0 + (RAND() * 60)),2,0),':',LPAD(FLOOR(0 + (RAND() * 60)),2,0));

 

Attachment: How to deal with too many table fields

1. First enter the database, execute show create table table_name;
2. Copy all fields of the table
3. Exit the database, create a temporary file, and save the copied content to this temporary file
4. Use Linux commands to process fields as comma-separated, cat file_name | awk -F '`' '{print $2}' | tr '\n' ','
5. Organize SQL according to the following raw SQL and comma-separated fields: insert into table_name() select from table_name_bak;
6. Modify the table name, paste the fields, and finally replace the primary key and unique index field values ​​with replace(uuid(), '-',''), and use CONCAT(FLOOR(2010 + (RAND() * 5)),' -',LPAD(FLOOR(1 + (RAND() * 12)),2,0),'-',LPAD(FLOOR(1 + (RAND() * 28)),2,0), ' ', LPAD(FLOOR(0 + (RAND() * 24)),2,0),':',LPAD(FLOOR(0 + (RAND() * 60)),2,0),':',LPAD( FLOOR(0 + (RAND() * 60)),2,0)) replaces the time field value to replace.

 

Reference article: http://blog.csdn.net/angus_17/article/details/8020833

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326801399&siteId=291194637