diff options
author | Joas Schilling <nickvergessen@owncloud.com> | 2016-02-10 12:01:55 +0100 |
---|---|---|
committer | Joas Schilling <nickvergessen@owncloud.com> | 2016-02-10 13:04:37 +0100 |
commit | 0ebb2050102190b1186c7338a84f86bd6f3f9d43 (patch) | |
tree | 21ebae1c9aa48d79156c6a0bdde8489d37ca9662 /apps/files/command/deleteorphanedfiles.php | |
parent | 9a2c517ca8eaf25bf142696e1479355112b1f108 (diff) | |
download | nextcloud-server-0ebb2050102190b1186c7338a84f86bd6f3f9d43.tar.gz nextcloud-server-0ebb2050102190b1186c7338a84f86bd6f3f9d43.zip |
Chunk the queries to make sure they don't time out
Diffstat (limited to 'apps/files/command/deleteorphanedfiles.php')
-rw-r--r-- | apps/files/command/deleteorphanedfiles.php | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/apps/files/command/deleteorphanedfiles.php b/apps/files/command/deleteorphanedfiles.php index d276e9a0993..f897c68fd8d 100644 --- a/apps/files/command/deleteorphanedfiles.php +++ b/apps/files/command/deleteorphanedfiles.php @@ -33,6 +33,8 @@ use Symfony\Component\Console\Output\OutputInterface; */ class DeleteOrphanedFiles extends Command { + const CHUNK_SIZE = 200; + /** * @var IDBConnection */ @@ -50,13 +52,31 @@ class DeleteOrphanedFiles extends Command { } public function execute(InputInterface $input, OutputInterface $output) { + $deletedEntries = 0; + + $query = $this->connection->getQueryBuilder(); + $query->select('fc.fileid') + ->from('filecache', 'fc') + ->where($query->expr()->isNull('s.numeric_id')) + ->leftJoin('fc', 'storages', 's', $query->expr()->eq('fc.storage', 's.numeric_id')) + ->setMaxResults(self::CHUNK_SIZE); + + $deleteQuery = $this->connection->getQueryBuilder(); + $deleteQuery->delete('filecache') + ->where($deleteQuery->expr()->eq('fileid', $deleteQuery->createParameter('objectid'))); - $sql = - 'DELETE FROM `*PREFIX*filecache` ' . - 'WHERE NOT EXISTS ' . - '(SELECT 1 FROM `*PREFIX*storages` WHERE `storage` = `numeric_id`)'; + $deletedInLastChunk = self::CHUNK_SIZE; + while ($deletedInLastChunk === self::CHUNK_SIZE) { + $deletedInLastChunk = 0; + $result = $query->execute(); + while ($row = $result->fetch()) { + $deletedInLastChunk++; + $deletedEntries += $deleteQuery->setParameter('objectid', (int) $row['fileid']) + ->execute(); + } + $result->closeCursor(); + } - $deletedEntries = $this->connection->executeUpdate($sql); $output->writeln("$deletedEntries orphaned file cache entries deleted"); } |