Skip to content

Commit

Permalink
perf(query): speed up parsing of a huge query (#8942)
Browse files Browse the repository at this point in the history
If a mutation has a lot of conditional mutations, this PR will speed up
the parsing of the query. cherry-pick of PR (#7871)
  • Loading branch information
mangalaman93 authored and jbhamra1 committed Aug 17, 2023
1 parent 429ed44 commit 8adccdc
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions edgraph/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,10 @@ func buildUpsertQuery(qc *queryContext) string {
}

qc.condVars = make([]string, len(qc.req.Mutations))
upsertQuery := strings.TrimSuffix(qc.req.Query, "}")

var upsertQB strings.Builder
x.Check2(upsertQB.WriteString(strings.TrimSuffix(qc.req.Query, "}")))

for i, gmu := range qc.gmuList {
isCondUpsert := strings.TrimSpace(gmu.Cond) != ""
if isCondUpsert {
Expand All @@ -756,13 +759,15 @@ func buildUpsertQuery(qc *queryContext) string {
// The variable __dgraph_0__ will -
// * be empty if the condition is true
// * have 1 UID (the 0 UID) if the condition is false
upsertQuery += qc.condVars[i] + ` as var(func: uid(0)) ` + cond + `
`
x.Check2(upsertQB.WriteString(qc.condVars[i]))
x.Check2(upsertQB.WriteString(` as var(func: uid(0)) `))
x.Check2(upsertQB.WriteString(cond))
x.Check2(upsertQB.WriteString("\n"))
}
}
upsertQuery += `}`

return upsertQuery
x.Check2(upsertQB.WriteString(`}`))
return upsertQB.String()
}

// updateMutations updates the mutation and replaces uid(var) and val(var) with
Expand Down

0 comments on commit 8adccdc

Please sign in to comment.