在我的MySQL表中包含超过2000万条记录。我想通过运行下面的索引删除它。
delete FROM mydb.dailyreportdetails where idDailyReportDetails>0 order by idDailyReportDetails asc limit 1000 ;在运行上面的查询时,我得到了如下所述的错误
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1205: 1205: Lock wait timeout exceeded; try restarting transaction
SQL Statement:是否有任何方法在mysql背景下运行查询或删除这些记录的更快步骤?
发布于 2017-06-24 07:31:38
你可以先找到要删除的id .
SELECT idDailyReportDetails
FROM mydb.dailyreportdetails
where idDailyReportDetails>0
order by idDailyReportDetails asc limit 1000,1 ;然后直接删除,使用来自select的值.
DELETE FROM mydb.dailyreportdetails
where idDailyReportDetails < ID;发布于 2017-06-24 07:41:39
您可以通过以下方法检查InnoDB锁
SHOW ENGINE InnoDB STATUS;然后你会得到状态或错误..。
如果您想要增加用例中的时间,请使用
set innodb_lock_wait_timeout=3000;之后,运行您的删除查询。
发布于 2017-06-24 07:45:53
从表中删除行的最快方法是通过id引用行。这对于二进制日志也是可取的。因此,最好的方法是使用一些编程语言,在循环中获取一组新的行in,然后通过在WHERE idDailyReportDetails IN ()子句中显式声明行来删除它们。
如果其他进程使用数据库,范围查询(其中id <某事)可能会因为锁而不断失败。
https://stackoverflow.com/questions/44733954
复制相似问题