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 1b2d5461f1c9a4cfc0022f556c50daee5065e873 1 parent c5197d4
Toshinori Kajihara kennyj authored jonleighton committed
22 activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
View
@@ -573,27 +573,9 @@ def show_variable(name)
# Returns a table's primary key and belonging sequence.
def pk_and_sequence_for(table)
keys = []
- result = execute("DESCRIBE #{quote_table_name(table)}", 'SCHEMA')
+ result = execute("SHOW INDEX FROM #{quote_table_name(table)} WHERE Key_name = 'PRIMARY'", 'SCHEMA')
result.each(:symbolize_keys => true, :as => :hash) do |row|
- keys << row[:Field] if row[:Key] == 'PRI'
- end
- keys.length == 1 ? [keys.first, nil] : nil
- end
-
- def detailed_pk_and_sequence_for(table) #:nodoc:
- keys = []
- 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
-
- result = execute(sql, 'SCHEMA')
- result.each(:symbolize_keys => true, :as => :hash) do |row|
- keys << row[:column_name] if row[:constraint_type] == "PRIMARY KEY"
+ keys << row[:Column_name]
end
keys.length == 1 ? [keys.first, nil] : nil
end
23 activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
View
@@ -756,28 +756,9 @@ def show_variable(name)
# Returns a table's primary key and belonging sequence.
def pk_and_sequence_for(table) #:nodoc:
keys = []
- result = execute("DESCRIBE #{quote_table_name(table)}", 'SCHEMA')
- result.each_hash do |row|
- keys << row['Field'] if row['Key'] == 'PRI'
- end
- result.free
- keys.length == 1 ? [keys.first, nil] : nil
- end
-
- def detailed_pk_and_sequence_for(table) #:nodoc:
- keys = []
- 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
-
- result = execute(sql, 'SCHEMA')
+ result = execute("SHOW INDEX FROM #{quote_table_name(table)} WHERE Key_name = 'PRIMARY'", 'SCHEMA')
result.each_hash do |h|
- keys << h["column_name"] if h["constraint_type"] == "PRIMARY KEY"
+ keys << h["Column_name"]
end
result.free
keys.length == 1 ? [keys.first, nil] : nil
4 activerecord/lib/active_record/schema_dumper.rb
View
@@ -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.