比如说有2张表,A和B,
AB表结构如下
create table A (
A_id int(20),
name varchar(20)
);
create table A (
B_id int(20),
A_id int(20), -- 此字段和A表的A_id关联
name varchar(20)
);
现在有以下场景,在海量数据的情况下,如果高效地将A表的数据更新到B表,我回答用子查询,就像这样
update B set B.name = (select name from A where A.A_id = B.A_id);
但是面试官说不行,数据量太大的情况下会很慢,不可行;
大家还有更好的办法吗?
update B t set name=a.name
from B b
join A a on a.A_id=b.A_id
where b.B_id=t.B_id
或者
update B t set name=a.name
from B b
,A a
where b.B_id=t.B_id AND a.A_id=b.A_id
大佬,我觉得这样应该会快。