diff options
author | Martin Schùˆrrer <martin@schuerrer.org> | 2014-03-03 17:44:55 +0100 |
---|---|---|
committer | Martin Schùˆrrer <martin@schuerrer.org> | 2014-03-04 10:05:30 +0100 |
commit | f317cc8bc007978d7b135ddd1acdd7e3d1e582a3 (patch) | |
tree | 26894ebe8a7d684799fd3397b861db769e3f877b /activerecord/lib | |
parent | ffcc6172b4d40ca7c8b02fd298c679b5bcf5787b (diff) | |
download | rails-f317cc8bc007978d7b135ddd1acdd7e3d1e582a3.tar.gz rails-f317cc8bc007978d7b135ddd1acdd7e3d1e582a3.tar.bz2 rails-f317cc8bc007978d7b135ddd1acdd7e3d1e582a3.zip |
Make exists? use bound values.
When we build a query with an inline value that is a numeric (e.g.
because it's out of range for an int4) PostgreSQL doesn't use an index
on the column, since it's now comparing numerics and not int4s.
This leads to a _very_ slow query.
When we use bound parameters instead of inline values PostgreSQL
raises numeric_value_out_of_range since no automatic coercion happens.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/relation/finder_methods.rb | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 7099bdd285..1ba7fc47c0 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -292,7 +292,12 @@ module ActiveRecord when Array, Hash relation = relation.where(conditions) else - relation = relation.where(table[primary_key].eq(conditions)) if conditions != :none + if conditions != :none + column = columns_hash[primary_key] + substitute = connection.substitute_at(column, bind_values.length) + relation = where(table[primary_key].eq(substitute)) + relation.bind_values += [[column, conditions]] + end end connection.select_value(relation, "#{name} Exists", relation.bind_values) ? true : false |