Duplicate entry on INSERT after DELETE from table in transaction

寵の児 提交于 2019-12-25 07:29:06

问题


I'm looking for any ideas to explain (and prevent) the following:

1) We have a Magento reindex process (price or stock) that is failing from time to time with:

exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '512723-1-1' for key 'PRIMARY'' in /[omitted]/lib/Zend/Db/Statement/Pdo.php:242

Full trace: https://gist.github.com/werdan/5255362

2) Magento reindex is done in transaction, which can be sketched as following:

START TRANSACTION;
DELETE FROM `cataloginventory_stock_status`;
INSERT INTO cataloginventory_stock_status SELECT * FROM cataloginventory_stock_status_idx;
COMMIT;

3) It should be impossible to break consistency of the table between DELETE and INSERT commands, nevertheless it happens.

We have checked that tables cataloginventory_stock_status and cataloginventory_stock_status_idx have identical keys and structure.

What else can be suggested as an explanation and solution?


回答1:


This is what many SQL dump tools do:

LOCK TABLES foo WRITE;
ALTER TABLE foo DISABLE KEYS;

INSERT INTO foo (foo_id, foo_name) VALUES (1, 'One'), 
    (2, 'Two'), 
    (3, 'Three');

ALTER TABLE foo ENABLE KEYS;
UNLOCK TABLES;


来源:https://stackoverflow.com/questions/15663626/duplicate-entry-on-insert-after-delete-from-table-in-transaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!