简体   繁体   English

如何像MyISAM一样快速地将数据导入InnoDB表

[英]How to import data into InnoDB tables as fast as MyISAM

I am new to MySQL and I know very little about it. 我是MySQL的新手,我对它知之甚少。

The problem I am facing is as mentioned below: 我面临的问题如下所述:

Previously I had data which had MyISAM as its MySQL engine and it used to take around 15 minutes to get imported on any machine. 以前我的数据有MyISAM作为它的MySQL引擎,过去需要大约15分钟才能在任何机器上导入。 However, when I change the engine to InnoDB it's taking almost 90 minutes to get imported on any machine. 但是,当我将引擎更改为InnoDB时,需要花费大约90分钟才能在任何机器上导入。 Kindly suggest me for the same. 请建议我同样的。

Innodb tables are faster for concurrent inserts. 对于并发插入,Innodb表更快。 See Innodb Performance Optimization Basics 请参阅Innodb性能优化基础知识

For best performance, you need to tune INNODB engine in my.cnf file. 为获得最佳性能,您需要在my.cnf文件中调整INNODB引擎。

Assuming, if you have 4GB RAM then try configuring my.cnf file as: 假设,如果您有4GB RAM尝试将my.cnf文件配置为:

#Innodb
innodb_buffer_pool_size = 1G
innodb_additional_mem_pool_size = 512M
innodb_log_file_size = 1G
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 30
innodb_file_per_table=1
innodb_file_format=barracuda
innodb_strict_mode=1
innodb_autoinc_lock_mode = 0

Then restart the MySQL server. 然后重启MySQL服务器。

@Omesh has the some great tips & I think were part of the reason my latest imports of 4.5GB & 5.0GB SQL of 36 million rows were successful. @Omesh有一些很棒的提示,我认为这是我最新导入的4.5GB和5.0GB SQL的3600万行成功的部分原因。 I wanted to get one spot where some other additional tips helped. 我想得到一个其他一些额外提示有帮助的地方。

  1. Follow @Omesh MySQL configuration suggestions. 关注@Omesh MySQL配置建议。

  2. Next, look at the actual SQL file you're attempting to import. 接下来,查看您尝试导入的实际SQL文件。 The tool we used to dump the data added these lines right before the INSERT statement: 我们用于转储数据的工具在INSERT语句之前添加了这些行:

    /*!40000 ALTER TABLE `table_name` DISABLE KEYS */;

    What I did was uncomment that line. 我所做的是取消注释。 It disables the checking of the primary keys during insert, allowing it to go faster. 它在插入期间禁用主键检查,使其更快。 If you do not have that line, then you can manually add it: 如果您没有该行,则可以手动添加它:

    ALTER TABLE `table_name` DISABLE KEYS;

    Use caution if this is a live database actively being used. 如果这是一个正在使用的实时数据库,请小心使用。

  3. Immediately after the INSERT statement(s), the tool also added this line. INSERT语句之后,该工具立即添加了该行。 Remove the comments as well to re-enable the primary keys: 删除注释以重新启用主键:

    /*!40000 ALTER TABLE `table_name` ENABLE KEYS */;

    Again, it should look like this: 再次,它应该是这样的:

    ALTER TABLE `table_name` ENABLE KEYS;

  4. Before actually running the insert statement, turn off autocommit , unique_checks , and foreign_key_checks with a query: 在实际运行insert语句之前, unique_checks使用查询关闭autocommitunique_checksforeign_key_checks

    set autocommit=0; set unique_checks=0; set foreign_key_checks=0;

  5. Now run your import: 现在运行你的导入:

    source /path/to/your/file.sql

  6. Once the import has successfully finished, you'll want to commit then re-enable unique_checks and foreign_key_checks : 导入成功完成后,您将要提交然后重新启用unique_checksforeign_key_checks

    commit; set unique_checks=1; set foreign_key_checks=1;

Some other considerations for doing imports that you may want to consider are: 您可能需要考虑的进口的其他一些注意事项是:

  • if you're using a virtual machine, up the available resources (RAM, CPU core count) for the VM until the import is complete 如果您正在使用虚拟机,请为VM启动可用资源(RAM,CPU核心数),直到导入完成为止

  • if you're using a physical machine, maybe borrow some RAM from another machine long enough to complete the import 如果您使用的是物理机,可以从另一台机器借用一些RAM足够长的时间来完成导入

  • if possible, use 2 different disks: 1 dedicated to read from (source of SQL file) and one dedicated for write (where the DB or VM lives) 如果可能的话,使用2个不同的磁盘:1个专用于读取(SQL文件源)和1个专用于写入(DB或VM所在的位置)

Hope this helps someone else! 希望这有助于其他人!

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

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