[Performance Improvement Idea] Add multiple columns to table using single ALTER statement

25 views
Skip to first unread message

Gaurish Sharma

unread,
Oct 17, 2015, 7:25:02 AM10/17/15
to Ruby on Rails: Core
To add 2 new cols to existing table in migration, we will have to do:


add_column :my_table, :col1, :string
add_column :my_table, :col2, :string


this will create 2 ALTER table statements.


Alter table my_table add column col1 varchar(255)
Alter table my_table add column col2 varchar(255)



I don't know about all databases, but atleast MySQL doesn't seem to modify the table "in place". MySQL actually creates a new table, as copy of the old table with the specified modifications.Two separate statements would require MySQL to do that copy operation twice.so I am wondering can we add support for adding multiple columns to table using single ALTER TABLE? if yes, then I see following approches

1. Add support to accept multiple cols, in add_column[1]
2. Introduce a new method add_columns
3. Internal optimisation, if there are multiple add_column calls to same table in a migration, consolidate them & generate single ALTER TABLE statement. not sure if that is possible?

If you any thoughts on this Performance Improvement Idea, let me know

--Gaurish 



Derek Prior

unread,
Oct 17, 2015, 7:51:35 AM10/17/15
to rubyonra...@googlegroups.com
I believe change_table, perhaps in combination with the bulk option, will do what you want. 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages