Permalink
Browse files

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)
  • Loading branch information...
2 parents 9c37416 + f9b9179 commit 11afb74089b0e8ad9b85de3f3fcb1f6dd179fa66 @jonleighton jonleighton committed Nov 19, 2011
@@ -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
@@ -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)

0 comments on commit 11afb74

Please sign in to comment.