In my rails app I have a method that looks like this:
def find_person(field, attribute)
Person.where.not(Hash[field, nil])
.where.not("lower(#{field}) = ?", attribute.downcase)
.where("difference(#{field}, ?) < 2", attribute)
.order('RANDOM()')
.limit(3)
.pluck("lower(#{field})")
.uniq
end
This query is very slow because of .order('RANDOM()')
. How can I make this query faster?