批量update的问题
表结构1
create table a1(id int(1) not null primary key auto_increment,x1 varchar(122));
insert into a1 values('1','a1');
insert into a1 values('2','a2');
表结构2
create table a2(id int(1) not null primary key auto_increment,x1 varchar(122),x2 varchar(122));
insert into a2 values('1','a1','a1');
insert into a2 values('2','a2','a2');
insert into a2 values('3','a2','a2');
insert into a2 values('4','a1','a1');
insert into a2 values('5','a2','a2');
insert into a2 values('6','a1','a1');
现在执行这个SQL。将表a2中的字段x1的值换成表a1中x1字段值对应的id
update a2 a2 set a2.x1 = (select a1.id from a1 a1 where a1.x1 =a2.x1)
变成这个结果。
11a1
22a2
32a2
41a1
52a2
61a1
其中a2表中的数据是1000万,a1表中2000条记录。
update a2 a2 set a2.x1 = (select a1.id from a1 a1 where a1.x1 =a2.x1)
有没比这个更高效的SQL
[解决办法]
update a2,a1
set a2.x1=a1.id
where a2.x1=a1.x1;
a1.x1上加索引
[解决办法]
一楼的就已经很高效了,添加上索引即可。