From f168eb8bdbfd4757c2ea52a549a6d2783a1d6a02 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Mon, 15 Nov 2021 11:41:51 +0000 Subject: [PATCH 1/2] fix: add indexes and optimise token list function --- packages/db/postgres/functions.sql | 16 +++++++--------- packages/db/postgres/tables.sql | 4 ++++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/db/postgres/functions.sql b/packages/db/postgres/functions.sql index 790b495e0f..db66cd7cbf 100644 --- a/packages/db/postgres/functions.sql +++ b/packages/db/postgres/functions.sql @@ -207,15 +207,13 @@ CREATE OR REPLACE FUNCTION user_keys_list(query_user_id BIGINT) LANGUAGE sql AS $$ -select (ak.id)::TEXT as id, - ak.name as name, - ak.secret as secret, - ak.inserted_at as created, - count(u.id) as uploads -from auth_key ak -left outer join upload u on ak.id = u.auth_key_id -where ak.user_id = query_user_id and u.deleted_at is null and ak.deleted_at is null -group by ak.id +SELECT (ak.id)::TEXT AS id, + ak.name AS name, + ak.secret AS secret, + ak.inserted_at AS created, + CASE WHEN EXISTS(SELECT 42 FROM upload u WHERE ak.id = u.auth_key_id AND u.deleted_at IS NULL) THEN 1 ELSE 0 END + FROM auth_key ak + WHERE ak.user_id = query_user_id AND ak.deleted_at IS NULL $$; CREATE OR REPLACE FUNCTION content_dag_size_total() RETURNS TEXT diff --git a/packages/db/postgres/tables.sql b/packages/db/postgres/tables.sql index e4c613af7f..2f6e160c0d 100644 --- a/packages/db/postgres/tables.sql +++ b/packages/db/postgres/tables.sql @@ -32,6 +32,8 @@ CREATE TABLE IF NOT EXISTS auth_key deleted_at TIMESTAMP WITH TIME ZONE ); +CREATE INDEX IF NOT EXISTS auth_key_user_id_idx ON auth_key (user_id); + -- Details of the root of a file/directory stored on web3.storage. CREATE TABLE IF NOT EXISTS content ( @@ -105,6 +107,7 @@ CREATE TABLE IF NOT EXISTS pin ); CREATE INDEX IF NOT EXISTS pin_updated_at_idx ON pin (updated_at); +CREATE INDEX IF NOT EXISTS pin_status_idx ON pin (status); -- Upload type is the type of received upload data. CREATE TYPE upload_type AS ENUM @@ -143,6 +146,7 @@ CREATE TABLE IF NOT EXISTS upload ); CREATE INDEX IF NOT EXISTS upload_updated_at_idx ON upload (updated_at); +CREATE INDEX IF NOT EXISTS upload_auth_key_id_idx ON upload (auth_key_id); -- Details of the backups created for an upload. CREATE TABLE IF NOT EXISTS backup From 4ffa2f60b24de4300bc823abc30b7ed9503334db Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Mon, 15 Nov 2021 12:01:55 +0000 Subject: [PATCH 2/2] fix: cast as bigint --- packages/db/postgres/functions.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/db/postgres/functions.sql b/packages/db/postgres/functions.sql index db66cd7cbf..7a0f7eae40 100644 --- a/packages/db/postgres/functions.sql +++ b/packages/db/postgres/functions.sql @@ -211,7 +211,7 @@ SELECT (ak.id)::TEXT AS id, ak.name AS name, ak.secret AS secret, ak.inserted_at AS created, - CASE WHEN EXISTS(SELECT 42 FROM upload u WHERE ak.id = u.auth_key_id AND u.deleted_at IS NULL) THEN 1 ELSE 0 END + CASE WHEN EXISTS(SELECT 42 FROM upload u WHERE ak.id = u.auth_key_id AND u.deleted_at IS NULL) THEN 1::BIGINT ELSE 0::BIGINT END FROM auth_key ak WHERE ak.user_id = query_user_id AND ak.deleted_at IS NULL $$;