MySQL 基于 ID 更新缓慢(150,000 条记录)
MySQL Update Based on ID Slow (150,000 records)
我正在尝试根据另一个 table 上的 ID 更新一个 table,但我遇到了一些性能问题。我在 table_to_update 上有 150,000 行,在 table_to_get_data.
上有 400,000 行
Table 更新
+----+-----------------+
| id | field_to_update |
+----+-----------------+
| 1 | orange |
| 2 | apple |
| 3 | pear |
| 1 | orange |
+----+-----------------+
Table 获取数据
+----+-----------------+
| id | field |
+----+-----------------+
| 1 | orange |
| 2 | apple |
| 3 | pear |
+----+-----------------+
所以我尝试了 3 种不同的方法:
方法一:
UPDATE table_to_update t1, table_to_get_data t2
SET t1.field_to_update = t2.field
WHERE t1.id = t2.id
方法二:
UPDATE table_to_update
JOIN table_to_get_data
ON table_to_update.id = table_to_get_data.id
SET table_to_update.field_to_update = table_to_get_data.field
方法三:
UPDATE table_to_update
LEFT JOIN table_to_get_data
ON table_to_update.id = table_to_get_data.id
SET table_to_update.field_to_update = table_to_get_data.field
到目前为止,方法 3 似乎是最快的,但是,计算更新 1000 行所需的时间,我需要 12 个小时才能完成整个 table 的更新。有没有更有效的方法来更新 table?
编辑:
添加了 EXPLAIN table
EXPLAIN Table
为您从两个表中加入的列创建索引。
它会为你创造奇迹。
我正在尝试根据另一个 table 上的 ID 更新一个 table,但我遇到了一些性能问题。我在 table_to_update 上有 150,000 行,在 table_to_get_data.
上有 400,000 行Table 更新
+----+-----------------+
| id | field_to_update |
+----+-----------------+
| 1 | orange |
| 2 | apple |
| 3 | pear |
| 1 | orange |
+----+-----------------+
Table 获取数据
+----+-----------------+
| id | field |
+----+-----------------+
| 1 | orange |
| 2 | apple |
| 3 | pear |
+----+-----------------+
所以我尝试了 3 种不同的方法:
方法一:
UPDATE table_to_update t1, table_to_get_data t2
SET t1.field_to_update = t2.field
WHERE t1.id = t2.id
方法二:
UPDATE table_to_update
JOIN table_to_get_data
ON table_to_update.id = table_to_get_data.id
SET table_to_update.field_to_update = table_to_get_data.field
方法三:
UPDATE table_to_update
LEFT JOIN table_to_get_data
ON table_to_update.id = table_to_get_data.id
SET table_to_update.field_to_update = table_to_get_data.field
到目前为止,方法 3 似乎是最快的,但是,计算更新 1000 行所需的时间,我需要 12 个小时才能完成整个 table 的更新。有没有更有效的方法来更新 table?
编辑: 添加了 EXPLAIN table EXPLAIN Table
为您从两个表中加入的列创建索引。
它会为你创造奇迹。