문제

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?

도움이 되었습니까?

해결책

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;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top