From 1f23e1326f3010bc84353aba82d4aa7ff2f999e4 Mon Sep 17 00:00:00 2001
From: Bartek Wrona <wrona@syncad.com>
Date: Mon, 2 Nov 2020 10:21:59 +0100
Subject: [PATCH] Redefined helper index storing author_id, created_at pair to
 significantly speedup hive_accounts_info_view queries.

---
 hive/db/db_state.py                                  | 2 +-
 hive/db/schema.py                                    | 2 +-
 hive/db/sql_scripts/upgrade/upgrade_table_schema.sql | 7 +++++++
 3 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/hive/db/db_state.py b/hive/db/db_state.py
index f3f98834c..a823d01cc 100644
--- a/hive/db/db_state.py
+++ b/hive/db/db_state.py
@@ -99,7 +99,7 @@ class DbState:
 
             'hive_posts_parent_id_idx',
             'hive_posts_depth_idx',
-            'hive_posts_created_at_author_id_idx',
+            'hive_posts_author_id_created_at_idx',
             'hive_posts_root_id_id_idx',
 
             'hive_posts_community_id_idx',
diff --git a/hive/db/schema.py b/hive/db/schema.py
index 1a51eb749..f6a133272 100644
--- a/hive/db/schema.py
+++ b/hive/db/schema.py
@@ -144,7 +144,7 @@ def build_metadata():
         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_created_at_author_id_idx', 'created_at', 'author_id'),
+        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'),
diff --git a/hive/db/sql_scripts/upgrade/upgrade_table_schema.sql b/hive/db/sql_scripts/upgrade/upgrade_table_schema.sql
index 24fd2d572..149829cc2 100644
--- a/hive/db/sql_scripts/upgrade/upgrade_table_schema.sql
+++ b/hive/db/sql_scripts/upgrade/upgrade_table_schema.sql
@@ -286,3 +286,10 @@ IF EXISTS (SELECT * FROM pg_constraint WHERE conname='hive_votes_ux1') THEN
 END IF;
 END
 $$
+;
+
+--- Change definition of index hive_posts_created_at_author_id_idx to hive_posts_author_id_created_at_idx to improve hive_accounts_info_view performance.
+DROP INDEX IF EXISTS public.hive_posts_created_at_author_id_idx;
+
+CREATE INDEX IF NOT EXISTS hive_posts_author_id_created_at_idx ON public.hive_posts ( author_id DESC, created_at DESC);
+
-- 
GitLab