From 817b051bf5f78764f6e318e14a85f8dae62769c7 Mon Sep 17 00:00:00 2001 From: Christoph Wurst <1374172+ChristophWurst@users.noreply.github.com> Date: Wed, 2 Sep 2026 15:04:08 +0200 Subject: [PATCH] perf(db): index the iMIP invitation scan IMipService's cron (every ~300s) ran findIMipMessagesAscending, which filtered mail_messages on imip_message plus three flags with a sent_at window and ORDER BY sent_at. No index matched, so every run full-scanned the whole table (~944k rows) and filesorted, usually to return nothing. It was #2 by rows examined on a 903-account instance, 100% full scans. Add mail_msg_imip_idx (imip_message, sent_at): the rare imip_message value makes the leading column selective (1.45% of rows) and sent_at removes the filesort. The mutable flags (imip_processed, imip_error, flag_junk) are left out on purpose to avoid index churn; imip_message flips at most once. Registered via AddMissingIndicesEvent for existing installs and added to the 2022 message-index migration for new ones. Assisted-by: Claude:claude-opus-4-8 Signed-off-by: Christoph Wurst <1374172+ChristophWurst@users.noreply.github.com> Signed-off-by: Daniel Kesselberg --- lib/Listener/OptionalIndicesListener.php | 6 ++++++ lib/Migration/Version1130Date20220412111833.php | 3 +++ tests/Unit/Listener/OptionalIndicesListenerTest.php | 1 + 3 files changed, 10 insertions(+) diff --git a/lib/Listener/OptionalIndicesListener.php b/lib/Listener/OptionalIndicesListener.php index 40046dc267..821912c252 100644 --- a/lib/Listener/OptionalIndicesListener.php +++ b/lib/Listener/OptionalIndicesListener.php @@ -60,6 +60,12 @@ public function handle(Event $event): void { ['mailbox_id', 'flag_deleted', 'sent_at'], ); + $event->addMissingIndex( + 'mail_messages', + 'mail_msg_imip_idx', + ['imip_message', 'sent_at'], + ); + $event->addMissingIndex( 'mail_classifiers', 'mail_class_creat_idx', diff --git a/lib/Migration/Version1130Date20220412111833.php b/lib/Migration/Version1130Date20220412111833.php index 69b6070f09..b2a830f874 100644 --- a/lib/Migration/Version1130Date20220412111833.php +++ b/lib/Migration/Version1130Date20220412111833.php @@ -176,6 +176,9 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt // mail_msg_mb_del_snt_idx was added later and may not exist until optional indices are created $messagesTable->addIndex(['mailbox_id', 'flag_deleted', 'sent_at'], 'mail_msg_mb_del_snt_idx'); + // mail_msg_imip_idx was added later and may not exist until optional indices are created + $messagesTable->addIndex(['imip_message', 'sent_at'], 'mail_msg_imip_idx'); + return $schema; } diff --git a/tests/Unit/Listener/OptionalIndicesListenerTest.php b/tests/Unit/Listener/OptionalIndicesListenerTest.php index 0ee6931a67..dc650e1ded 100644 --- a/tests/Unit/Listener/OptionalIndicesListenerTest.php +++ b/tests/Unit/Listener/OptionalIndicesListenerTest.php @@ -52,5 +52,6 @@ public function testHandleRegistersOptionalIndices(): void { $indexNames = array_column($event->getMissingIndices(), 'indexName'); self::assertContains('mail_msg_mb_del_snt_idx', $indexNames); + self::assertContains('mail_msg_imip_idx', $indexNames); } }