diff --git a/models/actions/run_list.go b/models/actions/run_list.go index 88f3d3dd822..e07b30c265f 100644 --- a/models/actions/run_list.go +++ b/models/actions/run_list.go @@ -106,6 +106,15 @@ func (opts FindRunOptions) ToJoins() []db.JoinFunc { } func (opts FindRunOptions) ToOrders() string { + // When scoped to a repo, sort by `index`: it reuses the unique + // `repo_index` (repo_id, index) index, so the query seeks repo_id and + // walks index descending instead of filesorting all matching rows. + // Within a repo `index` is co-monotonic with `id`, so the order is the same. + if opts.RepoID > 0 { + return "`action_run`.`index` DESC" + } + // `index` is scoped per repo, so it is meaningless across repos. With no + // RepoID, sort by the global, PK-indexed `id` for a deterministic order. return "`action_run`.`id` DESC" }