我试图根据另一个MySQL表的信息更新一个MySQL表.
我的original
号桌看起来像:
id | value
------------
1 | hello
2 | fortune
3 | my
4 | old
5 | friend
tobeupdated
号桌看起来像:
uniqueid | id | value
---------------------
1 | | something
2 | | anything
3 | | old
4 | | friend
5 | | fortune
我想根据value
(存储在VARCHAR(32)
字段中的字符串)将tobeupdated
中的id
更新为original
中的id
.
希望更新后的表格如下所示:
uniqueid | id | value
---------------------
1 | | something
2 | | anything
3 | 4 | old
4 | 5 | friend
5 | 2 | fortune
我有一个有效的查询,但速度非常慢:
UPDATE tobeupdated, original
SET tobeupdated.id = original.id
WHERE tobeupdated.value = original.value
这会耗尽我的CPU,最终导致超时,只执行了一小部分更新(有数千个值需要匹配).我知道匹配value
会很慢,但这是我唯一需要匹配的数据.
有没有更好的方法来更新这样的值?我可以为合并后的结果创建第三个表,如果这样更快的话?
我试了MySQL - How can I update a table with values from another table?次,但都没用.有什么 idea 吗?
提前感谢您对MySQL新手的帮助!