From a76b951e1c3b4b8e9b71daa33f35a2cecfd3303d Mon Sep 17 00:00:00 2001
From: jsalyers <jsalyers@syncad.com>
Date: Tue, 10 Nov 2020 19:03:48 -0500
Subject: [PATCH] [JES] Create a view for getting the muted accounts. Not
 In/Not Exists/Left Join all have extremely similar processing times. Went
 with Not In for readibility

---
 ...t_ranked_post_for_observer_communities.sql | 38 ++++---------------
 .../bridge_get_ranked_post_type.sql           |  3 +-
 2 files changed, 9 insertions(+), 32 deletions(-)

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 e879b8c7d..78f27b1fb 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,11 +9,7 @@ DECLARE
 BEGIN
   __post_id = find_comment_id( _author, _permlink, True );
   __account_id = find_account_id( _observer, True );
-  RETURN QUERY 
-    WITH muted_accounts AS (SELECT following as muted_account_id from hive_follows WHERE follower = __account_id AND state = 2 UNION
-                            SELECT hive_follows_indirect.following as muted_account_id FROM hive_follows hive_follows_direct JOIN hive_follows hive_follows_indirect ON hive_follows_direct.following = hive_follows_indirect.follower 
-                              WHERE hive_follows_direct.follower = __account_id AND hive_follows_direct.follow_muted AND hive_follows_indirect.state = 2)
-  SELECT
+  RETURN QUERY SELECT
       hp.id,
       hp.author,
       hp.parent_author,
@@ -49,8 +45,7 @@ BEGIN
       hp.community_title,
       hp.role_id,
       hp.is_pinned,
-      hp.curator_payout_value,
-      hp.is_muted
+      hp.curator_payout_value
   FROM
       hive_posts_view hp
       JOIN hive_subscriptions hs ON hp.community_id = hs.community_id
@@ -119,7 +114,6 @@ BEGIN
   FROM
       hive_posts_view hp
       JOIN hive_subscriptions hs ON hp.community_id = hs.community_id
-      LEFT JOIN muted_accounts ON hp.author_id = muted_accounts.muted_account_id 
   WHERE hs.account_id = __account_id AND NOT hp.is_paidout AND hp.depth = 0
       AND ( __post_id = 0 OR hp.sc_hot < __hot_limit OR ( hp.sc_hot = __hot_limit AND hp.id < __post_id ) )
       AND hp.author NOT IN (SELECT muted FROM muted_accounts_view WHERE observer = _observer)
@@ -144,11 +138,7 @@ BEGIN
       SELECT ( hp.payout + hp.pending_payout ) INTO __payout_limit FROM hive_posts hp WHERE hp.id = __post_id;
   END IF;
   __account_id = find_account_id( _observer, True );
-  RETURN QUERY
-  WITH muted_accounts AS (SELECT following as muted_account_id from hive_follows WHERE follower = __account_id AND state = 2 UNION
-                            SELECT hive_follows_indirect.following as muted_account_id FROM hive_follows hive_follows_direct JOIN hive_follows hive_follows_indirect ON hive_follows_direct.following = hive_follows_indirect.follower 
-                              WHERE hive_follows_direct.follower = __account_id AND hive_follows_direct.follow_muted AND hive_follows_indirect.state = 2)
-  SELECT
+  RETURN QUERY SELECT
       hp.id,
       hp.author,
       hp.parent_author,
@@ -224,11 +214,7 @@ BEGIN
   END IF;
   __account_id = find_account_id( _observer, True );
   __head_block_time = head_block_time();
-  RETURN QUERY
-  WITH muted_accounts AS (SELECT following as muted_account_id from hive_follows WHERE follower = __account_id AND state = 2 UNION
-                            SELECT hive_follows_indirect.following as muted_account_id FROM hive_follows hive_follows_direct JOIN hive_follows hive_follows_indirect ON hive_follows_direct.following = hive_follows_indirect.follower 
-                              WHERE hive_follows_direct.follower = __account_id AND hive_follows_direct.follow_muted AND hive_follows_indirect.state = 2)
-  SELECT
+  RETURN QUERY SELECT
       hp.id,
       hp.author,
       hp.parent_author,
@@ -269,7 +255,6 @@ BEGIN
   FROM
       hive_posts_view hp
       JOIN hive_subscriptions hs ON hp.community_id = hs.community_id
-      LEFT JOIN muted_accounts ON hp.author_id = muted_accounts.muted_account_id
   WHERE hs.account_id = __account_id AND NOT hp.is_paidout AND hp.payout_at BETWEEN __head_block_time + interval '12 hours' AND __head_block_time + interval '36 hours'
       AND ( __post_id = 0 OR ( hp.payout + hp.pending_payout ) < __payout_limit OR ( ( hp.payout + hp.pending_payout ) = __payout_limit AND hp.id < __post_id ) )
       AND hp.author NOT IN (SELECT muted FROM muted_accounts_view WHERE observer = _observer)
@@ -294,11 +279,7 @@ BEGIN
       SELECT hp.promoted INTO __promoted_limit FROM hive_posts hp WHERE hp.id = __post_id;
   END IF;
   __account_id = find_account_id( _observer, True );
-  RETURN QUERY
-  WITH muted_accounts AS (SELECT following as muted_account_id from hive_follows WHERE follower = __account_id AND state = 2 UNION
-                            SELECT hive_follows_indirect.following as muted_account_id FROM hive_follows hive_follows_direct JOIN hive_follows hive_follows_indirect ON hive_follows_direct.following = hive_follows_indirect.follower 
-                              WHERE hive_follows_direct.follower = __account_id AND hive_follows_direct.follow_muted AND hive_follows_indirect.state = 2)
-  SELECT
+  RETURN QUERY SELECT
       hp.id,
       hp.author,
       hp.parent_author,
@@ -339,7 +320,6 @@ BEGIN
   FROM
       hive_posts_view hp
       JOIN hive_subscriptions hs ON hp.community_id = hs.community_id
-      LEFT JOIN muted_accounts ON hp.author_id = muted_accounts.muted_account_id
   WHERE hs.account_id = __account_id AND NOT hp.is_paidout AND hp.promoted > 0
       AND ( __post_id = 0 OR hp.promoted < __promoted_limit OR ( hp.promoted = __promoted_limit AND hp.id < __post_id ) )
       AND hp.author NOT IN (SELECT muted FROM muted_accounts_view WHERE observer = _observer)
@@ -414,6 +394,7 @@ BEGIN
       WHERE
           hs.account_id = __account_id AND hp1.counter_deleted = 0 AND NOT hp1.is_paidout AND hp1.depth = 0
           AND ( __post_id = 0 OR hp1.sc_trend < __trending_limit OR ( hp1.sc_trend = __trending_limit AND hp1.id < __post_id ) )
+          AND hp.author NOT IN (SELECT muted FROM muted_accounts_view WHERE observer = _observer)
       ORDER BY hp1.sc_trend DESC, hp1.id DESC
       LIMIT _limit
   ) trending
@@ -440,11 +421,7 @@ BEGIN
       SELECT ( hp.payout + hp.pending_payout ) INTO __payout_limit FROM hive_posts hp WHERE hp.id = __post_id;
   END IF;
   __account_id = find_account_id( _observer, True );
-  RETURN QUERY
-  WITH muted_accounts AS (SELECT following as muted_account_id from hive_follows WHERE follower = __account_id AND state = 2 UNION
-                            SELECT hive_follows_indirect.following as muted_account_id FROM hive_follows hive_follows_direct JOIN hive_follows hive_follows_indirect ON hive_follows_direct.following = hive_follows_indirect.follower 
-                              WHERE hive_follows_direct.follower = __account_id AND hive_follows_direct.follow_muted AND hive_follows_indirect.state = 2)
-  SELECT
+  RETURN QUERY SELECT
       hp.id,
       hp.author,
       hp.parent_author,
@@ -486,7 +463,6 @@ BEGIN
       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
-      LEFT JOIN muted_accounts ON hp.author_id = muted_accounts.muted_account_id
   WHERE hs.account_id = __account_id AND NOT hp.is_paidout AND ha.is_grayed AND ( hp.payout + hp.pending_payout ) > 0
       AND ( __post_id = 0 OR ( hp.payout + hp.pending_payout ) < __payout_limit OR ( ( hp.payout + hp.pending_payout ) = __payout_limit AND hp.id < __post_id ) )
       AND hp.author NOT IN (SELECT muted FROM muted_accounts_view WHERE observer = _observer)
diff --git a/hive/db/sql_scripts/bridge_get_ranked_post_type.sql b/hive/db/sql_scripts/bridge_get_ranked_post_type.sql
index e486bf70c..af5aab718 100644
--- a/hive/db/sql_scripts/bridge_get_ranked_post_type.sql
+++ b/hive/db/sql_scripts/bridge_get_ranked_post_type.sql
@@ -35,7 +35,8 @@ CREATE TYPE bridge_api_post AS (
     community_title VARCHAR,
     role_id SMALLINT,
     is_pinned BOOLEAN,
-    curator_payout_value VARCHAR
+    curator_payout_value VARCHAR,
+    is_muted BOOLEAN
 );
 
 DROP TYPE IF EXISTS bridge_api_post_reblogs CASCADE;
-- 
GitLab