From bf79aa4fc14aeb2646331e767038acf0b77e9e7f Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sun, 27 Dec 2015 23:12:41 +0900 Subject: Improve `select_one` in `Mysql2Adapter` Avoid instanciate `ActiveRecord::Result` and calling `ActiveRecord::Result#hash_rows` for the performance. --- .../lib/active_record/connection_adapters/mysql2_adapter.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb') diff --git a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb index f4686b680c..7fbe2307c7 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb @@ -127,6 +127,16 @@ module ActiveRecord # execute(sql, name).map { |row| row.first } # end + # Returns a record hash with the column names as keys and column values + # as values. + def select_one(arel, name = nil, binds = []) + arel, binds = binds_from_relation(arel, binds) + execute(to_sql(arel, binds), name).each(as: :hash) do |row| + @connection.next_result while @connection.more_results? + return row + end + end + # Returns an array of arrays containing the field values. # Order is the same as that returned by +columns+. def select_rows(sql, name = nil, binds = []) -- cgit v1.2.3