Skip to content

Commit

Permalink
fix: add indexes and optimise token list function (#618)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw authored Nov 15, 2021
1 parent 11f394c commit 05ff3ed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
16 changes: 7 additions & 9 deletions packages/db/postgres/functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions packages/db/postgres/tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 05ff3ed

Please sign in to comment.