MM-55295: Improve DB performance of some (Take 2) (#25865)

* Revert "Revert "MM-55295: Improve DB performance of some APIs (#25318)" (#25852)"

This reverts commit 077221a940.

* Fix the issue

```release-note
NONE
```

* lint fix

```release-note
NONE
```
Этот коммит содержится в:
Agniva De Sarker
2024-01-11 10:18:36 +05:30
коммит произвёл GitHub
родитель 43cca04f04
Коммит 1d879ed0f4
12 изменённых файлов: 232 добавлений и 56 удалений

Просмотреть файл

@@ -1126,26 +1126,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) {
@@ -2070,6 +2060,20 @@ func (s SqlChannelStore) GetMember(ctx context.Context, channelID string, userID
return dbMember.ToModel(), nil
}
func (s SqlChannelStore) GetMemberLastViewedAt(ctx context.Context, channelID string, userID string) (int64, error) {
var lastViewedAt int64
if err := s.DBXFromContext(ctx).Get(&lastViewedAt, `SELECT COALESCE(LastViewedAt, 0) AS LastViewedAt
FROM ChannelMembers
WHERE ChannelId=? AND UserId=?`, channelID, userID); err != nil {
if err == sql.ErrNoRows {
return 0, store.NewErrNotFound("LastViewedAt", fmt.Sprintf("channelId=%s, userId=%s", channelID, userID))
}
return 0, errors.Wrapf(err, "failed to get lastViewedAt with channelId=%s and userId=%s", channelID, userID)
}
return lastViewedAt, nil
}
func (s SqlChannelStore) InvalidateAllChannelMembersForUser(userId string) {
allChannelMembersForUserCache.Remove(userId)
allChannelMembersForUserCache.Remove(userId + "_deleted")