简体   繁体   English

用另一个MySQL表更新一个MySQL表

[英]Update one MySQL table with values from another

I'm trying to update one MySQL table based on information from another. 我正在尝试根据另一个MySQL信息更新一个MySQL表。

My original table looks like: 我的original表格如下:

id | value
------------
1  | hello
2  | fortune
3  | my
4  | old
5  | friend

And the tobeupdated table looks like: tobeupdated表看起来像:

uniqueid | id | value
---------------------
1        |    | something
2        |    | anything
3        |    | old
4        |    | friend
5        |    | fortune

I want to update id in tobeupdated with the id from original based on value (strings stored in VARCHAR(32) field). 我想根据value (存储在VARCHAR(32)字段中的字符串)使用original id来更新tobeupdated中的id

The updated table will hopefully look like: 希望更新后的表如下所示:

uniqueid | id | value
---------------------
1        |    | something
2        |    | anything
3        | 4  | old
4        | 5  | friend
5        | 2  | fortune

I have a query that works, but it's very slow: 我有一个有效的查询,但是非常慢:

UPDATE tobeupdated, original
SET tobeupdated.id = original.id
WHERE tobeupdated.value = original.value

This maxes out my CPU and eventually leads to a timeout with only a fraction of the updates performed (there are several thousand values to match). 这使我的CPU耗尽,最终导致超时,并且仅执行了一部分更新(有数千个要匹配的值)。 I know matching by value will be slow, but this is the only data I have to match them together. 我知道按value匹配会很慢,但这是我必须将它们匹配在一起的唯一数据。

Is there a better way to update values like this? 有没有更好的方法来更新这样的值? I could create a third table for the merged results, if that would be faster? 如果可以更快的速度,我可以为合并结果创建第三个表?

I tried MySQL - How can I update a table with values from another table? 我尝试过MySQL-如何使用另一个表中的值更新表? , but it didn't really help. ,但并没有真正的帮助。 Any ideas? 有任何想法吗?

Thanks in advance for helping a MySQL novice! 在此先感谢您帮助MySQL新手!

UPDATE tobeupdated
INNER JOIN original ON (tobeupdated.value = original.value)
SET tobeupdated.id = original.id

That should do it, and really its doing exactly what yours is. 那应该做到,实际上它确实在做您的工作。 However, I prefer 'JOIN' syntax for joins rather than multiple 'WHERE' conditions, I think its easier to read 但是,我更喜欢使用“ JOIN”语法进行连接,而不是使用多个“ WHERE”条件,我认为它更易于阅读

As for running slow, how large are the tables? 至于运行缓慢,表有多大? You should have indexes on tobeupdated.value and original.value 您应该在tobeupdated.valueoriginal.value上具有索引

EDIT: we can also simplify the query 编辑:我们还可以简化查询

UPDATE tobeupdated
INNER JOIN original USING (value)
SET tobeupdated.id = original.id

USING is shorthand when both tables of a join have an identical named key such as id . 当联接的两个表都具有相同的命名key例如id时, USING是简写形式。 ie an equi-join - http://en.wikipedia.org/wiki/Join_(SQL)#Equi-join 即一个等值联接-http: //en.wikipedia.org/wiki/Join_( SQL)#Equi-join

It depends what is a use of those tables, but you might consider putting trigger on original table on insert and update. 这取决于这些表的用途,但是您可以考虑在插入和更新时在原始表上放置触发器。 When insert or update is done, update the second table based on only one item from the original table. 插入或更新完成后,仅根据原始表中的一项更新第二个表。 It will be quicker. 这样会更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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