PERF: PG queries for the UserEmail#email column was not using the index.

This commit is contained in:
Guo Xiang Tan 2018-03-19 11:31:14 +08:00
parent f053e4cf37
commit 52b9af10a1
3 changed files with 5 additions and 3 deletions

View File

@ -15,7 +15,7 @@ class FinishInstallationController < ApplicationController
email = params[:email].strip email = params[:email].strip
raise Discourse::InvalidParameters.new unless @allowed_emails.include?(email) raise Discourse::InvalidParameters.new unless @allowed_emails.include?(email)
return redirect_confirm(email) if UserEmail.exists?(email: email) return redirect_confirm(email) if UserEmail.where("lower(email) = ?", email).exists?
@user.email = email @user.email = email
@user.username = params[:username] @user.username = params[:username]

View File

@ -134,7 +134,9 @@ class User < ActiveRecord::Base
# set to true to optimize creation and save for imports # set to true to optimize creation and save for imports
attr_accessor :import_mode attr_accessor :import_mode
scope :with_email, ->(email) { joins(:user_emails).where(user_emails: { email: email }) } scope :with_email, ->(email) do
joins(:user_emails).where("lower(user_emails.email) = ?", email)
end
scope :human_users, -> { where('users.id > 0') } scope :human_users, -> { where('users.id > 0') }

View File

@ -104,7 +104,7 @@ class AdminUserIndexQuery
def filter_by_user_with_bypass(filter) def filter_by_user_with_bypass(filter)
if filter =~ /.+@.+/ if filter =~ /.+@.+/
# probably an email so try the bypass # probably an email so try the bypass
user_id = UserEmail.where(email: filter.downcase).pluck(:user_id).first user_id = UserEmail.where("lower(email) = ?", filter.downcase).pluck(:user_id).first
if user_id if user_id
return @query.where('users.id = ?', user_id) return @query.where('users.id = ?', user_id)
end end