MM-55295: Improve DB performance of some APIs (#25318)
Load tests show that channelstore.GetMember and channelstore.GetMembersForUser are among the chief queries that take up CPU in the DB. In this PR, we attempt some strategic optimizations to reduce/optimize calls to channelstore.GetMember 1. Optimize `(a *App) HasPermissionToChannel` We replace GetChannelMember with GetAllChannelMembersForUser because it's cache backed. So although it gets more data, it does not hit the DB and saves some latency. 2. Optimize getPostsForChannelAroundLastUnread We repace getChannelMember with getChannelMemberOnly which is a lite version of the store call which queries just the ChannelMembers table. This is because in the app layer, we just use the LastViewedAt attribute. Therefore, there is no reason to join with 5 tables when a single table can do the job. 3. Optimize publishWebsocketEventForPermalinkPost We use GetAllChannelMembersById instead of GetChannelMembersPage which again joins with a lot of other tables. 4. Optimize countMentionsFromPost Again, we replace GetChannelMember which is a costly call joining multiple tables, with GetAllChannelMembersNotifyPropsForChannel which is cache-backed and gives us just what we need in the app layer - notify props. ```release-note Make small optimizations in several DB calls: - App.HasPermissionToChannel - getPostsForChannelAroundLastUnread - publishWebsocketEventForPermalinkPost - countMentionsFromPost ``` https://mattermost.atlassian.net/browse/MM-55295 --------- Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
1ed5d87342
Коммит
ec88ab4ee9
@@ -1125,26 +1125,16 @@ func (s SqlChannelStore) GetChannelsByUser(userId string, includeDeleted bool, l
|
||||
return channels, nil
|
||||
}
|
||||
|
||||
func (s SqlChannelStore) GetAllChannelMembersById(channelID string) ([]string, error) {
|
||||
sql, args, err := s.channelMembersForTeamWithSchemeSelectQuery.Where(sq.Eq{
|
||||
"ChannelId": channelID,
|
||||
}).ToSql()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetAllChannelMembersById_ToSql")
|
||||
}
|
||||
|
||||
dbMembers := channelMemberWithSchemeRolesList{}
|
||||
err = s.GetReplicaX().Select(&dbMembers, sql, args...)
|
||||
func (s SqlChannelStore) GetAllChannelMemberIdsByChannelId(channelID string) ([]string, error) {
|
||||
userIDs := []string{}
|
||||
err := s.GetReplicaX().Select(&userIDs, `SELECT UserId
|
||||
FROM ChannelMembers
|
||||
WHERE ChannelId=?`, channelID)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to get ChannelMembers with channelID=%s", channelID)
|
||||
}
|
||||
|
||||
res := make([]string, len(dbMembers))
|
||||
for i, member := range dbMembers.ToModel() {
|
||||
res[i] = member.UserId
|
||||
}
|
||||
|
||||
return res, nil
|
||||
return userIDs, nil
|
||||
}
|
||||
|
||||
func (s SqlChannelStore) GetAllChannels(offset, limit int, opts store.ChannelSearchOpts) (model.ChannelListWithTeamData, error) {
|
||||
@@ -2069,6 +2059,33 @@ func (s SqlChannelStore) GetMember(ctx context.Context, channelID string, userID
|
||||
return dbMember.ToModel(), nil
|
||||
}
|
||||
|
||||
func (s SqlChannelStore) GetMemberOnly(ctx context.Context, channelID string, userID string) (*model.ChannelMember, error) {
|
||||
var dbMember model.ChannelMember
|
||||
if err := s.DBXFromContext(ctx).Get(&dbMember, `SELECT ChannelId,
|
||||
UserId,
|
||||
Roles,
|
||||
LastViewedAt,
|
||||
MsgCount,
|
||||
MentionCount,
|
||||
MentionCountRoot,
|
||||
COALESCE(UrgentMentionCount, 0) AS UrgentMentionCount,
|
||||
MsgCountRoot,
|
||||
NotifyProps,
|
||||
LastUpdateAt,
|
||||
SchemeUser,
|
||||
SchemeAdmin,
|
||||
SchemeGuest
|
||||
FROM ChannelMembers
|
||||
WHERE ChannelId=? AND UserId=?`, channelID, userID); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, store.NewErrNotFound("ChannelMember", fmt.Sprintf("channelId=%s, userId=%s", channelID, userID))
|
||||
}
|
||||
return nil, errors.Wrapf(err, "failed to get ChannelMember with channelId=%s and userId=%s", channelID, userID)
|
||||
}
|
||||
|
||||
return &dbMember, nil
|
||||
}
|
||||
|
||||
func (s SqlChannelStore) InvalidateAllChannelMembersForUser(userId string) {
|
||||
allChannelMembersForUserCache.Remove(userId)
|
||||
allChannelMembersForUserCache.Remove(userId + "_deleted")
|
||||
|
||||
Ссылка в новой задаче
Block a user