diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-11-19 05:00:37 -0800 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-11-19 05:00:37 -0800 |
commit | 11afb74089b0e8ad9b85de3f3fcb1f6dd179fa66 (patch) | |
tree | 65f954be5b9586a300cee71e29eb9587d1785581 | |
parent | 9c37416a3eaa446ec59cde1e7f8a913f62de3265 (diff) | |
parent | f9b917901608cda534d54b83b7d251c1fc73e31b (diff) | |
download | rails-11afb74089b0e8ad9b85de3f3fcb1f6dd179fa66.tar.gz rails-11afb74089b0e8ad9b85de3f3fcb1f6dd179fa66.tar.bz2 rails-11afb74089b0e8ad9b85de3f3fcb1f6dd179fa66.zip |
Merge pull request #3687 from kennyj/fix_3678-2
Use `show index from`. We could fix `pk_and_sequence_for` method's performance problem (GH #3678)
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb | 20 | ||||
-rw-r--r-- | activerecord/lib/active_record/schema_dumper.rb | 4 |
2 files changed, 3 insertions, 21 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index 3e043992e9..f72d2974c8 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -576,24 +576,8 @@ module ActiveRecord # Returns a table's primary key and belonging sequence. def pk_and_sequence_for(table) - execute_and_free("DESCRIBE #{quote_table_name(table)}", 'SCHEMA') do |result| - keys = each_hash(result).select { |row| row[:Key] == 'PRI' }.map { |row| row[:Field] } - keys.length == 1 ? [keys.first, nil] : nil - end - end - - def detailed_pk_and_sequence_for(table) - sql = <<-SQL - SELECT t.constraint_type, k.column_name - FROM information_schema.table_constraints t - JOIN information_schema.key_column_usage k - USING (constraint_name, table_schema, table_name) - WHERE t.table_schema = DATABASE() - AND t.table_name = '#{table}' - SQL - - execute_and_free(sql, 'SCHEMA') do |result| - keys = each_hash(result).select { |row| row[:constraint_type] == 'PRIMARY KEY' }.map { |row| row[:column_name] } + execute_and_free("SHOW INDEX FROM #{quote_table_name(table)} WHERE Key_name = 'PRIMARY'", 'SCHEMA') do |result| + keys = each_hash(result).map { |row| row[:Column_name] } keys.length == 1 ? [keys.first, nil] : nil end end diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index f70aa7a0bd..cdde5cf3b9 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -86,9 +86,7 @@ HEADER tbl = StringIO.new # first dump primary key column - if @connection.respond_to?(:detailed_pk_and_sequence_for) - pk, _ = @connection.detailed_pk_and_sequence_for(table) - elsif @connection.respond_to?(:pk_and_sequence_for) + if @connection.respond_to?(:pk_and_sequence_for) pk, _ = @connection.pk_and_sequence_for(table) elsif @connection.respond_to?(:primary_key) pk = @connection.primary_key(table) |