Skip to content

Commit 85b1f30

Browse files
lunnydelvh
andauthored
Improve get feed with pagination (#31821)
Fix #31752 @somera --------- Co-authored-by: delvh <[email protected]>
1 parent 83f37f6 commit 85b1f30

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

models/activities/action.go

+38-9
Original file line numberDiff line numberDiff line change
@@ -450,17 +450,46 @@ func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, int64, err
450450
return nil, 0, err
451451
}
452452

453-
sess := db.GetEngine(ctx).Where(cond).
454-
Select("`action`.*"). // this line will avoid select other joined table's columns
455-
Join("INNER", "repository", "`repository`.id = `action`.repo_id")
453+
actions := make([]*Action, 0, opts.PageSize)
454+
var count int64
456455

457-
opts.SetDefaultValues()
458-
sess = db.SetSessionPagination(sess, &opts)
456+
if opts.Page < 10 { // TODO: why it's 10 but other values? It's an experience value.
457+
sess := db.GetEngine(ctx).Where(cond).
458+
Select("`action`.*"). // this line will avoid select other joined table's columns
459+
Join("INNER", "repository", "`repository`.id = `action`.repo_id")
459460

460-
actions := make([]*Action, 0, opts.PageSize)
461-
count, err := sess.Desc("`action`.created_unix").FindAndCount(&actions)
462-
if err != nil {
463-
return nil, 0, fmt.Errorf("FindAndCount: %w", err)
461+
opts.SetDefaultValues()
462+
sess = db.SetSessionPagination(sess, &opts)
463+
464+
count, err = sess.Desc("`action`.created_unix").FindAndCount(&actions)
465+
if err != nil {
466+
return nil, 0, fmt.Errorf("FindAndCount: %w", err)
467+
}
468+
} else {
469+
// First, only query which IDs are necessary, and only then query all actions to speed up the overall query
470+
sess := db.GetEngine(ctx).Where(cond).
471+
Select("`action`.id").
472+
Join("INNER", "repository", "`repository`.id = `action`.repo_id")
473+
474+
opts.SetDefaultValues()
475+
sess = db.SetSessionPagination(sess, &opts)
476+
477+
actionIDs := make([]int64, 0, opts.PageSize)
478+
if err := sess.Table("action").Desc("`action`.created_unix").Find(&actionIDs); err != nil {
479+
return nil, 0, fmt.Errorf("Find(actionsIDs): %w", err)
480+
}
481+
482+
count, err = db.GetEngine(ctx).Where(cond).
483+
Table("action").
484+
Cols("`action`.id").
485+
Join("INNER", "repository", "`repository`.id = `action`.repo_id").Count()
486+
if err != nil {
487+
return nil, 0, fmt.Errorf("Count: %w", err)
488+
}
489+
490+
if err := db.GetEngine(ctx).In("`action`.id", actionIDs).Desc("`action`.created_unix").Find(&actions); err != nil {
491+
return nil, 0, fmt.Errorf("Find: %w", err)
492+
}
464493
}
465494

466495
if err := ActionList(actions).LoadAttributes(ctx); err != nil {

0 commit comments

Comments
 (0)