Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of inserting tens of millions of data into mysql Database

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

How does mysql implement circular insertion of tens of millions of levels of data?

1. Build a table:

CREATE TABLE `mysql_ genarate` (`id` int (11) NOT NULL AUTO_INCREMENT, `uuid` varchar (50) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=5990001 DEFAULT CHARSET=utf8

two。 Create stored procedures that are inserted in a loop

Create procedure test_two1 () begin declare i int default 0; while I < 3000 do INSERT into mysql_genarate (uuid) VALUES (UUID ()); set I = I + 1; end while; end #

Using call test_two1 (); testing, 3000 pieces of data took 74 seconds, which would be unbearable for tens of millions of data.

So I looked for an optimization method on the Internet and found that the sql statements inserted in batches could be spliced together, and the speed was greatly improved.

3. Optimized stored procedure

CREATE PROCEDURE insertPro (in sum INT) BEGINDECLARE count INT DEFAULT 0TracterDECLARE i INT DEFAULT 0Shiset @ exesql = concat ("insert into mysql_genarate (uuid) values"); set @ exedata = ""; set count=0;set iDeposit while count0 then set @ exedata = SUBSTRING (@ exedata, 2); set @ exesql = concat ("insert into mysql_genarate (uuid) values", @ exedata); prepare stmt from @ exesql; execute stmt; DEALLOCATE prepare stmt;end if;end

Calling call insertPro (3000) takes a few tenths of a second, which is an acceptable speed.

Then call call insertPro (30000000); test 30 million data inserts, and the result takes 1824.203s (30 minutes). The speed of the personal computer is enough.

In addition, java multithread is used to assemble sql at the same time, and every 10000 threads are submitted once. In the case of 8 threads running at the same time, 30 million data inserts take 336 seconds, and 100 million data inserts take 1087 seconds.

These are the details of how mysql implements circular insertion of tens of millions of levels of data, please pay attention to other related articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report