Adding a materialized view for reply_view. Fixes #466
This commit is contained in:
parent
930cad41b4
commit
4778f198e1
6 changed files with 161 additions and 5 deletions
25
server/migrations/2020-01-29-011901_create_reply_materialized_view/down.sql
vendored
Normal file
25
server/migrations/2020-01-29-011901_create_reply_materialized_view/down.sql
vendored
Normal file
|
@ -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
|
||||||
|
;
|
27
server/migrations/2020-01-29-011901_create_reply_materialized_view/up.sql
vendored
Normal file
27
server/migrations/2020-01-29-011901_create_reply_materialized_view/up.sql
vendored
Normal file
|
@ -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
|
||||||
|
;
|
1
server/migrations/2020-01-29-030825_create_user_mention_materialized_view/down.sql
vendored
Normal file
1
server/migrations/2020-01-29-030825_create_user_mention_materialized_view/down.sql
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
drop view user_mention_mview;
|
67
server/migrations/2020-01-29-030825_create_user_mention_materialized_view/up.sql
vendored
Normal file
67
server/migrations/2020-01-29-030825_create_user_mention_materialized_view/up.sql
vendored
Normal file
|
@ -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
|
||||||
|
;
|
||||||
|
|
|
@ -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
|
echo "explain (analyze, format json) select * from site_view limit 1" > explain.sql
|
||||||
psql -qAt -U lemmy -f explain.sql > site_view.json
|
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
|
grep "Execution Time" *.json
|
||||||
|
|
||||||
rm explain.sql
|
rm explain.sql
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use super::user_mention_view::user_mention_view::BoxedQuery;
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use diesel::pg::Pg;
|
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(
|
#[derive(
|
||||||
Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
|
Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
|
||||||
)]
|
)]
|
||||||
|
@ -63,7 +90,7 @@ pub struct UserMentionView {
|
||||||
|
|
||||||
pub struct UserMentionQueryBuilder<'a> {
|
pub struct UserMentionQueryBuilder<'a> {
|
||||||
conn: &'a PgConnection,
|
conn: &'a PgConnection,
|
||||||
query: BoxedQuery<'a, Pg>,
|
query: super::user_mention_view::user_mention_mview::BoxedQuery<'a, Pg>,
|
||||||
for_user_id: i32,
|
for_user_id: i32,
|
||||||
sort: &'a SortType,
|
sort: &'a SortType,
|
||||||
unread_only: bool,
|
unread_only: bool,
|
||||||
|
@ -73,9 +100,9 @@ pub struct UserMentionQueryBuilder<'a> {
|
||||||
|
|
||||||
impl<'a> UserMentionQueryBuilder<'a> {
|
impl<'a> UserMentionQueryBuilder<'a> {
|
||||||
pub fn create(conn: &'a PgConnection, for_user_id: i32) -> Self {
|
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 {
|
UserMentionQueryBuilder {
|
||||||
conn,
|
conn,
|
||||||
|
@ -109,7 +136,7 @@ impl<'a> UserMentionQueryBuilder<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn list(self) -> Result<Vec<UserMentionView>, Error> {
|
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;
|
let mut query = self.query;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue