aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/connection_adapters/mysql_adapter.rb18
2 files changed, 17 insertions, 3 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 3311b082ca..52af1ea816 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Optimization for Mysql selects using mysql-ruby extension greater than 2.6.3. #2426. [skaes.web.de]
+
* Speed up the setting of table_name. #2428. [skaes@web.de]
* Optimize instantiation of STI subclass records. In partial fullfilment of #1236. [skaes@web.de]
diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
index 886363bcbe..4d034adec5 100755
--- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -4,20 +4,25 @@ module ActiveRecord
class Base
# Establishes a connection to the database that's used by all Active Record objects.
def self.mysql_connection(config) # :nodoc:
+ # Only include the MySQL driver if one hasn't already been loaded
unless self.class.const_defined?(:Mysql)
begin
- # Only include the MySQL driver if one hasn't already been loaded
require_library_or_gem 'mysql'
+ # The C version of mysql returns null fields in each_hash if Mysql::VERSION is defined
+ ConnectionAdapters::MysqlAdapter.null_values_in_each_hash = Mysql.const_defined?(:VERSION)
rescue LoadError => cannot_require_mysql
# Only use the supplied backup Ruby/MySQL driver if no driver is already in place
begin
require 'active_record/vendor/mysql'
require 'active_record/vendor/mysql411'
+ # The ruby version of mysql returns null fields in each_hash
+ ConnectionAdapters::MysqlAdapter.null_values_in_each_hash = true
rescue LoadError
raise cannot_require_mysql
end
end
end
+
config = config.symbolize_keys
host = config[:host]
@@ -73,6 +78,9 @@ module ActiveRecord
@@emulate_booleans = true
cattr_accessor :emulate_booleans
+ cattr_accessor :null_values_in_each_hash
+ @@null_values_in_each_hash = false
+
LOST_CONNECTION_ERROR_MESSAGES = [
"Server shutdown in progress",
"Broken pipe",
@@ -288,8 +296,12 @@ module ActiveRecord
@connection.query_with_result = true
result = execute(sql, name)
rows = []
- all_fields_initialized = result.fetch_fields.inject({}) { |all_fields, f| all_fields[f.name] = nil; all_fields }
- result.each_hash { |row| rows << all_fields_initialized.dup.update(row) }
+ if @@null_values_in_each_hash
+ result.each_hash { |row| rows << row }
+ else
+ all_fields = result.fetch_fields.inject({}) { |fields, f| fields[f.name] = nil; fields }
+ result.each_hash { |row| rows << all_fields.dup.update(row) }
+ end
result.free
rows
end