Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve get feed with pagination #31821

Merged
merged 18 commits into from
Sep 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions models/activities/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,17 +450,46 @@ func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, int64, err
return nil, 0, err
}

lunny marked this conversation as resolved.
Show resolved Hide resolved
sess := db.GetEngine(ctx).Where(cond).
Select("`action`.*"). // this line will avoid select other joined table's columns
Join("INNER", "repository", "`repository`.id = `action`.repo_id")
actions := make([]*Action, 0, opts.PageSize)
var count int64

opts.SetDefaultValues()
sess = db.SetSessionPagination(sess, &opts)
if opts.Page < 10 { // TODO: why it's 10 but other values? It's an experience value.
lunny marked this conversation as resolved.
Show resolved Hide resolved
sess := db.GetEngine(ctx).Where(cond).
Select("`action`.*"). // this line will avoid select other joined table's columns
Join("INNER", "repository", "`repository`.id = `action`.repo_id")

actions := make([]*Action, 0, opts.PageSize)
count, err := sess.Desc("`action`.created_unix").FindAndCount(&actions)
if err != nil {
return nil, 0, fmt.Errorf("FindAndCount: %w", err)
opts.SetDefaultValues()
sess = db.SetSessionPagination(sess, &opts)

count, err = sess.Desc("`action`.created_unix").FindAndCount(&actions)
if err != nil {
return nil, 0, fmt.Errorf("FindAndCount: %w", err)
}
} else {
// First, only query which IDs are necessary, and only then query all actions to speed up the overall query
sess := db.GetEngine(ctx).Where(cond).
Select("`action`.id").
Join("INNER", "repository", "`repository`.id = `action`.repo_id")

opts.SetDefaultValues()
sess = db.SetSessionPagination(sess, &opts)

actionIDs := make([]int64, 0, opts.PageSize)
if err := sess.Table("action").Desc("`action`.created_unix").Find(&actionIDs); err != nil {
return nil, 0, fmt.Errorf("Find(actionsIDs): %w", err)
}

count, err = db.GetEngine(ctx).Where(cond).
Table("action").
Cols("`action`.id").
Join("INNER", "repository", "`repository`.id = `action`.repo_id").Count()
if err != nil {
return nil, 0, fmt.Errorf("Count: %w", err)
}

if err := db.GetEngine(ctx).In("`action`.id", actionIDs).Desc("`action`.created_unix").Find(&actions); err != nil {
return nil, 0, fmt.Errorf("Find: %w", err)
}
}

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