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

@@ -96,7 +96,7 @@ func (r *repository) runIdsQuery(ctx context.Context, query string, args []inter
}
func (r *repository) queryFunc(ctx context.Context, query string, args []interface{}, single bool, f func(rows *sqlx.Rows) error) error {
rows, err := dbWrapper.Queryx(ctx, query, args...)
rows, err := dbWrapper.QueryxContext(ctx, query, args...)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return err
@@ -119,13 +119,12 @@ func (r *repository) queryFunc(ctx context.Context, query string, args []interfa
return nil
}
// queryStruct executes a query and scans the result into the provided struct.
// Unlike the other query methods, this will return an error if no rows are found.
func (r *repository) queryStruct(ctx context.Context, query string, args []interface{}, out interface{}) error {
if err := r.queryFunc(ctx, query, args, true, func(rows *sqlx.Rows) error {
if err := rows.StructScan(out); err != nil {
return err
}
return nil
}); err != nil {
// changed from queryFunc, since it was not logging the performance correctly,
// since the query doesn't actually execute until Scan is called
if err := dbWrapper.Get(ctx, out, query, args...); err != nil {
return fmt.Errorf("executing query: %s [%v]: %w", query, args, err)
}