Skip to content

Commit c4b30a6

Browse files
authored
Fix: improve performance of cron schedule polling query (#3754)
* fix: update query to try to avoid expensive hash + nested loop * feat: add index, gen * fix: small bug * chore: gen
1 parent 5fbdc15 commit c4b30a6

3 files changed

Lines changed: 37 additions & 24 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- +goose NO TRANSACTION
2+
3+
-- +goose Up
4+
-- +goose StatementBegin
5+
CREATE INDEX CONCURRENTLY idx_workflow_triggers_id_version_id_tenant_id ON "WorkflowTriggers" ("id", "workflowVersionId", "tenantId");
6+
-- +goose StatementEnd
7+
8+
-- +goose Down
9+
-- +goose StatementBegin
10+
DROP INDEX CONCURRENTLY idx_workflow_triggers_id_version_id_tenant_id;
11+
-- +goose StatementEnd

pkg/repository/sqlcv1/ticker.sql

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,7 @@ WHERE
9494
RETURNING *;
9595

9696
-- name: PollCronSchedules :many
97-
WITH latest_workflow_versions AS (
98-
SELECT
99-
"workflowId",
100-
MAX("order") as max_order
101-
FROM
102-
"WorkflowVersion"
103-
WHERE
104-
"deletedAt" IS NULL
105-
GROUP BY "workflowId"
106-
),
107-
eligible_cron_with_versions AS (
97+
WITH eligible_cron_with_versions AS MATERIALIZED (
10898
SELECT
10999
cronSchedule."parentId",
110100
cronSchedule."cron",
@@ -133,6 +123,17 @@ eligible_cron_with_versions AS (
133123
)
134124
FOR UPDATE OF cronSchedule SKIP LOCKED
135125
),
126+
latest_workflow_versions AS (
127+
SELECT
128+
DISTINCT ON ("workflowId")
129+
"workflowId",
130+
"id"
131+
FROM
132+
"WorkflowVersion"
133+
WHERE
134+
"deletedAt" IS NULL
135+
ORDER BY "workflowId", "order" DESC
136+
),
136137
eligible_cron_schedules AS (
137138
SELECT
138139
ecv."parentId",
@@ -143,7 +144,7 @@ eligible_cron_schedules AS (
143144
FROM
144145
eligible_cron_with_versions as ecv
145146
JOIN
146-
latest_workflow_versions as l ON ecv."workflowId" = l."workflowId" AND ecv."order" = l.max_order
147+
latest_workflow_versions as l ON ecv."workflowId" = l."workflowId" AND ecv."workflowVersionId" = l."id"
147148
)
148149
UPDATE
149150
"WorkflowTriggerCronRef" as cronSchedules

pkg/repository/sqlcv1/ticker.sql.go

Lines changed: 13 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)