Add CTE in update_posts_rshares
query plan:
"Update on hive_posts hp (cost=49319711.40..51880296.38 rows=0 width=0)"
" CTE votes_rshares_view"
" -> HashAggregate (cost=49306718.70..49319710.83 rows=866142 width=84)"
" Group Key: hv.post_id"
" -> Seq Scan on hive_votes hv (cost=0.00..29444020.90 rows=993134890 width=13)"
" -> Nested Loop (cost=0.57..2560585.54 rows=860755 width=202)"
" -> CTE Scan on votes_rshares_view votes_rshares (cost=0.00..17322.84 rows=866142 width=192)"
" -> Index Scan using hive_posts_pkey on hive_posts hp (cost=0.57..2.43 rows=1 width=45)"
" Index Cond: (id = votes_rshares.post_id)"
" Filter: ((counter_deleted = 0) AND ((abs_rshares <> votes_rshares.abs_rshares) OR (vote_rshares <> votes_rshares.rshares) OR (total_votes <> votes_rshares.total_votes) OR (net_votes <> votes_rshares.net_votes)))"
instead of:
"Update on hive_posts hp (cost=49306484.28..51871414.98 rows=0 width=0)"
" -> Nested Loop (cost=49306484.28..51871414.98 rows=860783 width=202)"
" -> Subquery Scan on votes_rshares (cost=49306483.71..49328137.26 rows=866142 width=192)"
" -> HashAggregate (cost=49306483.71..49319475.84 rows=866142 width=84)"
" Group Key: hv.post_id"
" -> Seq Scan on hive_votes hv (cost=0.00..29443880.57 rows=993130157 width=13)"
" -> Index Scan using hive_posts_pkey on hive_posts hp (cost=0.57..2.43 rows=1 width=45)"
" Index Cond: (id = votes_rshares.post_id)"
" Filter: ((counter_deleted = 0) AND ((abs_rshares <> votes_rshares.abs_rshares) OR (vote_rshares <> votes_rshares.rshares) OR (total_votes <> votes_rshares.total_votes) OR (net_votes <> votes_rshares.net_votes)))"
CTE allows to move HashAggregate
out of the Nested Loop
,
time of CTE update in range (1,87000000)
~ 46 minutes
Edited by Michal Zander