From 4c356d6995d1ce6a38763f855e309dfb02e37eeb Mon Sep 17 00:00:00 2001 From: Johannes Merkel Date: Mon, 5 Feb 2024 20:03:11 +0100 Subject: [PATCH] perf(db): optimise indices of mail tables Signed-off-by: Johannes Merkel --- lib/Listener/OptionalIndicesListener.php | 58 ++++++++++++++++ .../Version1100Date20210304143008.php | 3 +- .../Version1130Date20220412111833.php | 4 +- .../Version2300Date20221216115727.php | 3 +- .../Version3600Date20240205180726.php | 66 +++++++++++++++++++ 5 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 lib/Migration/Version3600Date20240205180726.php diff --git a/lib/Listener/OptionalIndicesListener.php b/lib/Listener/OptionalIndicesListener.php index 2036b28ad8..78e6893159 100644 --- a/lib/Listener/OptionalIndicesListener.php +++ b/lib/Listener/OptionalIndicesListener.php @@ -57,6 +57,64 @@ public function handle(Event $event): void { ], ); } + + $event->addMissingIndex( + 'mail_messages', + 'mail_messages_strucanalyz_idx', + ['structure_analyzed'] + ); + + $event->addMissingIndex( + 'mail_classifiers', + 'mail_class_creat_idx', + ['created_at'] + ); + + $event->addMissingIndex( + 'mail_accounts', + 'mail_acc_prov_idx', + ['provisioning_id'] + ); + + $event->addMissingIndex( + 'mail_aliases', + 'mail_alias_accid_idx', + ['account_id'] + ); + + if (method_exists($event, 'replaceIndex')) { + $event->replaceIndex( + 'mail_messages', + ['mail_messages_mb_id_uid'], + 'mail_messages_mb_id_uid_uidx', + ['mailbox_id', 'uid'], + true + ); + + $event->replaceIndex( + 'mail_smime_certificates', + ['mail_smime_certs_uid_idx'], + 'mail_smime_certs_uid_email_idx', + ['user_id', 'email_address'], + false + ); + + $event->replaceIndex( + 'mail_trusted_senders', + ['mail_trusted_senders_type'], + 'mail_trusted_senders_idx', + ['user_id', 'email', 'type'], + false + ); + + $event->replaceIndex( + 'mail_coll_addresses', + ['mail_coll_addr_userid_index', 'mail_coll_addr_email_index'], + 'mail_coll_idx', + ['user_id', 'email', 'display_name'], + false + ); + } } } diff --git a/lib/Migration/Version1100Date20210304143008.php b/lib/Migration/Version1100Date20210304143008.php index 34f83ebdd4..f5dfc680fb 100644 --- a/lib/Migration/Version1100Date20210304143008.php +++ b/lib/Migration/Version1100Date20210304143008.php @@ -52,7 +52,8 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt 'default' => false ]); $tagsTable->setPrimaryKey(['id']); - $tagsTable->addIndex(['user_id'], 'mail_msg_tags_usr_id_index'); + // Dropped in Version3600Date20240205180726 because mail_msg_tags_usr_id_index is redundant with mail_msg_tags_usr_lbl_idx + // $tagsTable->addIndex(['user_id'], 'mail_msg_tags_usr_id_index'); $tagsTable->addUniqueIndex( [ 'user_id', diff --git a/lib/Migration/Version1130Date20220412111833.php b/lib/Migration/Version1130Date20220412111833.php index 5851ce155f..e9de99ffb1 100644 --- a/lib/Migration/Version1130Date20220412111833.php +++ b/lib/Migration/Version1130Date20220412111833.php @@ -167,7 +167,9 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt // Add named indices $messagesTable->addIndex(['mailbox_id', 'flag_important', 'flag_deleted', 'flag_seen'], 'mail_messages_id_flags'); $messagesTable->addIndex(['mailbox_id', 'flag_deleted', 'flag_flagged'], 'mail_messages_id_flags2'); - $messagesTable->addIndex(['mailbox_id'], 'mail_messages_mailbox_id'); + // Dropped in Version3600Date20240205180726 because mail_messages_mailbox_id is redundant with mail_messages_mb_id_uid + // $messagesTable->addIndex(['mailbox_id'], 'mail_messages_mailbox_id'); + // mail_messages_msgid_idx was added later and may not exist until optional indices are created $messagesTable->addIndex( ['message_id'], diff --git a/lib/Migration/Version2300Date20221216115727.php b/lib/Migration/Version2300Date20221216115727.php index c27b01916e..ae910d834d 100644 --- a/lib/Migration/Version2300Date20221216115727.php +++ b/lib/Migration/Version2300Date20221216115727.php @@ -67,7 +67,8 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt ]); $table->setPrimaryKey(['id'], 'mail_smime_certs_id_idx'); $table->addIndex(['user_id'], 'mail_smime_certs_uid_idx'); - $table->addIndex(['id', 'user_id'], 'mail_smime_certs_id_uid_idx'); + // Dropped in Version3600Date20240205180726 + // $table->addIndex(['id', 'user_id'], 'mail_smime_certs_id_uid_idx'); } return $schema; diff --git a/lib/Migration/Version3600Date20240205180726.php b/lib/Migration/Version3600Date20240205180726.php new file mode 100644 index 0000000000..0dbd4e6343 --- /dev/null +++ b/lib/Migration/Version3600Date20240205180726.php @@ -0,0 +1,66 @@ + + * + * @author Johannes Merkel + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Mail\Migration; + +use Closure; +use OCP\DB\ISchemaWrapper; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +class Version3600Date20240205180726 extends SimpleMigrationStep { + + /** + * @param IOutput $output + * @param Closure(): ISchemaWrapper $schemaClosure + * @param array $options + * @return null|ISchemaWrapper + */ + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + $mailboxesTable = $schema->getTable('mail_messages'); + + if ($mailboxesTable->hasIndex('mail_messages_mailbox_id')) { + $mailboxesTable->dropIndex('mail_messages_mailbox_id'); + } + + $mailboxesTable = $schema->getTable('mail_smime_certificates'); + + if ($mailboxesTable->hasIndex('mail_smime_certs_id_uid_idx')) { + $mailboxesTable->dropIndex('mail_smime_certs_id_uid_idx'); + } + + $mailboxesTable = $schema->getTable('mail_tags'); + + if ($mailboxesTable->hasIndex('mail_msg_tags_usr_id_index')) { + $mailboxesTable->dropIndex('mail_msg_tags_usr_id_index'); + } + + return $schema; + } +}