Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add indexes and optimise token list function #618

Merged
merged 2 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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