sql - Proper MySQL way to add a column from one table to another -
i have large table (~10 million records) contains several keys other, smaller tables. keys unique in each of smaller tables not in large one. add column large table 1 of smaller tables based on keys matching, i'm not sure of "right" way it. have solution works, takes fair amount of time (thought may unavoidable) , doesn't feel it's optimal way it. here's have:
create table new_big_table big_table; alter table new_big_table add(new_column tinyint not null); insert new_big_table select big_table.*, smaller_table.my_column big_table join smaller_table on big_table.key1 = smaller_table.key1 , big_table.key2 = smaller_table.key2;
this gets job done, smells i'm doing wrong. seems @ minimum shouldn't need create duplicate of table done. there more direct (and more efficient?) way of doing this?
it might worth mentioning personal, hobby project @ home, free hog resources of machine (since i'm 1 using it). such, if there straightforward performance tuning tips doing things this, i'd appreciate them (i'm experimenting on amazon ec2 instance since should lot faster , have more memory personal desktop).
have tried add column in-place?
alter table big_table add new_column tinyint; update big_table bt join smaller_table st on bt.key1 = st.key1 , bt.key2 = st.key2 set bt.new_column = st.my_column; alter table big_table modify new_column tinyint not null;
Comments
Post a Comment