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(engine): add missing index + improve query #5876

Merged
merged 1 commit into from
Jul 7, 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
47 changes: 27 additions & 20 deletions engine/api/workflow/dao_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,26 +334,33 @@ func LoadRunsSummaries(db gorp.SqlExecutor, projectkey, workflowname string, off

if len(tagFilter) > 0 {
// Posgres operator: '<@' means 'is contained by' eg. 'ARRAY[2,7] <@ ARRAY[1,7,4,2,6]' ==> returns true
query = fmt.Sprintf(`select %s
from workflow_run wr
join project on wr.project_id = project.id
join workflow on wr.workflow_id = workflow.id
join (
select workflow_run_id, string_agg(all_tags, ',') as tags
from (
select workflow_run_id, tag || '=' || value "all_tags"
from workflow_run_tag
order by tag
) as all_wr_tags
group by workflow_run_id
) as tags on wr.id = tags.workflow_run_id
where project.projectkey = $1
and workflow.name = $2
AND wr.to_delete = false
and string_to_array($5, ',') <@ string_to_array(tags.tags, ',')
order by wr.start desc
limit $3 offset $4`, selectedColumn)

query = fmt.Sprintf(`
WITH workflowID as (
SELECT workflow.id FROM workflow
JOIN project ON project.id = workflow.project_id
WHERE workflow.name = $2 AND project.projectkey = $1
),
runs as (
SELECT %s
FROM workflow_run wr
JOIN workflowID ON workflowID.id = wr.workflow_id
WHERE wr.to_delete = false
),
tags as (
SELECT workflow_run_id, tag || '=' || value "all_tags"
FROM workflow_run_tag
JOIN runs ON runs.id = workflow_run_id
),
aggTags as (
SELECT workflow_run_id, string_agg(all_tags, ',') as tags
FROM tags
GROUP BY workflow_run_id
)
SELECT runs.*
FROM runs
JOIN aggTags ON aggTags.workflow_run_id = runs.id
WHERE string_to_array($5, ',') <@ string_to_array(aggTags.tags, ',')
ORDER BY runs.start DESC OFFSET $4 LIMIT $3`, selectedColumn)
var tags []string
for k, v := range tagFilter {
tags = append(tags, k+"="+v)
Expand Down
5 changes: 5 additions & 0 deletions engine/sql/api/226_indexes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- +migrate Up
CREATE INDEX idx_workflow_node_run_workflow_id ON workflow_node_run (workflow_id, vcs_hash, workflow_node_name, num);

-- +migrate Down
DROP INDEX idx_workflow_node_run_workflow_id;