From 6502dfae343850f510471b72ebece1c1329343e0 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 7 Oct 2021 16:11:49 +0200 Subject: [PATCH] add better index for finding unindexed paths for the following query 'SELECT "path" FROM "oc_filecache" WHERE ("storage" = $storage) AND ("size" < 0) ORDER BY "fileid" DESC LIMIT 1;' currently the database will in some cases decide to priorize the sort by fileid over the filter when picking what index to use, resulting in a much slower query. by creating an index that allows first sorting by fileid and also filter by storage and size this case will be greatly sped up Signed-off-by: Robin Appelman --- core/Application.php | 4 ++++ core/Command/Db/AddMissingIndices.php | 7 +++++++ core/Migrations/Version13000Date20170718121200.php | 1 + 3 files changed, 12 insertions(+) diff --git a/core/Application.php b/core/Application.php index f0478173d43dd..43f32fcbdcc88 100644 --- a/core/Application.php +++ b/core/Application.php @@ -114,6 +114,10 @@ function (GenericEvent $event) use ($container) { if (!$table->hasIndex('fs_size')) { $subject->addHintForMissingSubject($table->getName(), 'fs_size'); } + + if (!$table->hasIndex('fs_id_storage_size')) { + $subject->addHintForMissingSubject($table->getName(), 'fs_id_storage_size'); + } } if ($schema->hasTable('twofactor_providers')) { diff --git a/core/Command/Db/AddMissingIndices.php b/core/Command/Db/AddMissingIndices.php index 2e6b3dc9aeb34..c4360157f725c 100644 --- a/core/Command/Db/AddMissingIndices.php +++ b/core/Command/Db/AddMissingIndices.php @@ -144,6 +144,13 @@ private function addCoreIndexes(OutputInterface $output) { $updated = true; $output->writeln('Filecache table updated successfully.'); } + if (!$table->hasIndex('fs_id_storage_size')) { + $output->writeln('Adding additional size index to the filecache table, this can take some time...'); + $table->addIndex(['fileid', 'storage', 'size'], 'fs_id_storage_size'); + $this->connection->migrateToSchema($schema->getWrappedSchema()); + $updated = true; + $output->writeln('Filecache table updated successfully.'); + } } $output->writeln('Check indices of the twofactor_providers table.'); diff --git a/core/Migrations/Version13000Date20170718121200.php b/core/Migrations/Version13000Date20170718121200.php index 7b959c6073cb9..e8dbf1fc0fb20 100644 --- a/core/Migrations/Version13000Date20170718121200.php +++ b/core/Migrations/Version13000Date20170718121200.php @@ -261,6 +261,7 @@ public function changeSchema(IOutput $output, \Closure $schemaClosure, array $op $table->addIndex(['storage', 'mimetype'], 'fs_storage_mimetype'); $table->addIndex(['storage', 'mimepart'], 'fs_storage_mimepart'); $table->addIndex(['storage', 'size', 'fileid'], 'fs_storage_size'); + $table->addIndex(['fileid', 'storage', 'size'], 'fs_id_storage_size'); $table->addIndex(['mtime'], 'fs_mtime'); $table->addIndex(['size'], 'fs_size'); }