From 6b12aa7610f483971cfc65fdfd9e47c147288c85 Mon Sep 17 00:00:00 2001 From: Bartek Wrona <wrona@syncad.com> Date: Sat, 7 Nov 2020 23:50:10 +0100 Subject: [PATCH] Changes in index definitions, to significantly speedup 'condenser_api.get_trending_tags' call. Dedicated index created: hive_posts_category_id_payout_plus_pending_payout_depth_idx Other indexes (mostly used by bridge_api.get_ranked_posts) has been extended by WHERE condition to eliminate rows does not matching conditions. This way indexes size has been significantly reduced. --- hive/db/db_state.py | 10 +++--- hive/db/schema.py | 10 +++--- .../upgrade/upgrade_table_schema.sql | 31 +++++++++++++++++++ 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/hive/db/db_state.py b/hive/db/db_state.py index 1638a273e..b6af6fa05 100644 --- a/hive/db/db_state.py +++ b/hive/db/db_state.py @@ -108,17 +108,17 @@ class DbState: 'hive_posts_root_id_id_idx', 'hive_posts_community_id_idx', - 'hive_posts_category_id_idx', 'hive_posts_payout_at_idx', 'hive_posts_payout_idx', - 'hive_posts_promoted_idx', - 'hive_posts_sc_trend_id_is_paidout_idx', - 'hive_posts_sc_hot_id_is_paidout_idx', + 'hive_posts_promoted_id_idx', + 'hive_posts_sc_trend_id_idx', + 'hive_posts_sc_hot_id_idx', 'hive_posts_block_num_idx', 'hive_posts_block_num_created_idx', 'hive_posts_cashout_time_id_idx', 'hive_posts_updated_at_idx', - 'hive_posts_payout_plus_pending_payout_id_is_paidout_idx', + 'hive_posts_payout_plus_pending_payout_id_idx', + 'hive_posts_category_id_payout_plus_pending_payout_depth_idx', 'hive_posts_api_helper_author_s_permlink_idx', diff --git a/hive/db/schema.py b/hive/db/schema.py index 046aed727..a1fc3ec4c 100644 --- a/hive/db/schema.py +++ b/hive/db/schema.py @@ -139,18 +139,18 @@ def build_metadata(): sa.Index('hive_posts_parent_id_idx', 'parent_id'), sa.Index('hive_posts_community_id_idx', 'community_id'), - sa.Index('hive_posts_category_id_idx', 'category_id'), sa.Index('hive_posts_payout_at_idx', 'payout_at'), sa.Index('hive_posts_payout_idx', 'payout'), - sa.Index('hive_posts_promoted_idx', 'promoted'), - sa.Index('hive_posts_sc_trend_id_is_paidout_idx', 'sc_trend', 'id', 'is_paidout'), - sa.Index('hive_posts_sc_hot_id_is_paidout_idx', 'sc_hot', 'id', 'is_paidout'), + sa.Index('hive_posts_promoted_id_idx', 'promoted', 'id', postgresql_where=sql_text("NOT is_paidout AND counter_deleted = 0")), + sa.Index('hive_posts_sc_trend_id_idx', 'sc_trend', 'id', postgresql_where=sql_text("NOT is_paidout AND counter_deleted = 0 AND depth = 0")), + sa.Index('hive_posts_sc_hot_id_idx', 'sc_hot', 'id', postgresql_where=sql_text("NOT is_paidout AND counter_deleted = 0 AND depth = 0")), sa.Index('hive_posts_author_id_created_at_idx', sa.text('author_id DESC, created_at DESC')), sa.Index('hive_posts_block_num_idx', 'block_num'), sa.Index('hive_posts_block_num_created_idx', 'block_num_created'), sa.Index('hive_posts_cashout_time_id_idx', 'cashout_time', 'id'), sa.Index('hive_posts_updated_at_idx', sa.text('updated_at DESC')), - sa.Index('hive_posts_payout_plus_pending_payout_id_is_paidout_idx', sa.text('(payout+pending_payout), id, is_paidout')) + sa.Index('hive_posts_payout_plus_pending_payout_id_idx', sa.text('(payout+pending_payout), id, is_paidout'), postgresql_where=sql_text("counter_deleted = 0 AND NOT is_paidout")), + sa.Index('hive_posts_category_id_payout_plus_pending_payout_depth_idx', sa.text('category_id, (payout+pending_payout), depth'), postgresql_where=sql_text("NOT is_paidout AND counter_deleted = 0")) ) sa.Table( diff --git a/hive/db/sql_scripts/upgrade/upgrade_table_schema.sql b/hive/db/sql_scripts/upgrade/upgrade_table_schema.sql index 85991ad09..743dce0c3 100644 --- a/hive/db/sql_scripts/upgrade/upgrade_table_schema.sql +++ b/hive/db/sql_scripts/upgrade/upgrade_table_schema.sql @@ -326,3 +326,34 @@ CREATE INDEX IF NOT EXISTS hive_notification_cache_dst_score_idx ON hive_notific 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); +--- condenser_get_trending_tags optimizations and slight index improvements. + +DROP INDEX IF EXISTS hive_posts_category_id_idx; + +CREATE INDEX IF NOT EXISTS hive_posts_category_id_payout_plus_pending_payout_depth_idx ON hive_posts (category_id, (payout + pending_payout), depth) + WHERE NOT is_paidout AND counter_deleted = 0; + +DROP INDEX IF EXISTS hive_posts_sc_trend_id_is_paidout_idx; + +CREATE INDEX IF NOT EXISTS hive_posts_sc_trend_id_idx ON hive_posts USING btree (sc_trend, id) + WHERE NOT is_paidout AND counter_deleted = 0 AND depth = 0 +; + +DROP INDEX IF EXISTS hive_posts_sc_hot_id_is_paidout_idx; + +CREATE INDEX IF NOT EXISTS hive_posts_sc_hot_id_idx ON hive_posts (sc_hot, id) + WHERE NOT is_paidout AND counter_deleted = 0 AND depth = 0 + ; + +DROP INDEX IF EXISTS hive_posts_payout_plus_pending_payout_id_is_paidout_idx; + +CREATE INDEX IF NOT EXISTS hive_posts_payout_plus_pending_payout_id_idx ON hive_posts ((payout + pending_payout), id) + WHERE counter_deleted = 0 AND NOT is_paidout +; + +DROP INDEX IF EXISTS hive_posts_promoted_idx; + +CREATE INDEX IF NOT EXISTS hive_posts_promoted_id_idx ON hive_posts (promoted, id) + WHERE NOT is_paidout AND counter_deleted = 0 + ; + -- GitLab