Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从mysql表中删除数百万条记录_Mysql_Database - Fatal编程技术网

如何从mysql表中删除数百万条记录

如何从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

在我的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后台运行查询或更快的步骤来删除这些记录?

您可以首先找到要删除的实际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从表中删除行的最快方法是通过id引用行。这对于二进制日志也是最好的。因此,最好的方法是使用某种编程语言,在循环中获取一组新的行ID,然后通过在
WHERE idDailyReportDetails in()
子句中显式声明行来删除这些行


如果数据库被其他进程使用,范围查询(其中id您最好在项目中运行cron job一段时间间隔,以便定期删除不需要的记录dailryreportdetails表中的iddailryreportdetails列auto increment key是否为?你能分享这个模式吗?这个语句最多删除1000条记录,而不是数百万条。显然是查询部分花费了时间,而不是删除。
idDailReportDetails
是否已编制索引?您可以增加innodb\u锁定\u等待\u超时。最好定期删除不需要的记录以避免此问题。您可以使用cron作业来执行此操作。您可以在项目中以一定的时间间隔运行cron作业,以便定期删除不需要的记录dailryreportdetails表中的iddailryreportdetails列自动递增键是吗?你能分享这个模式吗?这个语句最多删除1000条记录,而不是数百万条。显然是查询部分花费了时间,而不是删除。
idDailReportDetails
是否已编制索引?您可以增加innodb\u锁定\u等待\u超时。最好定期删除不需要的记录以避免此问题。您可以使用cron作业来执行类似操作。但运行单独的查询然后运行delete语句并不一定更快。但运行单独的查询然后运行delete语句也不一定更快。