Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use `show index from`. We could fix `pk_and_sequence_for` method's pe…

…rformance problem (GH #3678)
  • Loading branch information...
commit f9b917901608cda534d54b83b7d251c1fc73e31b 1 parent 396ef44
@kennyj kennyj authored
View
20 activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -576,24 +576,8 @@ def show_variable(name)
# 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
View
4 activerecord/lib/active_record/schema_dumper.rb
@@ -86,9 +86,7 @@ def table(table, stream)
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)
Please sign in to comment.
Something went wrong with that request. Please try again.