我有两张桌子:
表1:
shareID | shareName
__________________
1 shareA
2 shareB
3 shareC
表2:
shareID | shareName
__________________
0 shareA
0 shareA
0 shareB
0 shareC
我需要将shareID从table1复制到table2以获取匹配的shareName。
我有这个有效的查询
UPDATE table1, table2 SET table2.shareID=table1.shareID WHERE table2.shareName=table1.shareName
但问题是table2有大约600K行,而table1大约有350.所以有关这个的查询需要太长时间。
最初我认为由于内存限制它不会起作用,但在我写这个问题时,查询已经完成,我有我需要的东西。大约5分钟或更长时间。但我想知道是否有更好的方法来做到这一点?
由于
答案 0 :(得分:2)
只需使用join
:
UPDATE table1 t1 JOIN
table2 t2
ON t2.shareName = t1.shareName
SET t2.shareId = t1.shareID ;
然后,添加一个索引:
create index idx_table1_shareName on table1(shareName);
实际上,我不确定哪个表对索引更好(我对您查询中的表名与问题中的名称不同这一事实感到困惑)。因此,在两个表上构建一个:
create index idx_table2_shareName on table2(shareName);