Skip to content
This repository
Browse code

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...
commit 11afb74089b0e8ad9b85de3f3fcb1f6dd179fa66 2 parents 9c37416 + f9b9179
Jon Leighton authored November 19, 2011
20  activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -576,24 +576,8 @@ def show_variable(name)
576 576
 
577 577
       # Returns a table's primary key and belonging sequence.
578 578
       def pk_and_sequence_for(table)
579  
-        execute_and_free("DESCRIBE #{quote_table_name(table)}", 'SCHEMA') do |result|
580  
-          keys = each_hash(result).select { |row| row[:Key] == 'PRI' }.map { |row| row[:Field] }
581  
-          keys.length == 1 ? [keys.first, nil] : nil
582  
-        end
583  
-      end
584  
-
585  
-      def detailed_pk_and_sequence_for(table)
586  
-        sql = <<-SQL
587  
-          SELECT t.constraint_type, k.column_name
588  
-          FROM information_schema.table_constraints t
589  
-          JOIN information_schema.key_column_usage k
590  
-          USING (constraint_name, table_schema, table_name)
591  
-          WHERE t.table_schema = DATABASE()
592  
-            AND t.table_name   = '#{table}'
593  
-        SQL
594  
-
595  
-        execute_and_free(sql, 'SCHEMA') do |result|
596  
-          keys = each_hash(result).select { |row| row[:constraint_type] == 'PRIMARY KEY' }.map { |row| row[:column_name] }
  579
+        execute_and_free("SHOW INDEX FROM #{quote_table_name(table)} WHERE Key_name = 'PRIMARY'", 'SCHEMA') do |result|
  580
+          keys = each_hash(result).map { |row| row[:Column_name] }
597 581
           keys.length == 1 ? [keys.first, nil] : nil
598 582
         end
599 583
       end
4  activerecord/lib/active_record/schema_dumper.rb
@@ -86,9 +86,7 @@ def table(table, stream)
86 86
           tbl = StringIO.new
87 87
 
88 88
           # first dump primary key column
89  
-          if @connection.respond_to?(:detailed_pk_and_sequence_for)
90  
-            pk, _ = @connection.detailed_pk_and_sequence_for(table)
91  
-          elsif @connection.respond_to?(:pk_and_sequence_for)
  89
+          if @connection.respond_to?(:pk_and_sequence_for)
92 90
             pk, _ = @connection.pk_and_sequence_for(table)
93 91
           elsif @connection.respond_to?(:primary_key)
94 92
             pk = @connection.primary_key(table)

0 notes on commit 11afb74

Please sign in to comment.
Something went wrong with that request. Please try again.