Skip to content

Commit ead35bd

Browse files
CarlSchwanbackportbot[bot]
authored andcommitted
perf: Add index for getLatestVersion
perf: Add index for getLatestVersion Decrease time to execute the query from 10s of seconds to a few miliseconds. Signed-off-by: Carl Schwan <carlschwan@kde.org> [skip ci]
1 parent 6123335 commit ead35bd

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
'OCA\\Text\\Migration\\Version030901Date20231114150437' => $baseDir . '/../lib/Migration/Version030901Date20231114150437.php',
6464
'OCA\\Text\\Migration\\Version040100Date20240611165300' => $baseDir . '/../lib/Migration/Version040100Date20240611165300.php',
6565
'OCA\\Text\\Migration\\Version070000Date20250925110024' => $baseDir . '/../lib/Migration/Version070000Date20250925110024.php',
66+
'OCA\\Text\\Migration\\Version080000Date20260331132113' => $baseDir . '/../lib/Migration/Version080000Date20260331132113.php',
6667
'OCA\\Text\\Notification\\Notifier' => $baseDir . '/../lib/Notification/Notifier.php',
6768
'OCA\\Text\\Service\\ApiService' => $baseDir . '/../lib/Service/ApiService.php',
6869
'OCA\\Text\\Service\\AttachmentService' => $baseDir . '/../lib/Service/AttachmentService.php',

composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class ComposerStaticInitText
7878
'OCA\\Text\\Migration\\Version030901Date20231114150437' => __DIR__ . '/..' . '/../lib/Migration/Version030901Date20231114150437.php',
7979
'OCA\\Text\\Migration\\Version040100Date20240611165300' => __DIR__ . '/..' . '/../lib/Migration/Version040100Date20240611165300.php',
8080
'OCA\\Text\\Migration\\Version070000Date20250925110024' => __DIR__ . '/..' . '/../lib/Migration/Version070000Date20250925110024.php',
81+
'OCA\\Text\\Migration\\Version080000Date20260331132113' => __DIR__ . '/..' . '/../lib/Migration/Version080000Date20260331132113.php',
8182
'OCA\\Text\\Notification\\Notifier' => __DIR__ . '/..' . '/../lib/Notification/Notifier.php',
8283
'OCA\\Text\\Service\\ApiService' => __DIR__ . '/..' . '/../lib/Service/ApiService.php',
8384
'OCA\\Text\\Service\\AttachmentService' => __DIR__ . '/..' . '/../lib/Service/AttachmentService.php',
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Text\Migration;
11+
12+
use Closure;
13+
use OCP\DB\ISchemaWrapper;
14+
use OCP\IDBConnection;
15+
use OCP\Migration\Attributes\AddIndex;
16+
use OCP\Migration\Attributes\IndexType;
17+
use OCP\Migration\IOutput;
18+
use OCP\Migration\SimpleMigrationStep;
19+
use Override;
20+
21+
#[AddIndex(table: 'text_steps', type: IndexType::INDEX, description: 'Optimize cleanup job')]
22+
class Version080000Date20260331132113 extends SimpleMigrationStep {
23+
private bool $addIndexLater = false;
24+
25+
public function __construct(
26+
private readonly IDBConnection $connection,
27+
) {
28+
}
29+
30+
#[Override]
31+
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
32+
/** @var ISchemaWrapper $schema */
33+
$schema = $schemaClosure();
34+
35+
$table = $schema->getTable('text_steps');
36+
if (!$table->hasIndex('text_steps_doc_id_id_index')) {
37+
if ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_MARIADB
38+
|| $this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_MYSQL) {
39+
$table->addIndex(['document_id', 'id'], 'text_steps_doc_id_id_index');
40+
} else {
41+
$this->addIndexLater = true;
42+
}
43+
return $schema;
44+
}
45+
46+
return null;
47+
}
48+
49+
#[Override]
50+
public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options): ?ISchemaWrapper {
51+
/** @var ISchemaWrapper $schema */
52+
$schema = $schemaClosure();
53+
if ($this->addIndexLater) {
54+
// We need to add the index with a DESC manually
55+
// See https://github.com/doctrine/orm/issues/8128
56+
if ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_POSTGRES) {
57+
$stm = $this->connection->prepare('CREATE INDEX CONCURRENTLY text_steps_doc_id_id_index ON **PREFIX**text_steps (document_id, id DESC);');
58+
} elseif ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) {
59+
$stm = $this->connection->prepare('CREATE INDEX text_steps_doc_id_id_index ON **PREFIX**text_steps (document_id, id DESC) ONLINE;');
60+
} elseif ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_SQLITE) {
61+
$stm = $this->connection->prepare('CREATE INDEX text_steps_doc_id_id_index ON **PREFIX**text_steps (document_id, id DESC);');
62+
} else {
63+
throw new \RuntimeException('Unsupported platform');
64+
}
65+
$stm->execute();
66+
return $schema;
67+
}
68+
return null;
69+
}
70+
}

0 commit comments

Comments
 (0)