1

我刚刚从 MyISAM 表切换到 InnoDB,因为行锁定似乎比表锁定更好。

问题是现在我以前做的很多事情都不起作用了。

例如,我有一个脚本可以根据用户的浏览历史向他们推荐项目。这是通过在推荐表中插入或更新行来完成的。建议的实际计算是通过 SQL 语句完成的。所以有时我正在执行推荐语句,然后立即(在同一个 PHP 脚本中)请求结果,以便可以在那里向用户显示。使用 MyISAM 可以完美运行,但使用 InnoDB 有时会返回旧结果,或者没有结果(如果以前没有结果)。

我还有一张桌子,可以作为某事的队列。要使用它,我执行并更新,然后选择此更新的结果。它再次适用于 MyISAM,但不适用于 InnoDB。

在某些情况下,如何确保立即插入或更新行?在其他情况下,如果可以加快查询速度,我会对延迟插入或更新感到满意。但在概述的特定情况下,我需要它是即时的。是否有 PHP 命令可以执行此操作?还是与 MySQL 设置有关?

4

1 回答 1

2

It sounds like you could be having transaction issues since moving to InnoDB http://dev.mysql.com/doc/refman/5.0/en/innodb-transaction-model.html .

Unlike MyISAM, InnoDB supports database transactions that isolate your previous SQL commands until you have committed them with a special COMMIT sql command. Normally, this isn't a problem since you will be running off of the same transaction on the same database connection and AUTOCOMMIT is enabled by default. The autocommit feature of MySQL will automatically commit the transaction after a data change, such as after an UPDATE. However, I would guess that you are connecting to the database multiple times (or perhaps using a library that is doing so) and your library or configuration is disabling autocommit.

You can test this easily by issuing the SQL COMMIT after your UPDATE and seeing if the problem disappears. If this is the problem, the long-term solution depends on the libraries you are using, but generally you can enable autocommit again (either through your library or by removing the disabling entry from your my.cnf or my.ini file) or you can preferably start using transactions! There are plenty of articles everywhere for "MySQL data transactions tutorial."

You can enable autocommit via PHP by executing the SQL SET AUTOCOMMIT=1; once before executing the rest of your SQL.

This all assumes you are not having replication issues.

于 2013-04-06T07:08:20.957 回答