【发布时间】:2021-03-06 09:25:50
【问题描述】:
我的表结构如下:
table_a
id | customer_id | product_id
---+-------------+------
1 | c1 | p1
2 | c1 | p1
3 | c2 | p1
table_b
id | table_a_id | attribute
---+-------------+------
99 | 1 | a1
98 | 2 | a2
97 | 3 | a3
如您所见,table_a 有重复值,我想合并它们。
不幸的是,table_a PK 也用于table_b。
最终结果应该是:
table_a
id | customer_id | product_id
---+-------------+------
1 | c1 | p1
3 | c2 | p1
table_b
id | table_a_id | attribute
---+-------------+------
99 | 1 | a1
98 | 1 | a2
97 | 3 | a3
我必须更新 table_b 与 table_a 的关系,然后清除 table_a 上所有未设置的键。
不幸的是,我想到的唯一查询非常繁重,并且可以完成之前的数据库超时。 table_a 有 200k+ 条记录,table_b 至少是它的两倍。
我的想法是:
- 加入
table_a和table_b,得到:(table_b_id, table_a_customer_id, table_a_product_id) - 获取
table_a的分组版本。 (为了得到正确的idtable_a我刚刚使用了min("id") - 内联上面两个,用结果更新
table_b。
【问题讨论】:
标签: sql postgresql duplicates sql-update sql-delete