Tips for batch data update in mysql

Source: Internet
Author: User

Sometimes you need to manually repair or update data during development. Generally, you can write a main function in your project to read or modify the data, however, you need to connect to the Internet from the local device during execution,

If the data volume is large. If the number is hundreds of thousands or more, the performance will be very poor. At this time, it will be much better to write SQL statements in the database for direct execution.

In the innodb engine of the mysql database, General Data addition, deletion, and modification all have built-in transactions, and there is a default timeout limit, which seems to be 50 s, if the data volume is large and many connection queries time out,

In this case, you may want to create a temporary table and index the temporary table, and then add or modify the data to join the temporary table. The performance will be much faster at this time. For example:

Drop temporary table if exists tmp;

Create temporary table tmp
Select B. UserId, B. field1, B. field2, B. field3 from
(Select Max (AutoId) as AutoId
From T_Table2 where UserId
Group by UserId) a left join T_Table1 B on a. AutoId = B. AutoId;
Alter table tmp add index 'index _ userid' ('userid' ASC );

Insert into T_Table3 ('userid', 'field1', 'field2', 'field3', 'createtime', 'createip', 'modifytime', 'modifyip ')
Select a. UserId, a. field1, a. field2, a. field3, now (), '192. 0.0.1 ', now (), '192. 0.0.1'
From tmp;

Drop temporary table tmp;

Related Article

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.