Skip to content

Commit 4e1b203

Browse files
authored
PLU-467 [HOTFIX]: slow GetFlows query when query planner does a poor job (#958)
## Problem GetFlows endpoint and db query is slow for some users (e.g. myself) ## Solution Fetch the filtered slows and steps first to make it more deliberate. See notion: https://www.notion.so/opengov/GetFlows-query-slow-1df77dbba788808d9c9fe1f1f9be5bc7?pvs=4 for more details. ## What to test - [ ] Retrieving all pipes works - [ ] Filtering and paginate pipes work - [ ] Able to view all pipes for a given app - [ ] Able to view all pipes for a given connection
1 parent b1aeea9 commit 4e1b203

File tree

1 file changed

+43
-18
lines changed

1 file changed

+43
-18
lines changed

packages/backend/src/graphql/queries/get-flows.ts

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import paginate from '@/helpers/pagination'
2+
import Flow from '@/models/flow'
23

34
import type { QueryResolvers } from '../__generated__/types.generated'
45

@@ -7,31 +8,55 @@ const getFlows: QueryResolvers['getFlows'] = async (
78
params,
89
context,
910
) => {
10-
const flowsQuery = context.currentUser
11-
.$relatedQuery('flows')
12-
.joinRelated({
13-
steps: true,
11+
const filteredFlowIds = (
12+
await context.currentUser
13+
.$relatedQuery('flows')
14+
.distinct('id')
15+
.where((builder) => {
16+
if (params.name) {
17+
builder.where('name', 'ilike', `%${params.name}%`)
18+
}
19+
})
20+
).map((f) => f.id)
21+
22+
if (!filteredFlowIds.length) {
23+
return {
24+
pageInfo: {
25+
currentPage: 1,
26+
totalCount: 0,
27+
},
28+
edges: [],
29+
}
30+
}
31+
32+
const flowsQuery = Flow.query()
33+
.with('filtered_steps', (builder) => {
34+
builder
35+
.distinct('flow_id')
36+
.from('steps')
37+
.where((stepBuilder) => {
38+
if (params.connectionId) {
39+
stepBuilder.where('connection_id', params.connectionId)
40+
}
41+
42+
if (params.appKey) {
43+
stepBuilder.where('app_key', params.appKey)
44+
}
45+
46+
stepBuilder.withSoftDeleted()
47+
})
48+
.whereNull('deleted_at')
49+
.whereIn('flow_id', filteredFlowIds)
50+
.withSoftDeleted()
1451
})
52+
.innerJoin('filtered_steps', 'id', 'filtered_steps.flow_id')
1553
.withGraphFetched({
1654
steps: {
1755
connection: true,
1856
},
1957
pendingTransfer: true,
2058
})
21-
.where((builder) => {
22-
if (params.connectionId) {
23-
builder.where('steps.connection_id', params.connectionId)
24-
}
25-
26-
if (params.name) {
27-
builder.where('flows.name', 'ilike', `%${params.name}%`)
28-
}
29-
30-
if (params.appKey) {
31-
builder.where('steps.app_key', params.appKey)
32-
}
33-
})
34-
.groupBy('flows.id')
59+
.groupBy('id')
3560
.orderBy('active', 'desc')
3661
.orderBy('updated_at', 'desc')
3762

0 commit comments

Comments
 (0)