[MM-53407] server/user_store: avoid antijoin for AnalyticsActiveCount query (#23993)

This commit is contained in:
Ibrahim Serdar Acikgoz 2023-07-12 17:40:34 +03:00 committed by GitHub
parent abdf4e58c3
commit 51c6e77972
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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.")
}