SQL performance improvements (#6378)

* Change queryStruct to use tx.Get instead of queryFunc

Using queryFunc meant that the performance logging was inaccurate due to the query actually being executed during the call to Scan.

* Only add join args if join was added

* Omit joins that are only used for sorting when skipping sorting

Should provide some marginal improvement on systems with a lot of items.

* Make all calls to the database pass context.

This means that long queries can be cancelled by navigating to another page. Previously the query would continue to run, impacting on future queries.
This commit is contained in:
WithoutPants
2025-12-08 08:08:31 +11:00
committed by GitHub
parent e2dff05081
commit 0fd7a2ac20
9 changed files with 84 additions and 49 deletions

View File

@@ -24,8 +24,8 @@ type queryBuilder struct {
sortAndPagination string
}
func (qb queryBuilder) body() string {
return fmt.Sprintf("SELECT %s FROM %s%s", strings.Join(qb.columns, ", "), qb.from, qb.joins.toSQL())
func (qb queryBuilder) body(includeSortPagination bool) string {
return fmt.Sprintf("SELECT %s FROM %s%s", strings.Join(qb.columns, ", "), qb.from, qb.joins.toSQL(includeSortPagination))
}
func (qb *queryBuilder) addColumn(column string) {
@@ -33,7 +33,7 @@ func (qb *queryBuilder) addColumn(column string) {
}
func (qb queryBuilder) toSQL(includeSortPagination bool) string {
body := qb.body()
body := qb.body(includeSortPagination)
withClause := ""
if len(qb.withClauses) > 0 {
@@ -59,12 +59,14 @@ func (qb queryBuilder) findIDs(ctx context.Context) ([]int, error) {
}
func (qb queryBuilder) executeFind(ctx context.Context) ([]int, int, error) {
body := qb.body()
const includeSortPagination = true
body := qb.body(includeSortPagination)
return qb.repository.executeFindQuery(ctx, body, qb.args, qb.sortAndPagination, qb.whereClauses, qb.havingClauses, qb.withClauses, qb.recursiveWith)
}
func (qb queryBuilder) executeCount(ctx context.Context) (int, error) {
body := qb.body()
const includeSortPagination = false
body := qb.body(includeSortPagination)
withClause := ""
if len(qb.withClauses) > 0 {
@@ -131,10 +133,23 @@ func (qb *queryBuilder) join(table, as, onClause string) {
qb.joins.add(newJoin)
}
func (qb *queryBuilder) joinSort(table, as, onClause string) {
newJoin := join{
sort: true,
table: table,
as: as,
onClause: onClause,
joinType: "LEFT",
}
qb.joins.add(newJoin)
}
func (qb *queryBuilder) addJoins(joins ...join) {
qb.joins.add(joins...)
for _, j := range joins {
qb.args = append(qb.args, j.args...)
if qb.joins.addUnique(j) {
qb.args = append(qb.args, j.args...)
}
}
}