大删除查询给出超时

时间:2013-11-25 11:10:42

标签: mysql phpmyadmin

我有像这样的删除查询

DELETE c1
FROM tablename c1, tablename c2 
WHERE c1.rewardID > c2.rewardID AND 
      c1.transactionDate = c2.transactionDate AND 
      c1.orderID = c2.orderID 

这给了我php和phpmyadmin的超时错误,因为需要删除许多行。如何在没有超时错误的情况下执行它?

问题是当我打印mysql_error()

时,它在查询期间显示与MySQL服务器的连接丢失

任何人都可以提供此查询的精选版本,以便我可以通过php迭代它并删除

解释详情:

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra   
1   SIMPLE  c1  ALL     PRIMARY,rewardID    NULL    NULL    NULL    92966   
1   SIMPLE  c2  ALL     PRIMARY,rewardID    NULL    NULL    NULL    92966   Using where; Using join buffer

1 个答案:

答案 0 :(得分:1)

@Suresh Kamrushi说的是这样的。运行LIMITED DELETE直到没有更多行。

DELETE c1
FROM tablename c1, tablename c2 
WHERE c1.rewardID > c2.rewardID AND 
      c1.transactionDate = c2.transactionDate AND 
      c1.orderID = c2.orderID
LIMIT 10000;

但JOIN未优化(未创建nesseccery索引),LIMIT可能没有帮助。请发布EXPLAIN输出,以便我们为您提供帮助。在查询下方查看以查看需要多长时间。

SELECT COUNT(*)
FROM tablename c1, tablename c2 
WHERE c1.rewardID > c2.rewardID AND 
      c1.transactionDate = c2.transactionDate AND 
      c1.orderID = c2.orderID

EDITED

谢谢,你试过这个INDEX吗?

ALTER TABLE tablename ADD INDEX (orderID, tranasctionDate, rewardID);

请发布您的CREATE TABLE..,了解rewardID的外观。

已编辑2

最后一次尝试。确保'set autocommit = 0;开始'回滚。

DELETE FROM tablename c1
WHERE EXISTS (
    SELECT 1
    FROM tablename c2 
    WHERE c1.rewardID > c2.rewardID AND 
          c1.transactionDate = c2.transactionDate AND 
          c1.orderID = c2.orderIDl
);