Skip to content

Commit

Permalink
fix(engine): add missing index + improve query (#5876)
Browse files Browse the repository at this point in the history
  • Loading branch information
sguiheux authored Jul 7, 2021
1 parent cf7caf9 commit 8e4c7de
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
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;

0 comments on commit 8e4c7de

Please sign in to comment.