Fast generation of millions test data on MySQL

Source: Internet
Author: User

1. The simplest method: write a stored procedure to iterate over the data

Create a function to insert:

CREATE PROCEDURE ' insert_test_size ' (in num int)
Begin
declare i int;
Set i=0;
While I<num do
INSERT into Test (A,b,word) VALUES (rand () *10,rand () *100,uuid ());
Set i=i+1;
End while;
End

This method can be added on the millions data, but the time spent is also very large, the implementation of 1W insertion takes time to 9.827s that is, if it is 100W data, it takes more than 15 minutes.

2, the other is to optimize the INSERT statement, directly from the table seconds randomly take a few data inserted into the table data is much faster.

BEGIN
declare i int;
Set i=0;
While I<num do
INSERT into City (cityname,citypinyin) Select Cityname,citypinyin from City WHERE id>= (the Select Floor (RAND () * (select MAX (ID) from city)), ORDER by ID LIMIT 1000;
Set i=i+1;
End while;
END

Inserting 1oW of data takes 14.358s faster than the first method by nearly 85%.

Fast generation of millions test data on MySQL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.