Innodb性能优化

发布于 2024-09-12 16:02:28 字数 492 浏览 3 评论 0原文

我网站的一部分需要批量插入,innodb 需要大约 40 分钟才能将该文件加载到数据库中。我一直在网上挖掘并发现了一些东西。

  1. innodb_autoinc_lock_mode=2 (不会生成连续的键)
  2. UNIQUE_CHECKS=0; (禁用唯一键检查)
  3. FOREIGN_KEY_CHECKS=0 (禁用外键检查)
  4. --log_bin=OFF 关闭用于复制的二进制日志

问题

我想为一个会话(即在批量插入期间)设置前 3 个选项。第一个选项不起作用 mysql 说未知系统变量“innodb_autoinc_lock_mode”。我正在使用 MySQL 5.0.4

最后一个选项,我想将其关闭,但我想知道如果我稍后需要复制,如果我再次打开它,它会开始工作吗?

建议

对于如何改进 innodb 引擎的批量插入/更新还有其他建议吗?或者请对我的发现发表评论。

谢谢

One of the portion of my site requires bulk insert, it takes around 40 mins for innodb to load that file into database. I have been digging around the web and found few things.

  1. innodb_autoinc_lock_mode=2 (It wont generate consecutive keys)
  2. UNIQUE_CHECKS=0; (disable unique key checks)
  3. FOREIGN_KEY_CHECKS=0 (disable foreign key checks)
  4. --log_bin=OFF turn off binary log used for replication

Problem

I want to set first 3 options for just one session i.e. during bulk insert. The first option does not work mysql says unknown system variable 'innodb_autoinc_lock_mode'. I am using MySQL 5.0.4

The last option, I would like to turn it off but I am wondering what if I need replication later will it just start working if I turn it on again?

Suggestions

Any other suggestions how to improve bulk inserts/updates for innodb engine? Or please comments on my findings.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨敛清荷 2024-09-19 16:02:28

假设您在单个或几个事务中加载数据,则大部分时间可能都花在构建索引上(取决于表的架构)。

不要在启用自动提交的情况下执行大量小插入,这会破坏每次提交的同步性能。

如果你的表比 innodb 缓冲池更大(或者几乎和 innodb 缓冲池一样大),那么你就有麻烦了;内存中无法容纳其索引的表无法有效插入,因为它必须执行 READS 操作才能插入。这样就可以更新现有的索引块。

请记住,光盘写入是可以的(它们大多是连续的,并且您有一个电池支持的 raid 控制器,对吗?),但读取速度很慢,需要避免。

总之,

  • 在少量大型事务中进行插入,例如 10k-100k 行或每行。不要使事务太大,否则您将耗尽日志。
  • 获得足够的内存,以便您的表能够容纳在内存中;适当设置 innodb 缓冲池(您正在运行 x86_64,对吧?)
  • 不要担心操作需要很长时间,因为由于 MVCC,您的应用程序将能够对以前版本的行进行操作(假设它只是读取) 。

不要进行上面列出的任何优化,它们可能会浪费时间(不要相信我的话 - 在您的实验室中的测试系统上对有/没有这些优化的操作进行基准测试)。

关闭唯一检查非常危险,因为您最终会得到损坏的数据。

Assuming you are loading the data in a single or few transactions, most of the time is likely to be spent building indexes (depending on the schema of the table).

Do not do a large number of small inserts with autocommit enabled, that will destroy performance with syncs for each commit.

If your table is bigger (or nearly as big as) the innodb buffer pool you are in trouble; a table which can't fit in ram with its indexes cannot be inserted into efficiently, as it will have to do READS to insert. This is so that existing index blocks can be updated.

Remember that disc writes are ok (they are mostly sequential, and you have a battery-backed raid controller, right?), but reads are slow and need to be avoided.

In summary

  • Do the insert in a small number of big-ish transactions, say 10k-100k rows or each. Don't make the transactions too big or you'll exhaust the logs.
  • Get enough ram that your table fits in memory; set the innodb buffer pool appropriately (You are running x86_64, right?)
  • Don't worry about the operation taking a long time, as due to MVCC, your app will be able to operate on the previous versions of the rows assuming it's only reading.

Don't make any of the optimisations listed above, they're probably waste of time (don't take my word for it - benchmark the operation on a test system in your lab with/without those).

Turning unique checks off is actively dangerous as you'll end up with broken data.

脱离于你 2024-09-19 16:02:28

要回答你问题的最后一部分,不,它不会再次开始工作;它会再次开始工作。如果不复制插入但复制后续更新,结果将不会很漂亮。禁用外键和唯一键应该没问题,前提是您之后重新启用它们,并处理任何约束违规。

您需要多久执行一次此操作?您可以更频繁地加载较小的数据集吗?

To answer the last part of your question, no it won't just start working again; if the inserts are not replicated but subsequent updates are, the result will not be a pretty sight. Disabling foreign and unique keys should be OK, provided you re-enable them afterwards, and deal with any constraint violations.

How often do you have to do this? Can you load smaller datasets more frequently?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文