이것이 내가 생각할 수있는 유일한 방법이며 제한에 대한 오류가 발생합니다. 결과 루프가 상당히 클 수 있으므로 while 루프 내에서 청크를 삭제하려고합니다.내부 조인과 한계가있는 mysql 삭제
첫 번째 계산서가 올바르게 작동합니다. 그것은 두 번째 delete 문이며 그렇지 않습니다. 조인 및/또는 제한 사용으로 인해 추측됩니다. 가입하는 동안 삭제를 어떻게 제한 할 수 있습니까?
//find total count to delete
$stmt = $db->prepare("
SELECT
COUNT(*)
FROM app_logs
INNER JOIN users
ON users.user_id = app_logs.user_id
INNER JOIN computers
ON computers.computer_id = users.computer_id AND computers.account_id != :account
WHERE app_logs.timestamp < :cutoff_time
");
$binding = array(
'account' => 2,
'cutoff_time' => strtotime('-3 months')
);
$stmt->execute($binding);
//get total results count from above
$found_count = $stmt->fetch(PDO::FETCH_COLUMN, 0);
echo $found_count; //ex. 15324
//delete rows
$stmt = $db->prepare("
DELETE
FROM app_logs
INNER JOIN users
ON users.user_id = spc_app_logs.user_id
INNER JOIN computers
ON computers.computer_id = users.computer_id AND computers.account_id != :account
WHERE app_logs.timestamp < :cutoff_time
LIMIT :limit
");
$binding = array(
'account' => 2,
'cutoff_time' => strtotime('-3 months'),
'limit' => 2000
);
while($found_count > 0)
{
$stmt->execute($binding);
$found_count = $found_count - $binding['limit'];
}