优化加入后更新查询,并在大表上加入语句

发布于 2025-02-02 16:37:31 字数 998 浏览 4 评论 0原文

我有2个桌子A和B。 A包含〜6M记录和B〜1M记录。 b包含A列名称A_ID的外键。 我需要移动表之间的关系,因此表A将包含B_ID,然后从B中删除A_ID。

我创建了此迁移:

UPDATE A
SET b_id = B.id
FROM B
INNER JOIN A ON B.a_id = A.id;

这是解释查询:

"Update on A  (cost=64444.71..138800306174.79 rows=7984529761815 width=915)"
"  ->  Nested Loop  (cost=64444.71..138800306174.79 rows=7984529761815 width=915)"
"        ->  Seq Scan on A  (cost=0.00..485941.45 rows=5779245 width=899)"
"        ->  Materialize  (cost=64444.71..642396.62 rows=1381587 width=16)"
"              ->  Hash Join  (cost=64444.71..628741.69 rows=1381587 width=16)"
"                    Hash Cond: (A.id = B.a_id)"
"                    ->  Seq Scan on A  (cost=0.00..485941.45 rows=5779245 width=10)"
"                    ->  Hash  (cost=40427.87..40427.87 rows=1381587 width=14)"
"                          ->  Seq Scan on B (cost=0.00..40427.87 rows=1381587 width=14)"

这需要太长(超过半小时),超过我有迁移的时间窗口。为了优化更新语句,有什么要做的吗?

I have 2 tables A and B.
A contains ~6M records and B ~1M records.
B contains foreign key to A, column name a_id.
I need to move the relationship between the tables, so table A will contain b_id, and remove the a_id from B.

I created this migration:

UPDATE A
SET b_id = B.id
FROM B
INNER JOIN A ON B.a_id = A.id;

This is the explain query:

"Update on A  (cost=64444.71..138800306174.79 rows=7984529761815 width=915)"
"  ->  Nested Loop  (cost=64444.71..138800306174.79 rows=7984529761815 width=915)"
"        ->  Seq Scan on A  (cost=0.00..485941.45 rows=5779245 width=899)"
"        ->  Materialize  (cost=64444.71..642396.62 rows=1381587 width=16)"
"              ->  Hash Join  (cost=64444.71..628741.69 rows=1381587 width=16)"
"                    Hash Cond: (A.id = B.a_id)"
"                    ->  Seq Scan on A  (cost=0.00..485941.45 rows=5779245 width=10)"
"                    ->  Hash  (cost=40427.87..40427.87 rows=1381587 width=14)"
"                          ->  Seq Scan on B (cost=0.00..40427.87 rows=1381587 width=14)"

This is taking too long(More than half an hour), more than my time window i have for the migration. Is there anything to do in order to optimize the update statement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文