From 51c6e779729d424108c8010642bfc5e51241f0fe Mon Sep 17 00:00:00 2001 From: Ibrahim Serdar Acikgoz Date: Wed, 12 Jul 2023 17:40:34 +0300 Subject: [PATCH] [MM-53407] server/user_store: avoid antijoin for AnalyticsActiveCount query (#23993) --- server/channels/store/sqlstore/user_store.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/server/channels/store/sqlstore/user_store.go b/server/channels/store/sqlstore/user_store.go index b709fdd9d5..cc63bc038a 100644 --- a/server/channels/store/sqlstore/user_store.go +++ b/server/channels/store/sqlstore/user_store.go @@ -1359,15 +1359,17 @@ func (us SqlUserStore) AnalyticsActiveCount(timePeriod int64, options model.User query := us.getQueryBuilder().Select("COUNT(*)").From("Status AS s").Where("LastActivityAt > ?", time) if !options.IncludeBotAccounts { - query = query.LeftJoin("Bots ON s.UserId = Bots.UserId").Where("Bots.UserId IS NULL") + if us.DriverName() == model.DatabaseDriverPostgres { + query = query.LeftJoin("Bots ON s.UserId = Bots.UserId").Where("Bots.UserId IS NULL") + } else { + query = query.Where(sq.Expr("UserId NOT IN (SELECT UserId FROM Bots)")) + } } - if !options.IncludeDeleted { query = query.LeftJoin("Users ON s.UserId = Users.Id").Where("Users.DeleteAt = 0") } queryStr, args, err := query.ToSql() - if err != nil { return 0, errors.Wrap(err, "analytics_active_count_tosql") } @@ -1384,7 +1386,11 @@ func (us SqlUserStore) AnalyticsActiveCountForPeriod(startTime int64, endTime in query := us.getQueryBuilder().Select("COUNT(*)").From("Status AS s").Where("LastActivityAt > ? AND LastActivityAt <= ?", startTime, endTime) if !options.IncludeBotAccounts { - query = query.LeftJoin("Bots ON s.UserId = Bots.UserId").Where("Bots.UserId IS NULL") + if us.DriverName() == model.DatabaseDriverPostgres { + query = query.LeftJoin("Bots ON s.UserId = Bots.UserId").Where("Bots.UserId IS NULL") + } else { + query = query.Where(sq.Expr("UserId NOT IN (SELECT UserId FROM Bots)")) + } } if !options.IncludeDeleted { @@ -1392,7 +1398,6 @@ func (us SqlUserStore) AnalyticsActiveCountForPeriod(startTime int64, endTime in } queryStr, args, err := query.ToSql() - if err != nil { return 0, errors.Wrap(err, "Failed to build query.") }