The following PostgreSQL query
UPDATE table_A A
SET is_active = false
FROM table_A
WHERE A.parent_id IS NULL AND A.is_active = true AND A.id = ANY
(SELECT (B.parent_id)
FROM table_A B
INNER JOIN table_B ON table_A.foreign_id = table_B.id
WHERE table_B.deleted = true);
gets stuck loading endlessly. I know correlated sub-queries can take long, but a SELECT using the same parameters worked quickly and returned the desired results. And I have a small data set that I let run for an entire day just to make sure it wouldn't eventually work with time.
Table_A uses hierarchical data structures and only a specific level of hierarchy has a foreign key that I can use to join and check the second table. The idea is :
Find all rows in Table_A whose associated Table_B row has its "deleted" value set to true.
From this set of results get the parent_id column
For any row in table_A whose id is part of the parent_id column, so for all parents, check if their is_active is true and if so make it false.
The EXPLAIN:
Update on table_A A (cost=0.00..3906658758867.89 rows=89947680 width=192) -> Nested Loop (cost=0.00..3906658758867.89 rows=89947680 width=192)
Join Filter: (SubPlan 1)
-> Seq Scan on table_A (cost=0.00..37899.20 rows=410720 width=14)
-> Materialize (cost=0.00..37901.39 rows=438 width=185)
-> Seq Scan on table_A A (cost=0.00..37899.20 rows=438 width=185)
Filter: ((parent_id IS NULL) AND is_active)
SubPlan 1
-> Nested Loop (cost=0.00..42405.74 rows=410720 width=8)
-> Seq Scan on table_B (cost=0.00..399.34 rows=1 width=0)
Filter: (deleted AND (table_A.foreign_id = id))
-> Seq Scan on table_A B (cost=0.00..37899.20 rows=410720 width=8)
JIT: Functions: 17 " Options: Inlining true, Optimization true, Expressions true, Deforming true"