简体   繁体   English

使用附加行添加和更新列/创建新表

[英]Add and update column / create new table with the additional row

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. 我有两个巨大的表,表A中有1000万行,表B中有250万行。两个表都有一个共同的字段ID。 Table A has ~250 columns and table B has 5 columns. 表A有~250列,表B有5列。 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. 表B中的所有ID都存在于表A中。我想在表A中向表B添加一个字段(以日期格式表示)。我有两个选项,两个都需要很长时间才能运行。 I want to know which will be efficient. 我想知道哪个会有效率。

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

Option 2: 选项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. id在两个表中都被索引,ENGINE是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 . 我认为2因为在选项1中,添加列需要时间,然后在更新时,将大量时间用于状态copy to tmp table In option 1, it straight away starts with the state 'Sending data'. 在选项1中,它立即从状态'发送数据'开始。 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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM