From 775487c19d0b5cf0e0fc029addbf0f4ca8452f47 Mon Sep 17 00:00:00 2001 From: Bartek Wrona <wrona@syncad.com> Date: Thu, 5 Nov 2020 17:33:11 +0100 Subject: [PATCH] Fixed bugs in implementation of bridge_api.get_account_posts when sort = "feed" Eliminated performance problems in the above implementation. Defined 2 new indexes: hive_feed_cache_block_num_idx and hive_feed_cache_created_at_idx. --- hive/db/db_state.py | 3 ++ hive/db/schema.py | 5 +- .../condenser_get_by_feed_with_reblog.sql | 53 ++++++++----------- .../upgrade/upgrade_table_schema.sql | 32 ++++++----- 4 files changed, 46 insertions(+), 47 deletions(-) diff --git a/hive/db/db_state.py b/hive/db/db_state.py index a20a36ea0..1638a273e 100644 --- a/hive/db/db_state.py +++ b/hive/db/db_state.py @@ -94,6 +94,9 @@ class DbState: to_locate = [ 'hive_blocks_created_at_idx', + 'hive_feed_cache_block_num_idx', + 'hive_feed_cache_created_at_idx', + 'hive_follows_ix5a', # (following, state, created_at, follower) 'hive_follows_ix5b', # (follower, state, created_at, following) 'hive_follows_block_num_idx', diff --git a/hive/db/schema.py b/hive/db/schema.py index 239cab021..c1388fdea 100644 --- a/hive/db/schema.py +++ b/hive/db/schema.py @@ -286,9 +286,12 @@ def build_metadata(): sa.Column('post_id', sa.Integer, nullable=False), sa.Column('account_id', sa.Integer, nullable=False), sa.Column('created_at', sa.DateTime, nullable=False), - sa.Column('block_num', sa.Integer, nullable=True), + sa.Column('block_num', sa.Integer, nullable=False), sa.PrimaryKeyConstraint('account_id', 'post_id', name='hive_feed_cache_pk'), sa.ForeignKeyConstraint(['block_num'], ['hive_blocks.num'], name='hive_feed_cache_fk1'), + + sa.Index('hive_feed_cache_block_num_idx', 'block_num'), + sa.Index('hive_feed_cache_created_at_idx', 'created_at') ) sa.Table( diff --git a/hive/db/sql_scripts/condenser_get_by_feed_with_reblog.sql b/hive/db/sql_scripts/condenser_get_by_feed_with_reblog.sql index 22f17365f..1189ad0e0 100644 --- a/hive/db/sql_scripts/condenser_get_by_feed_with_reblog.sql +++ b/hive/db/sql_scripts/condenser_get_by_feed_with_reblog.sql @@ -1,33 +1,27 @@ DROP FUNCTION IF EXISTS condenser_get_by_feed_with_reblog; -CREATE OR REPLACE FUNCTION condenser_get_by_feed_with_reblog( - in _account VARCHAR, - in _author VARCHAR, - in _permlink VARCHAR, - in _limit INTEGER -) -RETURNS SETOF bridge_api_post -AS -$function$ +CREATE OR REPLACE FUNCTION condenser_get_by_feed_with_reblog( IN _account VARCHAR, IN _author VARCHAR, IN _permlink VARCHAR, IN _limit INTEGER) + RETURNS SETOF bridge_api_post + LANGUAGE 'plpgsql' + STABLE + ROWS 1000 +AS $BODY$ DECLARE __post_id INTEGER := 0; - __cutoff INTEGER; + __cutoff INTEGER := 0; __account_id INTEGER := find_account_id( _account, True ); - __min_data TIMESTAMP; + __min_date TIMESTAMP; BEGIN IF _permlink <> '' THEN __post_id = find_comment_id( _author, _permlink, True ); + SELECT MIN(hfc.created_at) INTO __min_date + FROM hive_feed_cache hfc + JOIN hive_follows hf ON hfc.account_id = hf.following + WHERE hf.state = 1 AND hf.follower = __account_id AND hfc.post_id = __post_id; END IF; __cutoff = block_before_head( '1 month' ); - __min_data = - ( - SELECT MIN(hfc.created_at) - FROM hive_feed_cache hfc - JOIN hive_follows hf ON hfc.account_id = hf.following - WHERE hf.state = 1 AND hf.follower = __account_id AND ( ( __post_id = 0 ) OR ( hfc.post_id = __post_id ) ) - ); RETURN QUERY SELECT hp.id, @@ -69,21 +63,16 @@ BEGIN FROM hive_posts_view hp JOIN ( - SELECT hfc.post_id + SELECT hfc.post_id, MIN(hfc.created_at) as min_created FROM hive_feed_cache hfc - JOIN - ( - SELECT following - FROM hive_follows - WHERE state = 1 AND follower = __account_id - ) T ON hfc.account_id = T.following - JOIN hive_feed_cache hfc2 ON hfc2.account_id = T.following AND( __post_id = 0 OR hfc.post_id <= __post_id ) - WHERE hfc.block_num > __cutoff + JOIN hive_follows hf ON hfc.account_id = hf.following + WHERE (__post_id = 0 OR hfc.created_at <= __min_date) + AND hfc.block_num > __cutoff AND hf.state = 1 AND hf.follower = __account_id GROUP BY hfc.post_id - HAVING ( __post_id = 0 ) OR ( MIN(hfc.created_at) <= __min_data ) - ORDER BY MIN(hfc.created_at) DESC + ORDER BY min_created DESC LIMIT _limit - ) T ON hp.id = T.post_id; + ) T ON hp.id = T.post_id + ORDER BY T.min_created DESC; END -$function$ -language plpgsql STABLE; +$BODY$ +; diff --git a/hive/db/sql_scripts/upgrade/upgrade_table_schema.sql b/hive/db/sql_scripts/upgrade/upgrade_table_schema.sql index 03abe3a36..85991ad09 100644 --- a/hive/db/sql_scripts/upgrade/upgrade_table_schema.sql +++ b/hive/db/sql_scripts/upgrade/upgrade_table_schema.sql @@ -304,21 +304,25 @@ WHERE NOT EXISTS (SELECT data_type --- Notification cache to significantly speedup notification APIs. CREATE TABLE IF NOT EXISTS hive_notification_cache ( - id BIGINT NOT NULL, - block_num INT NOT NULL, - type_id INT NOT NULL, - dst INT NULL, - src INT NULL, - dst_post_id INT NULL, - post_id INT NULL, - score INT NOT NULL, - created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL, - community_title VARCHAR(32) NULL, - community VARCHAR(16) NULL, - payload VARCHAR NULL, - - CONSTRAINT hive_notification_cache_pk PRIMARY KEY (id) + id BIGINT NOT NULL, + block_num INT NOT NULL, + type_id INT NOT NULL, + dst INT NULL, + src INT NULL, + dst_post_id INT NULL, + post_id INT NULL, + score INT NOT NULL, + created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL, + community_title VARCHAR(32) NULL, + community VARCHAR(16) NULL, + payload VARCHAR NULL, + + CONSTRAINT hive_notification_cache_pk PRIMARY KEY (id) ); CREATE INDEX IF NOT EXISTS hive_notification_cache_block_num_idx ON hive_notification_cache (block_num); CREATE INDEX IF NOT EXISTS hive_notification_cache_dst_score_idx ON hive_notification_cache (dst, score) WHERE dst IS NOT NULL; + +CREATE INDEX IF NOT EXISTS hive_feed_cache_block_num_idx on hive_feed_cache (block_num); +CREATE INDEX IF NOT EXISTS hive_feed_cache_created_at_idx on hive_feed_cache (created_at); + -- GitLab