Skip to content

perf(db): index mailbox list sort and recipient lookup - #13502

Open
ChristophWurst wants to merge 1 commit into
mainfrom
perf/db/missing-indexes
Open

perf(db): index mailbox list sort and recipient lookup#13502
ChristophWurst wants to merge 1 commit into
mainfrom
perf/db/missing-indexes

Conversation

@ChristophWurst

Copy link
Copy Markdown
Member

The mailbox list query (mailbox_id + flag_deleted, ORDER BY sent_at) had no covering index and fell back to a filesort; the standalone sent_at index was dropped in 2022 and never restored. mail_recipients.local_message_id had a foreign key but no backing index, so lookups and deletes scanned the whole table on Postgres and SQLite.

Register both via AddMissingIndicesEvent so existing installations add them out-of-band through occ db:add-missing-indices, and add them to the creating migrations so new installations get them up front.

Assisted-by: Claude:claude-opus-4-8

How to test

EXPLAIN SELECT m.id, m.sent_at FROM oc_mail_messages m
        WHERE m.mailbox_id = 9658 AND m.flag_deleted = 0
        ORDER BY m.sent_at DESC LIMIT 20;

EXPLAIN SELECT m.id, m.sent_at
        FROM oc_mail_messages m
        WHERE m.mailbox_id = 8464          
          AND m.flag_deleted = 0
          AND m.sent_at < 1429201770           
        ORDER BY m.sent_at DESC
        LIMIT 20;

^ replace IDs with something existent of your local db.

Before:

  1. 1|SIMPLE|m|ref|mail_messages_mb_id_uid_uidx,mail_msg_thrd_root_snt_idx,mail_msg_by_remote_id_idx|mail_messages_mb_id_uid_uidx|4|const|1|Using where; Using filesort
  2. 1|SIMPLE|m|ref|mail_messages_mb_id_uid_uidx,mail_msg_thrd_root_snt_idx,mail_msg_by_remote_id_idx|mail_messages_mb_id_uid_uidx|4|const|525|Using where; Using filesort
    After
  3. 1|SIMPLE|m|ref|mail_messages_mb_id_uid_uidx,mail_msg_thrd_root_snt_idx,mail_msg_by_remote_id_idx,mail_msg_mb_del_snt_idx|mail_msg_mb_del_snt_idx|6|const,const|1|Using where; Using index
  4. 1|SIMPLE|m|range|mail_messages_mb_id_uid_uidx,mail_msg_thrd_root_snt_idx,mail_msg_by_remote_id_idx,mail_msg_mb_del_snt_idx|mail_msg_mb_del_snt_idx|10||51|Using where; Using index

-> no more filesort

馃 AI (if applicable)

  • The content of this PR was partly or fully generated using AI

The mailbox list query (mailbox_id + flag_deleted, ORDER BY sent_at) had no covering index and fell back to a filesort; the standalone sent_at index was dropped in 2022 and never restored. mail_recipients.local_message_id had a foreign key but no backing index, so lookups and deletes scanned the whole table on Postgres and SQLite.

Register both via AddMissingIndicesEvent so existing installations add them out-of-band through occ db:add-missing-indices, and add them to the creating migrations so new installations get them up front.

Assisted-by: Claude:claude-opus-4-8
Signed-off-by: Christoph Wurst <1374172+ChristophWurst@users.noreply.github.com>
@ChristophWurst ChristophWurst self-assigned this Aug 17, 2026
@nextcloud-command nextcloud-command added the AI assisted This PR contains AI-assisted commits label Aug 17, 2026
@ChristophWurst

Copy link
Copy Markdown
Member Author

@CodeRabbit ai review and evaluate the before/after of the PR description against the code base

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3. to review AI assisted This PR contains AI-assisted commits performance 馃殌

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants