Skip to content
New issue

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

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

Already on GitHub? # to your account

pref: Optimize select query performance when there is no limit and offset #1035

Merged
merged 1 commit into from
Oct 26, 2024

Conversation

Tiscs
Copy link
Collaborator

@Tiscs Tiscs commented Oct 23, 2024

If there is no limit and offset, we can use a single query to get the count and scan, the additional count query is not necessary.

// If there is no limit and offset 
bundb.NewSelect().Model(users).Where("age > ?", 59).ScanAndCount(ctx)
// SELECT user.id, user.name, user.age FROM users WHERE user.age > 59;

// Otherwise
bundb.NewSelect().Model(users).Where("age > ?", 59).Offset(0).Limit(10).ScanAndCount(ctx)
// SELECT user.id, user.name, user.age FROM users WHERE user.age > 59;
// SELECT COUNT(*) FROM users WHERE user.age > 59;

In this way, we can use the more general repos layer method definition, like this:

type UsersRepo interface {
    // Implemention:
    //
    // var users []*models.User
    // query := r.bdb.NewSelect().Model(&users)
    // for _, mod := range mods {
    //     query = query.Apply(mod)
    // }
    // return query.ScanAndCount(ctx)
    //
    // Example 1:
    //
    //   users, _, err := usersRepo.FindUsersOlderThan(ctx, 59)
    //
    // Example 2:
    //
    //   users, total, err := usersRepo.FindUsersOlderThan(ctx, 59, func(sq *bun.SelectQuery) *bun.SelectQuery {
    //     return sq.Offset(0).Limit(10)
    //   })
    FindUsersOlderThan(ctx context.Context, age int, mods ...func(*bun.SelectQuery) *bun.SelectQuery) ([]*models.User, int, error)
}

@Tiscs Tiscs changed the title Optimize select query performance when there is no limit and offset pref: Optimize select query performance when there is no limit and offset Oct 23, 2024
@Tiscs Tiscs force-pushed the optimize_scan_and_count branch from 543ff64 to 4bb50ed Compare October 23, 2024 06:42
@Tiscs Tiscs force-pushed the optimize_scan_and_count branch from 4bb50ed to c774811 Compare October 23, 2024 06:44
@vmihailenco vmihailenco merged commit 8638613 into uptrace:master Oct 26, 2024
4 checks passed
@vmihailenco
Copy link
Member

Nice, thanks!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants