1

我有一个相当复杂的 DELETE 查询,我需要将其分解为多个批次,但是在我确定在执行查询之前将删除多少行之前,我不能这样做。这是我想要了解的查询:

DELETE parts, binaries FROM parts LEFT JOIN binaries ON binaries.ID = parts.binaryID LEFT JOIN releasenfo rn ON rn.binaryID = binaries.ID WHERE binaries.procstat IN (4, 6) AND (rn.binaryid IS NULL OR (rn.binaryid IS NOT NULL AND rn.nfo IS NOT NULL))) OR binaries.dateadded < '2013-01-04 22:01:17' - INTERVAL 36 HOUR;

这是我认为可能有效的方法,但它无效:

Select COUNT(*) FROM (DELETE parts, binaries FROM parts LEFT JOIN binaries ON binaries.ID = parts.binaryID LEFT JOIN releasenfo rn ON rn.binaryID = binaries.ID WHERE binaries.procstat IN (4, 6) AND (rn.binaryid IS NULL OR (rn.binaryid IS NOT NULL AND rn.nfo IS NOT NULL))) OR binaries.dateadded < '2013-01-04 22:01:17' - INTERVAL 36 HOUR) AS countme;

有人可以指出我正确的方向吗?

为了将其置于上下文中,我想将它与以下 php 伪代码一起使用:

$limit = 10000; // Do in batches to give user status            
$totalRows = $db->query("Count query goes here");

while($rowsLeft > 0)
{
    echo "$rowsLeft remaining to be deleted\n";
    $db->query("DELETE ...... LIMIT ".$limit);
    $rowsLeft -= $limit;
}   
4

1 回答 1

2

你可以简单地试试这个:

SELECT * FROM parts p 
LEFT JOIN binaries b ON b.ID = p.binaryID AND b.procstat IN (4, 6) 
LEFT JOIN releasenfo rn ON rn.binaryID = b.ID 
WHERE rn.binaryid IS NULL OR (rn.binaryid IS NOT NULL AND rn.nfo IS NOT NULL) OR 
        b.dateadded < '2013-01-04 22:01:17' - INTERVAL 36 HOUR;

DELETE p, b FROM parts p 
LEFT JOIN binaries b ON b.ID = p.binaryID AND b.procstat IN (4, 6) 
LEFT JOIN releasenfo rn ON rn.binaryID = b.ID 
WHERE rn.binaryid IS NULL OR (rn.binaryid IS NOT NULL AND rn.nfo IS NOT NULL) OR 
        b.dateadded < '2013-01-04 22:01:17' - INTERVAL 36 HOUR;
于 2013-01-05T04:00:58.430 回答