From 60f4f863dfb80932da2d31accf7fb5039b15e668 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Fri, 12 Feb 2021 15:57:02 +0100 Subject: [PATCH] :sparkles: Add missing indexes and improve others. --- CHANGES.md | 3 +++ backend/src/app/migrations.clj | 6 ++++++ .../migrations/sql/0035-add-storage-tables.sql | 11 ++++++----- .../sql/0047-mod-file-change-table.sql | 16 ++++++++++++++++ .../migrations/sql/0048-mod-storage-tables.sql | 9 +++++++++ 5 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 backend/src/app/migrations/sql/0047-mod-file-change-table.sql create mode 100644 backend/src/app/migrations/sql/0048-mod-storage-tables.sql diff --git a/CHANGES.md b/CHANGES.md index 326b99e2c..6ebc27f3d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -15,6 +15,9 @@ - Properly handle errors on github, gitlab and ldap auth backends. - Properly mark profile auth backend (on first register/ auth with 3rd party auth provider). - Fix problem width handoff code generation [Taiga #1204](https://tree.taiga.io/project/penpot/issue/1204) +- Add some missing database indexes (mainly improves performance on + large databases on file-update rpc method, and some background + tasks). ## 1.2.0-alpha diff --git a/backend/src/app/migrations.clj b/backend/src/app/migrations.clj index 7b0a19e6c..f124bebb9 100644 --- a/backend/src/app/migrations.clj +++ b/backend/src/app/migrations.clj @@ -152,6 +152,12 @@ {:name "0046-add-profile-complaint-table" :fn (mg/resource "app/migrations/sql/0046-add-profile-complaint-table.sql")} + {:name "0047-mod-file-change-table" + :fn (mg/resource "app/migrations/sql/0047-mod-file-change-table.sql")} + + {:name "0048-mod-storage-tables" + :fn (mg/resource "app/migrations/sql/0048-mod-storage-tables.sql")} + ]) diff --git a/backend/src/app/migrations/sql/0035-add-storage-tables.sql b/backend/src/app/migrations/sql/0035-add-storage-tables.sql index 4bf96725d..d1ec7f9d4 100644 --- a/backend/src/app/migrations/sql/0035-add-storage-tables.sql +++ b/backend/src/app/migrations/sql/0035-add-storage-tables.sql @@ -10,11 +10,17 @@ CREATE TABLE storage_object ( metadata jsonb NULL DEFAULT NULL ); +CREATE INDEX storage_object__id__deleted_at__idx + ON storage_object(id, deleted_at) + WHERE deleted_at IS NOT null; + CREATE TABLE storage_data ( id uuid PRIMARY KEY REFERENCES storage_object (id) ON DELETE CASCADE, data bytea NOT NULL ); +CREATE INDEX storage_data__id__idx ON storage_data(id); + -- Table used for store inflight upload ids, for later recheck and -- delete possible staled files that exists on the phisical storage -- but does not exists in the 'storage_object' table. @@ -28,8 +34,3 @@ CREATE TABLE storage_pending ( PRIMARY KEY (created_at, id) ); -CREATE INDEX storage_data__id__idx ON storage_data(id); -CREATE INDEX storage_object__id__deleted_at__idx - ON storage_object(id, deleted_at) - WHERE deleted_at IS NOT null; - diff --git a/backend/src/app/migrations/sql/0047-mod-file-change-table.sql b/backend/src/app/migrations/sql/0047-mod-file-change-table.sql new file mode 100644 index 000000000..01bb0f092 --- /dev/null +++ b/backend/src/app/migrations/sql/0047-mod-file-change-table.sql @@ -0,0 +1,16 @@ +--- Helps on the lagged changes query on update-file rpc +CREATE INDEX file_change__file_id__revn__idx ON file_change (file_id, revn); + +--- Drop redundant index +DROP INDEX page_change_file_id_idx; + +--- Add profile_id field. +ALTER TABLE file_change + ADD COLUMN profile_id uuid NULL REFERENCES profile (id) ON DELETE SET NULL; + +CREATE INDEX file_change__profile_id__idx + ON file_change (profile_id) + WHERE profile_id IS NOT NULL; + +--- Fix naming +ALTER INDEX file_change__created_at_idx RENAME TO file_change__created_at__idx; diff --git a/backend/src/app/migrations/sql/0048-mod-storage-tables.sql b/backend/src/app/migrations/sql/0048-mod-storage-tables.sql new file mode 100644 index 000000000..eb9fa8fed --- /dev/null +++ b/backend/src/app/migrations/sql/0048-mod-storage-tables.sql @@ -0,0 +1,9 @@ +--- Drop redundant index already covered by primary key +DROP INDEX storage_data__id__idx; + +--- Replace not efficient index with more efficient one +DROP INDEX storage_object__id__deleted_at__idx; + +CREATE INDEX storage_object__id__deleted_at__idx + ON storage_object(deleted_at, id) + WHERE deleted_at IS NOT NULL;