diff --git a/packages/db/postgres/functions.sql b/packages/db/postgres/functions.sql index 790b495e0f..7a0f7eae40 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::BIGINT ELSE 0::BIGINT 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