]> Untitled Git - lemmy.git/commitdiff
Adding a materialized view for reply_view. Fixes #466
authorDessalines <tyhou13@gmx.com>
Wed, 29 Jan 2020 03:07:17 +0000 (22:07 -0500)
committerDessalines <tyhou13@gmx.com>
Wed, 29 Jan 2020 04:53:19 +0000 (23:53 -0500)
server/migrations/2020-01-29-011901_create_reply_materialized_view/down.sql [new file with mode: 0644]
server/migrations/2020-01-29-011901_create_reply_materialized_view/up.sql [new file with mode: 0644]
server/migrations/2020-01-29-030825_create_user_mention_materialized_view/down.sql [new file with mode: 0644]
server/migrations/2020-01-29-030825_create_user_mention_materialized_view/up.sql [new file with mode: 0644]
server/query_testing/generate_explain_reports.sh
server/src/db/user_mention_view.rs

diff --git a/server/migrations/2020-01-29-011901_create_reply_materialized_view/down.sql b/server/migrations/2020-01-29-011901_create_reply_materialized_view/down.sql
new file mode 100644 (file)
index 0000000..06ec597
--- /dev/null
@@ -0,0 +1,25 @@
+-- Drop the materialized / built views
+drop view reply_view;
+create view reply_view as 
+with closereply as (
+    select 
+    c2.id, 
+    c2.creator_id as sender_id, 
+    c.creator_id as recipient_id
+    from comment c
+    inner join comment c2 on c.id = c2.parent_id
+    where c2.creator_id != c.creator_id
+    -- Do union where post is null
+    union
+    select
+    c.id,
+    c.creator_id as sender_id,
+    p.creator_id as recipient_id
+    from comment c, post p
+    where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
+)
+select cv.*,
+closereply.recipient_id
+from comment_view cv, closereply
+where closereply.id = cv.id
+;
diff --git a/server/migrations/2020-01-29-011901_create_reply_materialized_view/up.sql b/server/migrations/2020-01-29-011901_create_reply_materialized_view/up.sql
new file mode 100644 (file)
index 0000000..ebbb1df
--- /dev/null
@@ -0,0 +1,27 @@
+-- https://github.com/dessalines/lemmy/issues/197
+drop view reply_view;
+
+-- Do the reply_view referencing the comment_mview
+create view reply_view as 
+with closereply as (
+    select 
+    c2.id, 
+    c2.creator_id as sender_id, 
+    c.creator_id as recipient_id
+    from comment c
+    inner join comment c2 on c.id = c2.parent_id
+    where c2.creator_id != c.creator_id
+    -- Do union where post is null
+    union
+    select
+    c.id,
+    c.creator_id as sender_id,
+    p.creator_id as recipient_id
+    from comment c, post p
+    where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
+)
+select cv.*,
+closereply.recipient_id
+from comment_mview cv, closereply
+where closereply.id = cv.id
+;
diff --git a/server/migrations/2020-01-29-030825_create_user_mention_materialized_view/down.sql b/server/migrations/2020-01-29-030825_create_user_mention_materialized_view/down.sql
new file mode 100644 (file)
index 0000000..d93ebc2
--- /dev/null
@@ -0,0 +1 @@
+drop view user_mention_mview;
diff --git a/server/migrations/2020-01-29-030825_create_user_mention_materialized_view/up.sql b/server/migrations/2020-01-29-030825_create_user_mention_materialized_view/up.sql
new file mode 100644 (file)
index 0000000..b0ae4e9
--- /dev/null
@@ -0,0 +1,67 @@
+create view user_mention_mview as 
+with all_comment as
+(
+  select
+  ca.*
+  from comment_aggregates_mview ca
+)
+
+select
+    ac.id,
+    um.id as user_mention_id,
+    ac.creator_id,
+    ac.post_id,
+    ac.parent_id,
+    ac.content,
+    ac.removed,
+    um.read,
+    ac.published,
+    ac.updated,
+    ac.deleted,
+    ac.community_id,
+    ac.banned,
+    ac.banned_from_community,
+    ac.creator_name,
+    ac.creator_avatar,
+    ac.score,
+    ac.upvotes,
+    ac.downvotes,
+    u.id as user_id,
+    coalesce(cl.score, 0) as my_vote,
+    (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved,
+    um.recipient_id
+from user_ u
+cross join all_comment ac
+left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
+left join user_mention um on um.comment_id = ac.id
+
+union all
+
+select 
+    ac.id,
+    um.id as user_mention_id,
+    ac.creator_id,
+    ac.post_id,
+    ac.parent_id,
+    ac.content,
+    ac.removed,
+    um.read,
+    ac.published,
+    ac.updated,
+    ac.deleted,
+    ac.community_id,
+    ac.banned,
+    ac.banned_from_community,
+    ac.creator_name,
+    ac.creator_avatar,
+    ac.score,
+    ac.upvotes,
+    ac.downvotes,
+    null as user_id, 
+    null as my_vote,
+    null as saved,
+    um.recipient_id
+from all_comment ac
+left join user_mention um on um.comment_id = ac.id
+;
+
index 9ba910362667ef0a1c5e4c4f0ad4c0c4da5aec23..994a9627f4f5f12bf37c7509512eb144b76ee9ff 100755 (executable)
@@ -17,6 +17,15 @@ psql -qAt -U lemmy -f explain.sql > community_view.json
 echo "explain (analyze, format json) select * from site_view limit 1" > explain.sql
 psql -qAt -U lemmy -f explain.sql > site_view.json
 
+echo "explain (analyze, format json) select * from reply_view where user_id = 34 and recipient_id = 34" > explain.sql
+psql -qAt -U lemmy -f explain.sql > reply_view.json
+
+echo "explain (analyze, format json) select * from user_mention_view where user_id = 34 and recipient_id = 34" > explain.sql
+psql -qAt -U lemmy -f explain.sql > user_mention_view.json
+
+echo "explain (analyze, format json) select * from user_mention_mview where user_id = 34 and recipient_id = 34" > explain.sql
+psql -qAt -U lemmy -f explain.sql > user_mention_mview.json
+
 grep "Execution Time" *.json
 
 rm explain.sql
index 7a45d22204be6bd6d6a03476c3d8d4d18e5b936d..1cf43984a07f01fff0d91ab56707f2ad9cfe18f6 100644 (file)
@@ -1,4 +1,3 @@
-use super::user_mention_view::user_mention_view::BoxedQuery;
 use super::*;
 use diesel::pg::Pg;
 
@@ -31,6 +30,34 @@ table! {
   }
 }
 
+table! {
+  user_mention_mview (id) {
+    id -> Int4,
+    user_mention_id -> Int4,
+    creator_id -> Int4,
+    post_id -> Int4,
+    parent_id -> Nullable<Int4>,
+    content -> Text,
+    removed -> Bool,
+    read -> Bool,
+    published -> Timestamp,
+    updated -> Nullable<Timestamp>,
+    deleted -> Bool,
+    community_id -> Int4,
+    banned -> Bool,
+    banned_from_community -> Bool,
+    creator_name -> Varchar,
+    creator_avatar -> Nullable<Text>,
+    score -> BigInt,
+    upvotes -> BigInt,
+    downvotes -> BigInt,
+    user_id -> Nullable<Int4>,
+    my_vote -> Nullable<Int4>,
+    saved -> Nullable<Bool>,
+    recipient_id -> Int4,
+  }
+}
+
 #[derive(
   Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
 )]
@@ -63,7 +90,7 @@ pub struct UserMentionView {
 
 pub struct UserMentionQueryBuilder<'a> {
   conn: &'a PgConnection,
-  query: BoxedQuery<'a, Pg>,
+  query: super::user_mention_view::user_mention_mview::BoxedQuery<'a, Pg>,
   for_user_id: i32,
   sort: &'a SortType,
   unread_only: bool,
@@ -73,9 +100,9 @@ pub struct UserMentionQueryBuilder<'a> {
 
 impl<'a> UserMentionQueryBuilder<'a> {
   pub fn create(conn: &'a PgConnection, for_user_id: i32) -> Self {
-    use super::user_mention_view::user_mention_view::dsl::*;
+    use super::user_mention_view::user_mention_mview::dsl::*;
 
-    let query = user_mention_view.into_boxed();
+    let query = user_mention_mview.into_boxed();
 
     UserMentionQueryBuilder {
       conn,
@@ -109,7 +136,7 @@ impl<'a> UserMentionQueryBuilder<'a> {
   }
 
   pub fn list(self) -> Result<Vec<UserMentionView>, Error> {
-    use super::user_mention_view::user_mention_view::dsl::*;
+    use super::user_mention_view::user_mention_mview::dsl::*;
 
     let mut query = self.query;