diff --git a/hive/db/db_state.py b/hive/db/db_state.py index 3772593ecf3d3fbd2f8b9d80354b1a76bd318233..d8a53829925fb9dd4efe958f4d811e1b05fe600a 100644 --- a/hive/db/db_state.py +++ b/hive/db/db_state.py @@ -107,7 +107,7 @@ class DbState: 'hive_posts_author_id_created_at_idx', 'hive_posts_root_id_id_idx', - 'hive_posts_community_id_idx', + 'hive_posts_community_id_id_idx', 'hive_posts_payout_at_idx', 'hive_posts_payout_idx', 'hive_posts_promoted_id_idx', diff --git a/hive/db/schema.py b/hive/db/schema.py index 275d3d65d263e9d9c4e5bd965229dc87664b7533..3d3140ee15f1ed90d9522419958c6db71fa27c0a 100644 --- a/hive/db/schema.py +++ b/hive/db/schema.py @@ -139,7 +139,7 @@ def build_metadata(): sa.Index('hive_posts_root_id_id_idx', 'root_id','id'), sa.Index('hive_posts_parent_id_idx', 'parent_id'), - sa.Index('hive_posts_community_id_idx', 'community_id'), + sa.Index('hive_posts_community_id_id_idx', 'community_id', sa.text('id DESC')), sa.Index('hive_posts_payout_at_idx', 'payout_at'), sa.Index('hive_posts_payout_idx', 'payout'), diff --git a/hive/db/sql_scripts/bridge_get_ranked_post_for_observer_communities.sql b/hive/db/sql_scripts/bridge_get_ranked_post_for_observer_communities.sql index 85b1aa130b6b4493d696d003c81ca7ace7de242b..09d1cbe4e1f02c7ab4e382e5414ea8b0f06c12fc 100644 --- a/hive/db/sql_scripts/bridge_get_ranked_post_for_observer_communities.sql +++ b/hive/db/sql_scripts/bridge_get_ranked_post_for_observer_communities.sql @@ -9,52 +9,64 @@ DECLARE BEGIN __post_id = find_comment_id( _author, _permlink, True ); __account_id = find_account_id( _observer, True ); - RETURN QUERY SELECT - hp.id, - hp.author, - hp.parent_author, - hp.author_rep, - hp.root_title, - hp.beneficiaries, - hp.max_accepted_payout, - hp.percent_hbd, - hp.url, - hp.permlink, - hp.parent_permlink_or_category, - hp.title, - hp.body, - hp.category, - hp.depth, - hp.promoted, - hp.payout, - hp.pending_payout, - hp.payout_at, - hp.is_paidout, - hp.children, - hp.votes, - hp.created_at, - hp.updated_at, - hp.rshares, - hp.abs_rshares, - hp.json, - hp.is_hidden, - hp.is_grayed, - hp.total_votes, - hp.sc_trend, - hp.role_title, - hp.community_title, - hp.role_id, - hp.is_pinned, - hp.curator_payout_value, - hp.is_muted - FROM - hive_posts_view hp - JOIN hive_subscriptions hs ON hp.community_id = hs.community_id - JOIN hive_accounts_view ha ON ha.id = hp.author_id - WHERE hs.account_id = __account_id AND hp.depth = 0 AND NOT ha.is_grayed AND ( __post_id = 0 OR hp.id < __post_id ) - AND (NOT EXISTS (SELECT 1 FROM muted_accounts_by_id_view WHERE observer_id = __account_id AND muted_id = hp.author_id)) - ORDER BY hp.id DESC - LIMIT _limit; + RETURN QUERY + with post_ids as (select posts.id + from (select community_id + from hive_subscriptions + where account_id = __account_id) communities + cross join lateral (select hive_posts.id + from hive_posts + join hive_accounts on (hive_posts.author_id = hive_accounts.id) + where hive_posts.community_id = communities.community_id + and hive_posts.depth = 0 + and hive_posts.counter_deleted = 0 + and (__post_id = 0 OR hive_posts.id < __post_id) + and hive_accounts.reputation > '-464800000000'::bigint + order by id desc + limit _limit) posts + order by id desc + limit _limit) + SELECT + hp.id, + hp.author, + hp.parent_author, + hp.author_rep, + hp.root_title, + hp.beneficiaries, + hp.max_accepted_payout, + hp.percent_hbd, + hp.url, + hp.permlink, + hp.parent_permlink_or_category, + hp.title, + hp.body, + hp.category, + hp.depth, + hp.promoted, + hp.payout, + hp.pending_payout, + hp.payout_at, + hp.is_paidout, + hp.children, + hp.votes, + hp.created_at, + hp.updated_at, + hp.rshares, + hp.abs_rshares, + hp.json, + hp.is_hidden, + hp.is_grayed, + hp.total_votes, + hp.sc_trend, + hp.role_title, + hp.community_title, + hp.role_id, + hp.is_pinned, + hp.curator_payout_value, + hp.is_muted + from post_ids + join hive_posts_view hp using (id) + order by id desc; END $function$ language plpgsql STABLE; diff --git a/hive/db/sql_scripts/upgrade/upgrade_table_schema.sql b/hive/db/sql_scripts/upgrade/upgrade_table_schema.sql index 4eb0b13cfa50ac443f08b0732e740915221bc3cd..e0e6d418cc6bef25fc5ebe1c9e16d63efccd2b3b 100644 --- a/hive/db/sql_scripts/upgrade/upgrade_table_schema.sql +++ b/hive/db/sql_scripts/upgrade/upgrade_table_schema.sql @@ -73,10 +73,12 @@ drop index if exists hive_subscriptions_community_idx; drop index if exists hive_votes_post_id_idx; drop index if exists hive_votes_voter_id_idx; drop index if exists hive_votes_last_update_idx; +drop index if exists hive_posts_community_id_idx; CREATE INDEX IF NOT EXISTS hive_posts_cashout_time_id_idx ON hive_posts (cashout_time, id); CREATE INDEX IF NOT EXISTS hive_posts_updated_at_idx ON hive_posts (updated_at DESC); CREATE INDEX IF NOT EXISTS hive_votes_block_num_idx ON hive_votes (block_num); +CREATE INDEX IF NOT EXISTS hive_posts_community_id_id_idx ON hive_posts (community_id, id DESC); DO $BODY$