我有一个 mySQL 更新查询,有时会更新所有字段,有时会更新除一个以外的所有字段。
它在大约 10% 的调用中失败。
我的桌子是:
CREATE TABLE IF NOT EXISTS `grades` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`state` int(1) NOT NULL,
`result` varchar(255) NOT NULL,
`date_synced` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`) )
ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4395 ;
我的查询是:
$sqlstr = "UPDATE grades SET result = '$result', state = 2, date_synced = '$date', updated_at = '$date' WHERE id = $id";
当失败时,result、date_synced 和 updated_at 会更新,但状态保持不变。
还有另一个查询只更新状态字段,并且也间歇性地失败。
我一直无法在我们的测试环境中重现问题。生产 mySQL 数据库是否有问题或某种锁定冲突?
我有更多信息。我正在使用 mysqli。另一个只更新状态的查询是使用 mysql。这会导致问题吗?
我以为 InnoDB 按行锁定。它不允许部分行更新,是吗?
另一个更新来解决评论。
我的代码流非常线性。
The row is created with state=0.
<flash stuff here> and the row is updated with state=1
A cron job pulls all state=1 and sends an api call
if api call is successful, the row is updated with state=2, result, date_synced, and updated_at
if api call is error, the row is updated with state=3, result, and updated_at
state 字段永远不会设置回 0(在 flash 之后)或 1(在 api 调用之后)。由于 date _synced 和 result 正在设置,但(有时)状态仍为 1,这就像对 state 字段的更新正在被删除。
我将添加更新触发器,看看是否能提供更多信息。