简体   繁体   English

在MySQL中批量插入和更新多个表中的行的最佳方法

[英]Best way to bulk insert and update rows in multiple tables in mysql

I have like 10 table inserts and each insert has 1 either 1000 or even 1 lakh records and have got 4 table updates as well. 我喜欢10个表插入,每个插入有1个1000条甚至10万条记录,并且还有4条表更新。 Basically for 1 lakh records, it would be 10 lakh inserts and 4 lakh updates and this could run on an hourly basis. 基本上,对于10万条记录,将插入100万条记录,并进行40万条更新,并且每小时可以运行一次。 And the main constraint is that the whole 10 table inserts and 4 table updates has to be in one transaction. 而且主要的限制是整个10个表插入和4个表更新必须在一个事务中。

I read about several ways.For handling updates I might go with using a temporary table and and dump all the IDs which has to be updated to the table and then using a join with that temporary table from the REAL table to which the update has to be done. 我读了几种方法,为了处理更新,我可能会使用一个临时表并将所有必须更新的ID转储到该表中,然后使用与该临时表的联接从更新到的REAL表中进行操作。完成。 But I still dont have an effective solution for bulk inserting into 10 tables. 但是我仍然没有有效的解决方案来批量插入10张桌子。 It is eating up lot of time. 它正在消耗大量时间。 LOAD_DATA file is an option, but that might not be a good choice provided in realtime I might end up creating 100s of thousands of files as this operation will happen across many databases and scopes. LOAD_DATA文件是一个选项,但不是实时提供的一个不错的选择,我可能最终会创建数十万个文件,因为此操作将在许多数据库和范围内进行。 Please give your suggestions to increase the speed for these bulk inserts. 请提出您的建议,以提高这些批量插入的速度。

I am planning to change "bulk_insert_buffer_size" in mysql and any other parameters that would be useful as well. 我打算在mysql中更改“ bulk_insert_buffer_size”以及任何其他有用的参数。 So any kind of suggestion would help me a lot here 所以任何建议都会对我有很大帮助

How are you trying to do these bulk inserts? 您如何尝试进行这些批量插入? Are you using some client GUI like phpMyAdmin? 您是否正在使用某些客户端GUI(例如phpMyAdmin)? Please try to run the SQL on the MySQL client prompt. 请尝试在MySQL客户端提示符下运行SQL。

abhay> mysql -u<your_user> -h<your_host> -p < <path_to_your_sql_file> 

This should be fast enough to do all INSERT and UPDATES. 这应该足够快以执行所有INSERT和UPDATES。 Please do remember to put actual values where applicable. 请切记将实际值放在适用的地方。 And the SQL file should contain valid INSERT and UPDATE statements. 并且该SQL文件应包含有效的INSERT和UPDATE语句。

Few suggestions: 几点建议:

  1. you might try adding SET foreign_key_checks = 0 at the beginning of your SQL file and SET foreign_key_checks = 1 at the end 您可以尝试在SQL文件的开头添加SET foreign_key_checks = 0 ,并在末尾添加SET foreign_key_checks = 1
  2. if your tables have many indexes, temporarily disabling them might help. 如果您的表有很多索引,则暂时禁用它们可能会有所帮助。 Try ALTER TABLE...DISABLE KEYS and ALTER TABLE...ENABLE KEYS 尝试使用ALTER TABLE...DISABLE KEYSALTER TABLE...ENABLE KEYS
  3. You may even choose to split into multiple SQL files, one per table 您甚至可以选择拆分为多个SQL文件,每个表一个

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM