Skip to content

Commit ea2ea8e

Browse files
GiteaBotKN4CK3R
andauthored
Fix package list performance (#30520) (#30616)
Backport #30520 by @KN4CK3R Fixes #28255 The new query uses the id field to sort by "newer". This most not be correct (usually it is) but it's faster (see #28255). If someone has a better idea, please propose changes. Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
1 parent b4e1874 commit ea2ea8e

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

models/packages/package_version.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,10 @@ func (opts *PackageSearchOptions) configureOrderBy(e db.Engine) {
287287
// SearchVersions gets all versions of packages matching the search options
288288
func SearchVersions(ctx context.Context, opts *PackageSearchOptions) ([]*PackageVersion, int64, error) {
289289
sess := db.GetEngine(ctx).
290-
Where(opts.ToConds()).
290+
Select("package_version.*").
291291
Table("package_version").
292-
Join("INNER", "package", "package.id = package_version.package_id")
292+
Join("INNER", "package", "package.id = package_version.package_id").
293+
Where(opts.ToConds())
293294

294295
opts.configureOrderBy(sess)
295296

@@ -304,19 +305,18 @@ func SearchVersions(ctx context.Context, opts *PackageSearchOptions) ([]*Package
304305

305306
// SearchLatestVersions gets the latest version of every package matching the search options
306307
func SearchLatestVersions(ctx context.Context, opts *PackageSearchOptions) ([]*PackageVersion, int64, error) {
307-
cond := opts.ToConds().
308-
And(builder.Expr("pv2.id IS NULL"))
309-
310-
joinCond := builder.Expr("package_version.package_id = pv2.package_id AND (package_version.created_unix < pv2.created_unix OR (package_version.created_unix = pv2.created_unix AND package_version.id < pv2.id))")
311-
if opts.IsInternal.Has() {
312-
joinCond = joinCond.And(builder.Eq{"pv2.is_internal": opts.IsInternal.Value()})
313-
}
308+
in := builder.
309+
Select("MAX(package_version.id)").
310+
From("package_version").
311+
InnerJoin("package", "package.id = package_version.package_id").
312+
Where(opts.ToConds()).
313+
GroupBy("package_version.package_id")
314314

315315
sess := db.GetEngine(ctx).
316+
Select("package_version.*").
316317
Table("package_version").
317-
Join("LEFT", "package_version pv2", joinCond).
318318
Join("INNER", "package", "package.id = package_version.package_id").
319-
Where(cond)
319+
Where(builder.In("package_version.id", in))
320320

321321
opts.configureOrderBy(sess)
322322

0 commit comments

Comments
 (0)