admin管理员组文章数量:1128329
I need to update multiple columns of a table t1 with random rows from another table t2. t1 has ~ 1 Million Rows, t2 has 50k
I had originally tried to use this
Update t1 SET
field1=(select t2.field1 from t2 order by rand() limit 1),
field2=(select t2.field2 from t2 order by rand() limit 1)
+----+----------------------+------------+------------+-------+---------------+---------+---------+------+--------+----------+---------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+----------------------+------------+------------+-------+---------------+---------+---------+------+--------+----------+---------------------------------+
| 1 | UPDATE | t1| NULL | index | NULL | PRIMARY | 4 | NULL | 932170| 100.00 | NULL |
| 3 | UNCACHEABLE SUBQUERY | t2| NULL | ALL | NULL | NULL | NULL | NULL | 49717 | 100.00 | Using temporary; Using filesort |
| 2 | UNCACHEABLE SUBQUERY | t2| NULL | ALL | NULL | NULL | NULL | NULL | 49717| 100.00 | Using temporary; Using filesort |
+----+----------------------+------------+------------+-------+---------------+---------+---------+------+--------+----------+---------------------------------+
But the performance was poor, I then tried
Update t1 SET
field1=(select t2.field1 from t2 where id=FLOOR(1+rand()*50000) limit 1),
field2=(select t2.field2 from t2 where id=FLOOR(1+rand()*50000) limit 1)
+----+----------------------+------------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+----------------------+------------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+
| 1 | UPDATE | t1| NULL | index | NULL | PRIMARY | 4 | NULL | 932170| 100.00 | NULL |
| 3 | UNCACHEABLE SUBQUERY | t2| NULL | ALL | NULL | NULL | NULL | NULL | 49717| 10.00 | Using where |
| 2 | UNCACHEABLE SUBQUERY | t2| NULL | ALL | NULL | NULL | NULL | NULL | 49717| 10.00 | Using where |
+----+----------------------+------------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+
But this is even slower, is there a better way to do this?
Edit: Table info and Plans
CREATE TABLE t1 (
id int,
field1 varchar(400),
field2 varchar(400)
PRIMARY KEY(id))
CREATE TABLE t2 (
id int,
field1 varchar(400),
field2 varchar(400)
PRIMARY KEY(id))
This post does not answer my question because it is not related to an update statement, and im not sure how to change it to work with an update
本文标签: sqlPerformant Update of multiple columns using random values from another tableStack Overflow
版权声明:本文标题:sql - Performant Update of multiple columns using random values from another table - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736702614a1948505.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论