perf: add indices (#114)

old-stable24
Varun Patil 2022-10-26 10:06:45 -07:00
parent 6d8f06c885
commit cfeebbf4fa
4 changed files with 129 additions and 7 deletions

View File

@ -23,6 +23,8 @@ declare(strict_types=1);
namespace OCA\Memories\Command; namespace OCA\Memories\Command;
use OC\DB\Connection;
use OC\DB\SchemaWrapper;
use OCA\Files_External\Service\GlobalStoragesService; use OCA\Files_External\Service\GlobalStoragesService;
use OCA\Memories\AppInfo\Application; use OCA\Memories\AppInfo\Application;
use OCA\Memories\Db\TimelineWrite; use OCA\Memories\Db\TimelineWrite;
@ -58,6 +60,7 @@ class Index extends Command
protected OutputInterface $output; protected OutputInterface $output;
protected IManager $encryptionManager; protected IManager $encryptionManager;
protected IDBConnection $connection; protected IDBConnection $connection;
protected Connection $connectionForSchema;
protected TimelineWrite $timelineWrite; protected TimelineWrite $timelineWrite;
// Stats // Stats
@ -75,6 +78,7 @@ class Index extends Command
IConfig $config, IConfig $config,
IManager $encryptionManager, IManager $encryptionManager,
IDBConnection $connection, IDBConnection $connection,
Connection $connectionForSchema,
ContainerInterface $container ContainerInterface $container
) { ) {
parent::__construct(); parent::__construct();
@ -85,6 +89,7 @@ class Index extends Command
$this->config = $config; $this->config = $config;
$this->encryptionManager = $encryptionManager; $this->encryptionManager = $encryptionManager;
$this->connection = $connection; $this->connection = $connection;
$this->connectionForSchema = $connectionForSchema;
$this->timelineWrite = new TimelineWrite($connection, $preview); $this->timelineWrite = new TimelineWrite($connection, $preview);
try { try {
@ -116,8 +121,12 @@ class Index extends Command
protected function execute(InputInterface $input, OutputInterface $output): int protected function execute(InputInterface $input, OutputInterface $output): int
{ {
// Add missing indices
$output->writeln('Checking database indices');
\OCA\Memories\Db\AddMissingIndices::run(new SchemaWrapper($this->connectionForSchema), $this->connectionForSchema);
// Print mime type support information // Print mime type support information
$output->writeln('MIME Type support:'); $output->writeln("\nMIME Type support:");
$mimes = array_merge(Application::IMAGE_MIMES, Application::VIDEO_MIMES); $mimes = array_merge(Application::IMAGE_MIMES, Application::VIDEO_MIMES);
$someUnsupported = false; $someUnsupported = false;
foreach ($mimes as &$mimeType) { foreach ($mimes as &$mimeType) {

View File

@ -0,0 +1,48 @@
<?php
namespace OCA\Memories\Db;
use OC\DB\Connection;
use OC\DB\SchemaWrapper;
class AddMissingIndices
{
/**
* Add missing indices to the database schema.
*
* @param SchemaWrapper $schema Schema wrapper
* @param null|Connection $connection Connection to db
*/
public static function run(SchemaWrapper &$schema, $connection)
{
// Should migrate at end
$shouldMigrate = false;
// Add index on systemtag_object_mapping to speed up the query
if ($schema->hasTable('systemtag_object_mapping')) {
$table = $schema->getTable('systemtag_object_mapping');
if (!$table->hasIndex('memories_type_tagid')) {
$table->addIndex(['objecttype', 'systemtagid'], 'memories_type_tagid');
$shouldMigrate = true;
}
}
// Add index on recognize detections for file id to speed up joins
if ($schema->hasTable('recognize_face_detections')) {
$table = $schema->getTable('recognize_face_detections');
if (!$table->hasIndex('memories_file_id')) {
$table->addIndex(['file_id'], 'memories_file_id');
$shouldMigrate = true;
}
}
// Migrate
if ($shouldMigrate && null !== $connection) {
$connection->migrateToSchema($schema->getWrappedSchema());
}
return $schema;
}
}

View File

@ -160,7 +160,7 @@ trait TimelineQueryDays
} }
/** /**
* Get all folders inside a top folder * Get all folders inside a top folder.
*/ */
private function getSubfolderIdsRecursive( private function getSubfolderIdsRecursive(
IDBConnection &$conn, IDBConnection &$conn,
@ -222,12 +222,12 @@ trait TimelineQueryDays
} }
/** /**
* Get the query for oc_filecache join * Get the query for oc_filecache join.
* *
* @param IQueryBuilder $query Query builder * @param IQueryBuilder $query Query builder
* @param Folder|array $folder Either the top folder or array of folder Ids * @param array|Folder $folder Either the top folder or array of folder Ids
* @param bool $recursive Whether to get the days recursively * @param bool $recursive Whether to get the days recursively
* @param bool $archive Whether to get the days only from the archive folder * @param bool $archive Whether to get the days only from the archive folder
*/ */
private function getFilecacheJoinQuery( private function getFilecacheJoinQuery(
IQueryBuilder &$query, IQueryBuilder &$query,

View File

@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Your name <your@email.com>
* @author Your name <your@email.com>
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace OCA\Memories\Migration;
use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
/**
* Auto-generated migration step: Please modify to your needs!
*/
class Version400308Date20221026151748 extends SimpleMigrationStep
{
/**
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
*/
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void
{
}
/**
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper
{
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
// Speed up CTE lookup for subdirectories
$fileCacheTable = $schema->getTable('filecache');
$fileCacheTable->addIndex(['parent', 'mimetype'], 'memories_parent_mimetype');
// Add other indices
return \OCA\Memories\Db\AddMissingIndices::run($schema, null);
}
/**
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
*/
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void
{
}
}