如何优化 MySQL 以插入数百万行?
我需要将数百万行插入 MySQL 数据库(InnoDB 引擎).当桌子很大时,我有时间问题.几乎所有时间都花在插入查询上.也许有人知道如何优化它?
I need to insert millions rows into the MySQL database (InnoDB engine). I have a problem with time when the tables have big sizes. Almost all time is spent on insert queries. Maybe somebody know how to optimize it?
推荐答案
将大量数据导入 InnoDB:
To import large bulk of data into InnoDB:
在 MySQL 配置中设置
set in MySQL configuration
- innodb_doublewrite = 0
- innodb_buffer_pool_size = 50%+ 系统内存
- innodb_log_file_size = 512M
- log-bin = 0
- innodb_support_xa = 0
- innodb_flush_log_at_trx_commit = 0
事务开始后立即添加:
SET FOREIGN_KEY_CHECKS = 0;
SET FOREIGN_KEY_CHECKS = 0;
SET UNIQUE_CHECKS = 0;
SET UNIQUE_CHECKS = 0;
SET AUTOCOMMIT = 0;
SET AUTOCOMMIT = 0;
在交易结束前设置:
SET UNIQUE_CHECKS = 1;
SET UNIQUE_CHECKS = 1;
SET FOREIGN_KEY_CHECKS = 1;
SET FOREIGN_KEY_CHECKS = 1;
相关文章