PERF: Speed up admin user list main query (#16412)

This drops the join with the emails table since primary emails is already
on the users table.

Makes query 10x faster on large (6M+ users) sites.
This commit is contained in:
Rafael dos Santos Silva 2022-04-08 11:59:44 -03:00 committed by GitHub
parent 0f7b9878ff
commit be519d2aec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 3 deletions

View File

@ -4,7 +4,7 @@ class AdminUserIndexQuery
def initialize(params = {}, klass = User, trust_levels = TrustLevel.levels)
@params = params
@query = initialize_query_with_order(klass.joins(:primary_email))
@query = initialize_query_with_order(klass)
@trust_levels = trust_levels
end
@ -53,7 +53,7 @@ class AdminUserIndexQuery
if !custom_order.present?
if params[:query] == "active"
order << "COALESCE(users.last_seen_at, to_date('1970-01-01', 'YYYY-MM-DD')) DESC"
order << "users.last_seen_at DESC NULLS LAST"
else
order << "users.created_at DESC"
end
@ -93,7 +93,7 @@ class AdminUserIndexQuery
def filter_by_search
if params[:email].present?
return @query.where('user_emails.email = ?', params[:email].downcase)
return @query.joins(:primary_email).where('user_emails.email = ?', params[:email].downcase)
end
filter = params[:filter]