Question

I have two huge tables with 10 million rows in table A and 2.5 million rows in table B. Both tables have a common field id. Table A has ~250 columns and table B has 5 columns. All the ids in table B are present in table A. I want to add a field (in date format) in table A to table B. I have two options, and both are taking lot of time to run. I want to know which will be efficient.

Option 1:
alter table B add column field date;
update B join A using(id) set a.field=b.field;

Option 2:
create table C as select a.*,b.field from B join A using(id);

id is indexed in both the tables and ENGINE is MyISAM.

Which option will be faster?

I think 2 because in option 1, adding a column is taking time, then while updating, lot of time is taken for the state copy to tmp table. In option 1, it straight away starts with the state 'Sending data'. Am I correct?

Also, can I do this in any other faster way?

Was it helpful?

Solution

If you only whant to select information you can create a view to get all the data together

CREATE VIEW 'my_view' as select * from B join A on B.id = A.id;

so you can run

select field,any_field from my_view where any_condition;

having duplicated data in a database is absolutly unrecommend

i hope this help you. sice it isnt any of your options.


you can also run the query itself

 select  field,any_field from B join A on B.id = A.id where any_condition;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top