Skip to content

Commit

Permalink
NEXT-22163 - Optimize Taskhandler delete queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Isengo1989 authored and J-Rahe committed Jul 11, 2022
1 parent bb28a53 commit 64ede82
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
9 changes: 9 additions & 0 deletions changelog/_unreleased/2022-06-24-optimize-delete-queries.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Optimize Delete queries on cart and version tables
issue: Issue-2532
author: Micha Hobert
author_email: [email protected]
author_github: Isengo1989
---
# Core
* Adding a do-while loop and limit to the `DELETE` query in `CleanupCartTaskHandler` and `CleanupVersionTaskHandler` to 1000 entries. This is necessary when a scheduled task failed / was inactive for some time
12 changes: 7 additions & 5 deletions src/Core/Checkout/Cart/Cleanup/CleanupCartTaskHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ public function run(): void
$time = new \DateTime();
$time->modify(sprintf('-%s day', $this->days));

$this->connection->executeStatement(
<<<'SQL'
do {
$result = $this->connection->executeStatement(
<<<'SQL'
DELETE FROM cart
WHERE (updated_at IS NULL AND created_at <= :timestamp)
OR (updated_at IS NOT NULL AND updated_at <= :timestamp);
OR (updated_at IS NOT NULL AND updated_at <= :timestamp) LIMIT 1000;
SQL,
['timestamp' => $time->format(Defaults::STORAGE_DATE_TIME_FORMAT)]
);
['timestamp' => $time->format(Defaults::STORAGE_DATE_TIME_FORMAT)]
);
} while ($result > 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ public function run(): void
$time = new \DateTime();
$time->modify(sprintf('-%s day', $this->days));

$this->connection->executeStatement(
'DELETE FROM version WHERE created_at <= :timestamp',
['timestamp' => $time->format(Defaults::STORAGE_DATE_TIME_FORMAT)]
);

$this->connection->executeStatement(
'DELETE FROM version_commit WHERE created_at <= :timestamp',
['timestamp' => $time->format(Defaults::STORAGE_DATE_TIME_FORMAT)]
);
do {
$result = $this->connection->executeStatement(
'DELETE FROM version WHERE created_at <= :timestamp LIMIT 1000',
['timestamp' => $time->format(Defaults::STORAGE_DATE_TIME_FORMAT)]
);
} while ($result > 0);

do {
$result = $this->connection->executeStatement(
'DELETE FROM version_commit WHERE created_at <= :timestamp LIMIT 1000',
['timestamp' => $time->format(Defaults::STORAGE_DATE_TIME_FORMAT)]
);
} while ($result > 0);
}
}

0 comments on commit 64ede82

Please sign in to comment.