如何从mysql表中删除数百万条记录
在我的MySQL表中,包含超过2000万条记录。我想通过运行从较低的索引中删除它如何从mysql表中删除数百万条记录,mysql,database,Mysql,Database,在我的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 th
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后台运行查询或更快的步骤来删除这些记录?您可以首先找到要删除的实际id
SELECT idDailyReportDetails
FROM mydb.dailyreportdetails
where idDailyReportDetails>0
order by idDailyReportDetails asc limit 1000,1 ;
然后直接删除,使用选择中的值
DELETE FROM mydb.dailyreportdetails
where idDailyReportDetails < ID;
从mydb.dailReportDetails中删除
其中idDailReportDetails
您可以首先找到要删除的实际id
SELECT idDailyReportDetails
FROM mydb.dailyreportdetails
where idDailyReportDetails>0
order by idDailyReportDetails asc limit 1000,1 ;
然后直接删除,使用选择中的值
DELETE FROM mydb.dailyreportdetails
where idDailyReportDetails < ID;
从mydb.dailReportDetails中删除
其中idDailReportDetails
您可以通过以下方式检查InnoDB锁:
SHOW ENGINE InnoDB STATUS;
然后您将获得状态或错误
如果您想增加案例中的时间,请使用
set innodb_lock_wait_timeout=3000;
然后运行删除查询。您可以通过
SHOW ENGINE InnoDB STATUS;
然后您将获得状态或错误
如果您想增加案例中的时间,请使用
set innodb_lock_wait_timeout=3000;
然后运行删除查询。从表中删除行的最快方法是按id引用行。对于二进制日志,这也是最好的方法。因此,最好的方法是使用某种编程语言,在循环中获取一组新的行ID,然后通过在
WHERE idDailyReportDetails in()
子句中显式声明行来删除这些行
如果数据库被其他进程使用,范围查询(其中id
WHERE idDailyReportDetails in()
子句中显式声明行来删除这些行
如果数据库被其他进程使用,范围查询(其中id
idDailReportDetails
是否已编制索引?您可以增加innodb\u锁定\u等待\u超时。最好定期删除不需要的记录以避免此问题。您可以使用cron作业来执行此操作。您可以在项目中以一定的时间间隔运行cron作业,以便定期删除不需要的记录dailryreportdetails表中的iddailryreportdetails列自动递增键是吗?你能分享这个模式吗?这个语句最多删除1000条记录,而不是数百万条。显然是查询部分花费了时间,而不是删除。idDailReportDetails
是否已编制索引?您可以增加innodb\u锁定\u等待\u超时。最好定期删除不需要的记录以避免此问题。您可以使用cron作业来执行类似操作。但运行单独的查询然后运行delete语句并不一定更快。但运行单独的查询然后运行delete语句也不一定更快。