From e089e5d947ca500bd7fc27472f1a1c0633592a38 Mon Sep 17 00:00:00 2001 From: BryanFRD Date: Wed, 2 Sep 2026 12:47:59 +0200 Subject: [PATCH 1/2] perf(db): add indexes on foreign key and lookup columns --- .../2026-09-02-120000_add_indexes/down.sql | 17 ++++++++++++++++ .../2026-09-02-120000_add_indexes/up.sql | 20 +++++++++++++++++++ .../2026-09-02-120000_add_indexes/down.sql | 18 +++++++++++++++++ .../2026-09-02-120000_add_indexes/up.sql | 18 +++++++++++++++++ .../2026-09-02-120000_add_indexes/down.sql | 18 +++++++++++++++++ .../2026-09-02-120000_add_indexes/up.sql | 18 +++++++++++++++++ 6 files changed, 109 insertions(+) create mode 100644 migrations/mysql/2026-09-02-120000_add_indexes/down.sql create mode 100644 migrations/mysql/2026-09-02-120000_add_indexes/up.sql create mode 100644 migrations/postgresql/2026-09-02-120000_add_indexes/down.sql create mode 100644 migrations/postgresql/2026-09-02-120000_add_indexes/up.sql create mode 100644 migrations/sqlite/2026-09-02-120000_add_indexes/down.sql create mode 100644 migrations/sqlite/2026-09-02-120000_add_indexes/up.sql diff --git a/migrations/mysql/2026-09-02-120000_add_indexes/down.sql b/migrations/mysql/2026-09-02-120000_add_indexes/down.sql new file mode 100644 index 00000000..22eca210 --- /dev/null +++ b/migrations/mysql/2026-09-02-120000_add_indexes/down.sql @@ -0,0 +1,17 @@ +DROP INDEX idx_ciphers_user_uuid ON ciphers; +DROP INDEX idx_ciphers_organization_uuid ON ciphers; +DROP INDEX idx_attachments_cipher_uuid ON attachments; +DROP INDEX idx_folders_user_uuid ON folders; +DROP INDEX idx_folders_ciphers_folder_uuid ON folders_ciphers; +DROP INDEX idx_ciphers_collections_collection_uuid ON ciphers_collections; +DROP INDEX idx_favorites_cipher_uuid ON favorites; +DROP INDEX idx_devices_user_uuid ON devices; +DROP INDEX idx_collections_org_uuid ON collections; +DROP INDEX idx_users_collections_collection_uuid ON users_collections; +DROP INDEX idx_users_organizations_org_uuid ON users_organizations; +DROP INDEX idx_groups_organizations_uuid ON `groups`; +DROP INDEX idx_groups_users_users_organizations_uuid ON groups_users; +DROP INDEX idx_collections_groups_groups_uuid ON collections_groups; +DROP INDEX idx_event_org_uuid_event_date ON event; +DROP INDEX idx_event_cipher_uuid_event_date ON event; +DROP INDEX idx_event_event_date ON event; diff --git a/migrations/mysql/2026-09-02-120000_add_indexes/up.sql b/migrations/mysql/2026-09-02-120000_add_indexes/up.sql new file mode 100644 index 00000000..412bf96c --- /dev/null +++ b/migrations/mysql/2026-09-02-120000_add_indexes/up.sql @@ -0,0 +1,20 @@ +-- archives.cipher_uuid is left out: it is declared as an explicit FOREIGN KEY, +-- so InnoDB already maintains an index for it. Every other table below uses +-- inline REFERENCES, which MySQL parses and ignores, so no index exists. +CREATE INDEX idx_ciphers_user_uuid ON ciphers (user_uuid); +CREATE INDEX idx_ciphers_organization_uuid ON ciphers (organization_uuid); +CREATE INDEX idx_attachments_cipher_uuid ON attachments (cipher_uuid); +CREATE INDEX idx_folders_user_uuid ON folders (user_uuid); +CREATE INDEX idx_folders_ciphers_folder_uuid ON folders_ciphers (folder_uuid); +CREATE INDEX idx_ciphers_collections_collection_uuid ON ciphers_collections (collection_uuid); +CREATE INDEX idx_favorites_cipher_uuid ON favorites (cipher_uuid); +CREATE INDEX idx_devices_user_uuid ON devices (user_uuid); +CREATE INDEX idx_collections_org_uuid ON collections (org_uuid); +CREATE INDEX idx_users_collections_collection_uuid ON users_collections (collection_uuid); +CREATE INDEX idx_users_organizations_org_uuid ON users_organizations (org_uuid); +CREATE INDEX idx_groups_organizations_uuid ON `groups` (organizations_uuid); +CREATE INDEX idx_groups_users_users_organizations_uuid ON groups_users (users_organizations_uuid); +CREATE INDEX idx_collections_groups_groups_uuid ON collections_groups (groups_uuid); +CREATE INDEX idx_event_org_uuid_event_date ON event (org_uuid, event_date); +CREATE INDEX idx_event_cipher_uuid_event_date ON event (cipher_uuid, event_date); +CREATE INDEX idx_event_event_date ON event (event_date); diff --git a/migrations/postgresql/2026-09-02-120000_add_indexes/down.sql b/migrations/postgresql/2026-09-02-120000_add_indexes/down.sql new file mode 100644 index 00000000..3c4690c0 --- /dev/null +++ b/migrations/postgresql/2026-09-02-120000_add_indexes/down.sql @@ -0,0 +1,18 @@ +DROP INDEX idx_ciphers_user_uuid; +DROP INDEX idx_ciphers_organization_uuid; +DROP INDEX idx_attachments_cipher_uuid; +DROP INDEX idx_folders_user_uuid; +DROP INDEX idx_folders_ciphers_folder_uuid; +DROP INDEX idx_ciphers_collections_collection_uuid; +DROP INDEX idx_favorites_cipher_uuid; +DROP INDEX idx_archives_cipher_uuid; +DROP INDEX idx_devices_user_uuid; +DROP INDEX idx_collections_org_uuid; +DROP INDEX idx_users_collections_collection_uuid; +DROP INDEX idx_users_organizations_org_uuid; +DROP INDEX idx_groups_organizations_uuid; +DROP INDEX idx_groups_users_users_organizations_uuid; +DROP INDEX idx_collections_groups_groups_uuid; +DROP INDEX idx_event_org_uuid_event_date; +DROP INDEX idx_event_cipher_uuid_event_date; +DROP INDEX idx_event_event_date; diff --git a/migrations/postgresql/2026-09-02-120000_add_indexes/up.sql b/migrations/postgresql/2026-09-02-120000_add_indexes/up.sql new file mode 100644 index 00000000..c1059b34 --- /dev/null +++ b/migrations/postgresql/2026-09-02-120000_add_indexes/up.sql @@ -0,0 +1,18 @@ +CREATE INDEX idx_ciphers_user_uuid ON ciphers (user_uuid); +CREATE INDEX idx_ciphers_organization_uuid ON ciphers (organization_uuid); +CREATE INDEX idx_attachments_cipher_uuid ON attachments (cipher_uuid); +CREATE INDEX idx_folders_user_uuid ON folders (user_uuid); +CREATE INDEX idx_folders_ciphers_folder_uuid ON folders_ciphers (folder_uuid); +CREATE INDEX idx_ciphers_collections_collection_uuid ON ciphers_collections (collection_uuid); +CREATE INDEX idx_favorites_cipher_uuid ON favorites (cipher_uuid); +CREATE INDEX idx_archives_cipher_uuid ON archives (cipher_uuid); +CREATE INDEX idx_devices_user_uuid ON devices (user_uuid); +CREATE INDEX idx_collections_org_uuid ON collections (org_uuid); +CREATE INDEX idx_users_collections_collection_uuid ON users_collections (collection_uuid); +CREATE INDEX idx_users_organizations_org_uuid ON users_organizations (org_uuid); +CREATE INDEX idx_groups_organizations_uuid ON groups (organizations_uuid); +CREATE INDEX idx_groups_users_users_organizations_uuid ON groups_users (users_organizations_uuid); +CREATE INDEX idx_collections_groups_groups_uuid ON collections_groups (groups_uuid); +CREATE INDEX idx_event_org_uuid_event_date ON event (org_uuid, event_date); +CREATE INDEX idx_event_cipher_uuid_event_date ON event (cipher_uuid, event_date); +CREATE INDEX idx_event_event_date ON event (event_date); diff --git a/migrations/sqlite/2026-09-02-120000_add_indexes/down.sql b/migrations/sqlite/2026-09-02-120000_add_indexes/down.sql new file mode 100644 index 00000000..3c4690c0 --- /dev/null +++ b/migrations/sqlite/2026-09-02-120000_add_indexes/down.sql @@ -0,0 +1,18 @@ +DROP INDEX idx_ciphers_user_uuid; +DROP INDEX idx_ciphers_organization_uuid; +DROP INDEX idx_attachments_cipher_uuid; +DROP INDEX idx_folders_user_uuid; +DROP INDEX idx_folders_ciphers_folder_uuid; +DROP INDEX idx_ciphers_collections_collection_uuid; +DROP INDEX idx_favorites_cipher_uuid; +DROP INDEX idx_archives_cipher_uuid; +DROP INDEX idx_devices_user_uuid; +DROP INDEX idx_collections_org_uuid; +DROP INDEX idx_users_collections_collection_uuid; +DROP INDEX idx_users_organizations_org_uuid; +DROP INDEX idx_groups_organizations_uuid; +DROP INDEX idx_groups_users_users_organizations_uuid; +DROP INDEX idx_collections_groups_groups_uuid; +DROP INDEX idx_event_org_uuid_event_date; +DROP INDEX idx_event_cipher_uuid_event_date; +DROP INDEX idx_event_event_date; diff --git a/migrations/sqlite/2026-09-02-120000_add_indexes/up.sql b/migrations/sqlite/2026-09-02-120000_add_indexes/up.sql new file mode 100644 index 00000000..c1059b34 --- /dev/null +++ b/migrations/sqlite/2026-09-02-120000_add_indexes/up.sql @@ -0,0 +1,18 @@ +CREATE INDEX idx_ciphers_user_uuid ON ciphers (user_uuid); +CREATE INDEX idx_ciphers_organization_uuid ON ciphers (organization_uuid); +CREATE INDEX idx_attachments_cipher_uuid ON attachments (cipher_uuid); +CREATE INDEX idx_folders_user_uuid ON folders (user_uuid); +CREATE INDEX idx_folders_ciphers_folder_uuid ON folders_ciphers (folder_uuid); +CREATE INDEX idx_ciphers_collections_collection_uuid ON ciphers_collections (collection_uuid); +CREATE INDEX idx_favorites_cipher_uuid ON favorites (cipher_uuid); +CREATE INDEX idx_archives_cipher_uuid ON archives (cipher_uuid); +CREATE INDEX idx_devices_user_uuid ON devices (user_uuid); +CREATE INDEX idx_collections_org_uuid ON collections (org_uuid); +CREATE INDEX idx_users_collections_collection_uuid ON users_collections (collection_uuid); +CREATE INDEX idx_users_organizations_org_uuid ON users_organizations (org_uuid); +CREATE INDEX idx_groups_organizations_uuid ON groups (organizations_uuid); +CREATE INDEX idx_groups_users_users_organizations_uuid ON groups_users (users_organizations_uuid); +CREATE INDEX idx_collections_groups_groups_uuid ON collections_groups (groups_uuid); +CREATE INDEX idx_event_org_uuid_event_date ON event (org_uuid, event_date); +CREATE INDEX idx_event_cipher_uuid_event_date ON event (cipher_uuid, event_date); +CREATE INDEX idx_event_event_date ON event (event_date); From 7c2cc4c43b9a33d4476ce64ac91eead4655c42b5 Mon Sep 17 00:00:00 2001 From: BryanFRD Date: Sat, 5 Sep 2026 23:24:35 +0200 Subject: [PATCH 2/2] fix(db): allow the index migration to be reverted on MariaDB --- .../mysql/2026-09-02-120000_add_indexes/down.sql | 4 ++++ migrations/mysql/2026-09-02-120000_add_indexes/up.sql | 10 +++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/migrations/mysql/2026-09-02-120000_add_indexes/down.sql b/migrations/mysql/2026-09-02-120000_add_indexes/down.sql index 22eca210..e51c9fc5 100644 --- a/migrations/mysql/2026-09-02-120000_add_indexes/down.sql +++ b/migrations/mysql/2026-09-02-120000_add_indexes/down.sql @@ -1,3 +1,7 @@ +-- On MariaDB these indexes back the foreign keys created from the inline REFERENCES, +-- and dropping them is refused with error 1553 while the constraints are enforced. +SET FOREIGN_KEY_CHECKS = 0; + DROP INDEX idx_ciphers_user_uuid ON ciphers; DROP INDEX idx_ciphers_organization_uuid ON ciphers; DROP INDEX idx_attachments_cipher_uuid ON attachments; diff --git a/migrations/mysql/2026-09-02-120000_add_indexes/up.sql b/migrations/mysql/2026-09-02-120000_add_indexes/up.sql index 412bf96c..a1407960 100644 --- a/migrations/mysql/2026-09-02-120000_add_indexes/up.sql +++ b/migrations/mysql/2026-09-02-120000_add_indexes/up.sql @@ -1,6 +1,10 @@ --- archives.cipher_uuid is left out: it is declared as an explicit FOREIGN KEY, --- so InnoDB already maintains an index for it. Every other table below uses --- inline REFERENCES, which MySQL parses and ignores, so no index exists. +-- archives.cipher_uuid is left out: it is declared as an explicit FOREIGN KEY, so both +-- MySQL and MariaDB already maintain an index for it. +-- +-- Every other table below uses inline REFERENCES. MySQL parses and ignores those, so no +-- index exists there. MariaDB honours them and backs each one with an auto-named index, +-- which InnoDB drops once the named index below can serve the constraint. Both engines +-- therefore end up with exactly one index per column. CREATE INDEX idx_ciphers_user_uuid ON ciphers (user_uuid); CREATE INDEX idx_ciphers_organization_uuid ON ciphers (organization_uuid); CREATE INDEX idx_attachments_cipher_uuid ON attachments (cipher_uuid);