perf(tasks): only use DISTINCT when a join can multiply rows

The task search always emitted SELECT DISTINCT tasks.*, but the only join
that can produce duplicate rows is task_buckets without a project view to
pin it — task_positions and task_buckets are both unique on
(task_id, project_view_id), and every other condition filters through a
subquery.

DISTINCT forced a sort+unique over every matching row before the LIMIT
could apply, so `ORDER BY due_date ... LIMIT n` on the home overview could
never walk an index. On MySQL the longtext description also pushed it into
an on-disk temp table.

Gate it, and the count query along with it. Selecting task_positions.position
was only needed to satisfy Postgres' rule that ORDER BY expressions appear in
a DISTINCT select list, so that goes too.
This commit is contained in:
kolaente
2026-08-29 17:50:20 +02:00
parent b721eacf7a
commit cccb76e80e
+26 -8
View File
@@ -583,9 +583,17 @@ func (d *dbTaskSearcher) Search(opts *taskSearchOptions) (tasks []*Task, totalCo
return nil, 0, err
}
var distinct = "tasks.*"
if strings.Contains(orderby, "task_positions.") {
distinct += ", task_positions.position"
// The only join that can multiply rows is task_buckets without a view to pin
// it — task_positions and task_buckets are both unique on (task_id,
// project_view_id), everything else filters through subqueries. DISTINCT
// otherwise costs a full sort+unique of every matching row before the LIMIT,
// which is what stops `ORDER BY due_date … LIMIT n` from walking an index.
needsDistinct := joinTaskBuckets && opts.projectViewID == 0
var selectCols = "tasks.*"
if needsDistinct && strings.Contains(orderby, "task_positions.") {
// Postgres requires ORDER BY expressions to be in the DISTINCT select list.
selectCols += ", task_positions.position"
}
if expandSubtasks {
@@ -593,13 +601,19 @@ func (d *dbTaskSearcher) Search(opts *taskSearchOptions) (tasks []*Task, totalCo
}
query := d.s.Where(cond)
if rankByRelevance {
switch {
case rankByRelevance:
// Select() passes the raw column list through untouched while Distinct()
// (no args) still emits DISTINCT. Distinct("tasks.*, pdb.score(tasks.id)")
// would quote-corrupt the function call into "pdb"."score(tasks"."id)".
query = query.Select(distinct + ", pdb.score(tasks.id)").Distinct()
} else {
query = query.Distinct(distinct)
query = query.Select(selectCols + ", pdb.score(tasks.id)")
if needsDistinct {
query = query.Distinct()
}
case needsDistinct:
query = query.Distinct(selectCols)
default:
query = query.Select(selectCols)
}
if limit > 0 {
query = query.Limit(limit, start)
@@ -690,8 +704,12 @@ func (d *dbTaskSearcher) Search(opts *taskSearchOptions) (tasks []*Task, totalCo
queryCount = queryCount.Join("LEFT", "task_buckets", joinCond)
}
}
countCol := "count(tasks.id)"
if needsDistinct {
countCol = "count(DISTINCT tasks.id)"
}
totalCount, err = queryCount.
Select("count(DISTINCT tasks.id)").
Select(countCol).
Count(&Task{})
if err != nil {
sql, vals := queryCount.LastSQL()