-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(jobsdb): latest job status query optimization
- Loading branch information
Showing
7 changed files
with
86 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
sql/migrations/jobsdb/000010_add_index_job_status_table.down.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{{range .Datasets}} | ||
DROP INDEX IF EXISTS "idx_{{$.Prefix}}_job_status_{{.}}_jid_id"; | ||
DROP VIEW IF EXISTS "v_last_{{$.Prefix}}_job_status_{{.}}"; | ||
{{end}} |
4 changes: 4 additions & 0 deletions
4
sql/migrations/jobsdb/000010_add_index_job_status_table.up.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{{range .Datasets}} | ||
CREATE INDEX IF NOT EXISTS "idx_{{$.Prefix}}_job_status_{{.}}_jid_id" ON "{{$.Prefix}}_job_status_{{.}}" (job_id asc,id desc); | ||
CREATE OR REPLACE VIEW "v_last_{{$.Prefix}}_job_status_{{.}}" AS SELECT DISTINCT ON (job_id) * FROM "{{$.Prefix}}_job_status_{{.}}" ORDER BY job_id ASC,id DESC; | ||
{{end}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP FUNCTION IF EXISTS unionjobsdb(prefix text, num int); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
-- unionjobsdb function automatically joins datasets and returns jobs | ||
-- along with their latest jobs status (or null) | ||
-- | ||
-- Parameters | ||
-- prefix: table prefix, e.g. gw, rt, batch_rt | ||
-- num: number of datasets to include in the query, e.g. 4 | ||
CREATE OR REPLACE FUNCTION unionjobsdb(prefix text, num int) | ||
RETURNS table ( | ||
t_name text, | ||
job_id bigint, | ||
workspace_id text, | ||
uuid uuid, | ||
user_id text, | ||
parameters jsonb, | ||
custom_val character varying(64), | ||
event_payload jsonb, | ||
event_count integer, | ||
created_at timestamp with time zone, | ||
expire_at timestamp with time zone, | ||
status_id bigint, | ||
job_state character varying(64), | ||
attempt smallint, | ||
error_code character varying(32), | ||
error_response jsonb | ||
) | ||
AS $$ | ||
DECLARE | ||
qry text; | ||
BEGIN | ||
SELECT string_agg( | ||
format('SELECT %1$L, j.job_id, j.workspace_id, j.uuid, j.user_id, j.parameters, j.custom_val, j.event_payload, j.event_count, j.created_at, j.expire_at, latest_status.id, latest_status.job_state, latest_status.attempt, latest_status.error_code, latest_status.error_response FROM %1$I j LEFT JOIN %2$I latest_status on latest_status.job_id = j.job_id', alltables.table_name, 'v_last_' || prefix || '_job_status_'|| substring(alltables.table_name, char_length(prefix)+7,30)), | ||
' UNION ') INTO qry | ||
FROM (select table_name from information_schema.tables | ||
WHERE table_name LIKE prefix || '_jobs_%' order by table_name asc LIMIT num) alltables; | ||
RETURN QUERY EXECUTE qry; | ||
END; | ||
$$ LANGUAGE plpgsql; |