Inline task_states for better performance
This commit is contained in:
parent
731e41996b
commit
ab227537dd
|
@ -12,38 +12,33 @@ const pipe = require("@promistream/pipe");
|
|||
|
||||
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 = `
|
||||
WITH
|
||||
dependency_tasks AS (
|
||||
SELECT * FROM
|
||||
json_to_recordset(:dependencyTaskDefinitions) AS x(task text, task_version text)
|
||||
),
|
||||
task_states AS (
|
||||
SELECT
|
||||
*,
|
||||
(
|
||||
is_successful = TRUE
|
||||
AND (
|
||||
expires_at < NOW()
|
||||
OR is_invalidated = TRUE
|
||||
)
|
||||
) 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
|
||||
results.updated_at AS result_date,
|
||||
results.task_version,
|
||||
(
|
||||
results.is_successful = TRUE
|
||||
AND (
|
||||
results.expires_at < NOW()
|
||||
OR results.is_invalidated = TRUE
|
||||
)
|
||||
) AS is_candidate
|
||||
FROM items
|
||||
INNER JOIN tags
|
||||
ON tags.item_id = items.id
|
||||
AND tags.name = ANY(:tags)
|
||||
LEFT JOIN task_states AS states
|
||||
ON states.item_id = items.id
|
||||
AND states.task = :task
|
||||
LEFT JOIN task_results AS results
|
||||
ON results.item_id = items.id
|
||||
AND results.task = :task
|
||||
WHERE
|
||||
NOT EXISTS (
|
||||
SELECT FROM tasks_in_progress AS pr WHERE pr.item_id = items.id
|
||||
|
@ -65,15 +60,21 @@ let query = `
|
|||
WHERE
|
||||
NOT EXISTS (
|
||||
SELECT
|
||||
task_states.*
|
||||
results.*
|
||||
FROM dependency_tasks
|
||||
LEFT JOIN task_states
|
||||
ON dependency_tasks.task = task_states.task
|
||||
AND dependency_tasks.task_version = task_states.task_version
|
||||
AND task_states.item_id = candidates.id
|
||||
LEFT JOIN task_results AS results
|
||||
ON dependency_tasks.task = results.task
|
||||
AND dependency_tasks.task_version = results.task_version
|
||||
AND results.item_id = candidates.id
|
||||
WHERE
|
||||
task_states.is_candidate IS NULL
|
||||
OR task_states.is_candidate = TRUE
|
||||
results.is_successful IS NULL
|
||||
OR (
|
||||
results.is_successful = TRUE
|
||||
AND (
|
||||
results.expires_at < NOW()
|
||||
OR results.is_invalidated = TRUE
|
||||
)
|
||||
)
|
||||
)
|
||||
) LIMIT :resultLimit;
|
||||
`;
|
||||
|
|
Loading…
Reference in a new issue