Skip to content

Commit

Permalink
perf: improve performance of users-with-channel count query
Browse files Browse the repository at this point in the history
  • Loading branch information
Satont committed Dec 16, 2024
1 parent 6697cda commit 8a43de6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions apps/api-gql/internal/services/twir-users/twir-users.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type GetManyInput struct {
PerPage int
ChannelIsEnabled *bool
ChannelIsBotAdmin *bool
ChannelIsBanned *bool
UserIsBanned *bool
HasBadges []string
}

Expand Down Expand Up @@ -96,7 +96,7 @@ func (c *Service) GetMany(ctx context.Context, input GetManyInput) (
IDs: nil,
ChannelEnabled: input.ChannelIsEnabled,
ChannelIsBotAdmin: input.ChannelIsBotAdmin,
ChannelIsBanned: input.ChannelIsBanned,
IsBanned: input.UserIsBanned,
HasBadgesIDS: input.HasBadges,
}

Expand Down
22 changes: 13 additions & 9 deletions libs/repositories/users-with-channel/pgx/pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ func (c *Pgx) GetManyByIDS(
selectQuery = selectQuery.Where(squirrel.Eq{`u."isBotAdmin"`: input.ChannelIsBotAdmin})
}

if input.ChannelIsBanned != nil {
selectQuery = selectQuery.Where(squirrel.Eq{`u.is_banned`: input.ChannelIsBanned})
if input.IsBanned != nil {
selectQuery = selectQuery.Where(squirrel.Eq{`u.is_banned`: input.IsBanned})
}

query, args, err := selectQuery.ToSql()
Expand Down Expand Up @@ -184,15 +184,17 @@ func (c *Pgx) GetManyCount(ctx context.Context, input users_with_channel.GetMany
error,
) {
selectQuery := sq.
Select("COUNT(DISTINCT u.id)").
From("users u").
LeftJoin("channels uc ON u.id = uc.id").
LeftJoin("badges_users bu ON u.id = bu.user_id")
Select("COUNT(*)").
From("users u")

if len(input.IDs) > 0 {
selectQuery = selectQuery.Where(squirrel.Eq{"u.id": input.IDs})
}

if input.ChannelIsBotAdmin != nil || input.ChannelEnabled != nil {
selectQuery = selectQuery.LeftJoin("channels uc ON u.id = uc.id")
}

if input.ChannelEnabled != nil {
selectQuery = selectQuery.Where(squirrel.Eq{`uc."isEnabled"`: input.ChannelEnabled})
}
Expand All @@ -201,12 +203,14 @@ func (c *Pgx) GetManyCount(ctx context.Context, input users_with_channel.GetMany
selectQuery = selectQuery.Where(squirrel.Eq{`u."isBotAdmin"`: input.ChannelIsBotAdmin})
}

if input.ChannelIsBanned != nil {
selectQuery = selectQuery.Where(squirrel.Eq{`u.is_banned`: input.ChannelIsBanned})
if input.IsBanned != nil {
selectQuery = selectQuery.Where(squirrel.Eq{`u.is_banned`: input.IsBanned})
}

if len(input.HasBadgesIDS) > 0 {
selectQuery = selectQuery.Where(squirrel.Eq{"bu.badge_id": input.HasBadgesIDS})
selectQuery = selectQuery.
LeftJoin("channels uc ON u.id = uc.id").
Where(squirrel.Eq{"bu.badge_id": input.HasBadgesIDS})
}

query, args, err := selectQuery.ToSql()
Expand Down
2 changes: 1 addition & 1 deletion libs/repositories/users-with-channel/users-with-channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ type GetManyInput struct {

ChannelEnabled *bool
ChannelIsBotAdmin *bool
ChannelIsBanned *bool
IsBanned *bool
}

0 comments on commit 8a43de6

Please sign in to comment.