Inline task_states for better performance

backend-refactor
Sven Slootweg 4 years ago
parent 731e41996b
commit ab227537dd

@ -12,38 +12,33 @@ const pipe = require("@promistream/pipe");
const createMutationAPIWrapper = require("./mutation-api/wrapper"); const createMutationAPIWrapper = require("./mutation-api/wrapper");
// FIXME: Revert inlining of task_states once switched to PostgreSQL 12+, which can do this automatically using NOT MATERIALIZED
let query = ` let query = `
WITH WITH
dependency_tasks AS ( dependency_tasks AS (
SELECT * FROM SELECT * FROM
json_to_recordset(:dependencyTaskDefinitions) AS x(task text, task_version text) json_to_recordset(:dependencyTaskDefinitions) AS x(task text, task_version text)
), ),
task_states AS ( matching_items AS (
SELECT SELECT
*, DISTINCT ON (items.id)
items.*,
results.updated_at AS result_date,
results.task_version,
( (
is_successful = TRUE results.is_successful = TRUE
AND ( AND (
expires_at < NOW() results.expires_at < NOW()
OR is_invalidated = TRUE OR results.is_invalidated = TRUE
) )
) AS is_candidate ) AS is_candidate
FROM task_results
),
matching_items AS (
SELECT
DISTINCT ON (items.id)
items.*,
states.updated_at AS result_date,
states.is_candidate,
states.task_version
FROM items FROM items
INNER JOIN tags INNER JOIN tags
ON tags.item_id = items.id ON tags.item_id = items.id
AND tags.name = ANY(:tags) AND tags.name = ANY(:tags)
LEFT JOIN task_states AS states LEFT JOIN task_results AS results
ON states.item_id = items.id ON results.item_id = items.id
AND states.task = :task AND results.task = :task
WHERE WHERE
NOT EXISTS ( NOT EXISTS (
SELECT FROM tasks_in_progress AS pr WHERE pr.item_id = items.id SELECT FROM tasks_in_progress AS pr WHERE pr.item_id = items.id
@ -65,15 +60,21 @@ let query = `
WHERE WHERE
NOT EXISTS ( NOT EXISTS (
SELECT SELECT
task_states.* results.*
FROM dependency_tasks FROM dependency_tasks
LEFT JOIN task_states LEFT JOIN task_results AS results
ON dependency_tasks.task = task_states.task ON dependency_tasks.task = results.task
AND dependency_tasks.task_version = task_states.task_version AND dependency_tasks.task_version = results.task_version
AND task_states.item_id = candidates.id AND results.item_id = candidates.id
WHERE WHERE
task_states.is_candidate IS NULL results.is_successful IS NULL
OR task_states.is_candidate = TRUE OR (
results.is_successful = TRUE
AND (
results.expires_at < NOW()
OR results.is_invalidated = TRUE
)
)
) )
) LIMIT :resultLimit; ) LIMIT :resultLimit;
`; `;

Loading…
Cancel
Save