简体   繁体   English

如何加快与MYISAM性能相当的INNODB查询?

[英]How can I speed up INNODB queries comparable to MYISAM performance?

I have recently switched my database tables from MYISAM to INNODB and experience bad timeouts with queries, mostly inserts. 我最近将我的数据库表从MYISAM切换到INNODB,并通过查询(主要是插入)体验不良超时。 One function I use previously took <2 seconds to insert, delete and update a large collection of records across ~30 MYISAM tables, but now that they are INNODB, the function causes a PHP timeout. 我之前使用的一个函数花了<2秒来插入,删除和更新~30个MYISAM表中的大量记录集,但现在它们是INNODB,该函数导致PHP超时。

The timeout was set to 60 seconds. 超时设置为60秒。 I have optimised my script enough that now, even though there are still many queries, they are combined together (multiple inserts, multiple deletes, etc) and the script now takes ~25 seconds, which is a substantial increase from what appeared to be at least 60 seconds. 我已经足够优化了我的脚本,现在,即使仍有很多查询,它们被组合在一起(多次插入,多次删除等),脚本现在需要大约25秒,这比看起来大大增加了至少60秒。

This duration is still over 10x quicker when previously using MYISAM, is there any mistakes I could be making in the way I process these queries? 以前使用MYISAM时,这个持续时间仍然快了10倍,我处理这些查询的方式是否有任何错误? Or are there any settings that could assist in the performance? 或者是否有任何可以帮助提高性能的设置? Currently the MySQL is using the default settings of installation. 目前MySQL正在使用默认的安装设置。

The queries are nothing special, DELETE ... WHERE ... simple logic, same with the INSERT and UPDATE queries. 查询没什么特别的, DELETE ... WHERE ...简单的逻辑,与INSERTUPDATE查询相同。

Hard to say without knowing too much about your environment, but this might be more of a database tuning problem. 很难说不了解你的环境太多,但这可能更像是一个数据库调优问题。 InnoDB can be VERY slow on budget hardware where every write forces a true flush. InnoDB在预算硬件上非常慢,每次写入都会强制进行真正的刷新。 (This affects writes, not reads.) (这会影响写入,而不会影响。)

For instance, you may want to read up on options like: 例如,您可能想要阅读以下选项:

innodb_flush_log_at_trx_commit=2
sync_binlog=0

By avoiding the flushes you may be able to speed up your application considerably, but at the cost of potential data loss if the server crashes. 通过避免刷新,您可以大大加快应用程序的速度,但是如果服务器崩溃,可能会导致数据丢失。

If data loss is something you absolutely cannot live with, then the other option is to use better hardware. 如果数据丢失是您绝对无法忍受的,那么另一种选择是使用更好的硬件。

Run explain for each query. 为每个查询运行explain That is, if the slow query is select foo from bar; 也就是说,如果慢查询是select foo from bar; , run explain select foo from bar; ,运行explain select foo from bar; .

Examine the plan, and add indices as necessary. 检查计划,并根据需要添加索引。 Re-run the explain, and make sure the indices are being used. 重新运行说明,并确保使用索引。

Innodb构建哈希索引,通过传递BTREE索引和使用更快的哈希来帮助加快索引查找速度

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

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