Skip to content

Commit

Permalink
Addressing slow profile queries. #2777
Browse files Browse the repository at this point in the history
  • Loading branch information
dessalines authored and Nutomic committed Apr 25, 2023
1 parent 17527d0 commit 10cb992
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 141 deletions.
8 changes: 4 additions & 4 deletions crates/db_schema/src/impls/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
utils::{functions::lower, get_conn, DbPool},
SubscribedType,
};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;

#[async_trait]
Expand Down Expand Up @@ -298,7 +298,7 @@ impl ApubActor for Community {
let mut q = community
.into_boxed()
.filter(local.eq(true))
.filter(lower(name).eq(lower(community_name)));
.filter(lower(name).eq(community_name.to_lowercase()));
if !include_deleted {
q = q.filter(deleted.eq(false)).filter(removed.eq(false));
}
Expand All @@ -311,9 +311,9 @@ impl ApubActor for Community {
protocol_domain: &str,
) -> Result<Community, Error> {
let conn = &mut get_conn(pool).await?;
let from_actor_id = format!("{}/c/{}", protocol_domain, community_name).to_lowercase();
community
.filter(lower(name).eq(lower(community_name)))
.filter(actor_id.like(format!("{protocol_domain}%")))
.filter(lower(actor_id).eq(from_actor_id))
.first::<Self>(conn)
.await
}
Expand Down
15 changes: 4 additions & 11 deletions crates/db_schema/src/impls/person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,7 @@ use crate::{
traits::{ApubActor, Crud, Followable},
utils::{functions::lower, get_conn, naive_now, DbPool},
};
use diesel::{
dsl::insert_into,
result::Error,
ExpressionMethods,
JoinOnDsl,
QueryDsl,
TextExpressionMethods,
};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, JoinOnDsl, QueryDsl};
use diesel_async::RunQueryDsl;

#[async_trait]
Expand Down Expand Up @@ -134,7 +127,7 @@ impl ApubActor for Person {
let mut q = person
.into_boxed()
.filter(local.eq(true))
.filter(lower(name).eq(lower(from_name)));
.filter(lower(name).eq(from_name.to_lowercase()));
if !include_deleted {
q = q.filter(deleted.eq(false))
}
Expand All @@ -147,9 +140,9 @@ impl ApubActor for Person {
protocol_domain: &str,
) -> Result<Person, Error> {
let conn = &mut get_conn(pool).await?;
let from_actor_id = format!("{}/u/{}", protocol_domain, person_name).to_lowercase();
person
.filter(lower(name).eq(lower(person_name)))
.filter(actor_id.like(format!("{protocol_domain}%")))
.filter(lower(actor_id).eq(from_actor_id))
.first::<Self>(conn)
.await
}
Expand Down
7 changes: 1 addition & 6 deletions crates/db_views/src/comment_report_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,7 @@ impl CommentReportView {
community_person_ban::table.on(
community::id
.eq(community_person_ban::community_id)
.and(community_person_ban::person_id.eq(comment::creator_id))
.and(
community_person_ban::expires
.is_null()
.or(community_person_ban::expires.gt(now)),
),
.and(community_person_ban::person_id.eq(comment::creator_id)),
),
)
.left_join(
Expand Down
65 changes: 29 additions & 36 deletions crates/db_views/src/comment_view.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::structs::CommentView;
use diesel::{
dsl::now,
result::Error,
BoolExpressionMethods,
ExpressionMethods,
Expand Down Expand Up @@ -88,12 +87,7 @@ impl CommentView {
community_person_ban::table.on(
community::id
.eq(community_person_ban::community_id)
.and(community_person_ban::person_id.eq(comment::creator_id))
.and(
community_person_ban::expires
.is_null()
.or(community_person_ban::expires.gt(now)),
),
.and(community_person_ban::person_id.eq(comment::creator_id)),
),
)
.left_join(
Expand Down Expand Up @@ -199,12 +193,7 @@ impl<'a> CommentQuery<'a> {
community_person_ban::table.on(
community::id
.eq(community_person_ban::community_id)
.and(community_person_ban::person_id.eq(comment::creator_id))
.and(
community_person_ban::expires
.is_null()
.or(community_person_ban::expires.gt(now)),
),
.and(community_person_ban::person_id.eq(comment::creator_id)),
),
)
.left_join(
Expand Down Expand Up @@ -279,32 +268,34 @@ impl<'a> CommentQuery<'a> {
query = query.filter(comment::content.ilike(fuzzy_search(&search_term)));
};

if let Some(listing_type) = self.listing_type {
match listing_type {
ListingType::Subscribed => {
query = query.filter(community_follower::person_id.is_not_null())
} // TODO could be this: and(community_follower::person_id.eq(person_id_join)),
ListingType::Local => {
query = query.filter(community::local.eq(true)).filter(
community::hidden
.eq(false)
.or(community_follower::person_id.eq(person_id_join)),
)
}
ListingType::All => {
query = query.filter(
community::hidden
.eq(false)
.or(community_follower::person_id.eq(person_id_join)),
)
}
}
};

if let Some(community_id) = self.community_id {
query = query.filter(post::community_id.eq(community_id));
}

if self.post_id.is_none() && self.creator_id.is_none() && self.community_id.is_none() {
if let Some(listing_type) = self.listing_type {
match listing_type {
ListingType::Subscribed => {
query = query.filter(community_follower::person_id.is_not_null())
} // TODO could be this: and(community_follower::person_id.eq(person_id_join)),
ListingType::Local => {
query = query.filter(community::local.eq(true)).filter(
community::hidden
.eq(false)
.or(community_follower::person_id.eq(person_id_join)),
)
}
ListingType::All => {
query = query.filter(
community::hidden
.eq(false)
.or(community_follower::person_id.eq(person_id_join)),
)
}
}
};
}

if self.saved_only.unwrap_or(false) {
query = query.filter(comment_saved::comment_id.is_not_null());
}
Expand Down Expand Up @@ -354,7 +345,7 @@ impl<'a> CommentQuery<'a> {
limit_and_offset_unlimited(self.page, self.limit)
};

query = match self.sort.unwrap_or(CommentSortType::Hot) {
query = match self.sort.unwrap_or(CommentSortType::New) {
CommentSortType::Hot => query
.then_order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc())
.then_order_by(comment_aggregates::published.desc()),
Expand Down Expand Up @@ -610,6 +601,7 @@ mod tests {

let read_comment_views_no_person = CommentQuery::builder()
.pool(pool)
.sort(Some(CommentSortType::Hot))
.post_id(Some(data.inserted_post.id))
.build()
.list()
Expand All @@ -623,6 +615,7 @@ mod tests {

let read_comment_views_with_person = CommentQuery::builder()
.pool(pool)
.sort(Some(CommentSortType::Hot))
.post_id(Some(data.inserted_post.id))
.local_user(Some(&data.inserted_local_user))
.build()
Expand Down
3 changes: 1 addition & 2 deletions crates/db_views/src/local_user_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ impl LocalUserView {
})
}

// TODO check where this is used
pub async fn read_from_name(pool: &DbPool, name: &str) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
let (local_user, person, counts) = local_user::table
.filter(person::name.eq(name))
.filter(lower(person::name).eq(name.to_lowercase()))
.inner_join(person::table)
.inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id)))
.select((
Expand Down
15 changes: 2 additions & 13 deletions crates/db_views/src/post_report_view.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::structs::PostReportView;
use diesel::{
dsl::now,
result::Error,
BoolExpressionMethods,
ExpressionMethods,
Expand Down Expand Up @@ -77,12 +76,7 @@ impl PostReportView {
community_person_ban::table.on(
post::community_id
.eq(community_person_ban::community_id)
.and(community_person_ban::person_id.eq(post::creator_id))
.and(
community_person_ban::expires
.is_null()
.or(community_person_ban::expires.gt(now)),
),
.and(community_person_ban::person_id.eq(post::creator_id)),
),
)
.left_join(
Expand Down Expand Up @@ -194,12 +188,7 @@ impl<'a> PostReportQuery<'a> {
community_person_ban::table.on(
post::community_id
.eq(community_person_ban::community_id)
.and(community_person_ban::person_id.eq(post::creator_id))
.and(
community_person_ban::expires
.is_null()
.or(community_person_ban::expires.gt(now)),
),
.and(community_person_ban::person_id.eq(post::creator_id)),
),
)
.left_join(
Expand Down
70 changes: 32 additions & 38 deletions crates/db_views/src/post_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,7 @@ impl PostView {
community_person_ban::table.on(
post::community_id
.eq(community_person_ban::community_id)
.and(community_person_ban::person_id.eq(post::creator_id))
.and(
community_person_ban::expires
.is_null()
.or(community_person_ban::expires.gt(now)),
),
.and(community_person_ban::person_id.eq(post::creator_id)),
),
)
.inner_join(post_aggregates::table)
Expand Down Expand Up @@ -230,12 +225,7 @@ impl<'a> PostQuery<'a> {
community_person_ban::table.on(
post::community_id
.eq(community_person_ban::community_id)
.and(community_person_ban::person_id.eq(post::creator_id))
.and(
community_person_ban::expires
.is_null()
.or(community_person_ban::expires.gt(now)),
),
.and(community_person_ban::person_id.eq(post::creator_id)),
),
)
.inner_join(post_aggregates::table)
Expand Down Expand Up @@ -269,7 +259,7 @@ impl<'a> PostQuery<'a> {
)
.left_join(
community_block::table.on(
community::id
post::community_id
.eq(community_block::community_id)
.and(community_block::person_id.eq(person_id_join)),
),
Expand Down Expand Up @@ -321,27 +311,6 @@ impl<'a> PostQuery<'a> {
.filter(community::deleted.eq(false));
}

if let Some(listing_type) = self.listing_type {
match listing_type {
ListingType::Subscribed => {
query = query.filter(community_follower::person_id.is_not_null())
}
ListingType::Local => {
query = query.filter(community::local.eq(true)).filter(
community::hidden
.eq(false)
.or(community_follower::person_id.eq(person_id_join)),
);
}
ListingType::All => {
query = query.filter(
community::hidden
.eq(false)
.or(community_follower::person_id.eq(person_id_join)),
)
}
}
}
if self.community_id.is_none() {
query = query.then_order_by(post_aggregates::featured_local.desc());
} else if let Some(community_id) = self.community_id {
Expand All @@ -350,6 +319,35 @@ impl<'a> PostQuery<'a> {
.then_order_by(post_aggregates::featured_community.desc());
}

if let Some(creator_id) = self.creator_id {
query = query.filter(post::creator_id.eq(creator_id));
}

if self.community_id.is_none() && self.creator_id.is_none() {
// TODO you don't need to do this if you're given a community
if let Some(listing_type) = self.listing_type {
match listing_type {
ListingType::Subscribed => {
query = query.filter(community_follower::person_id.is_not_null())
}
ListingType::Local => {
query = query.filter(community::local.eq(true)).filter(
community::hidden
.eq(false)
.or(community_follower::person_id.eq(person_id_join)),
);
}
ListingType::All => {
query = query.filter(
community::hidden
.eq(false)
.or(community_follower::person_id.eq(person_id_join)),
)
}
}
}
}

if let Some(url_search) = self.url_search {
query = query.filter(post::url.eq(url_search));
}
Expand All @@ -363,10 +361,6 @@ impl<'a> PostQuery<'a> {
);
}

if let Some(creator_id) = self.creator_id {
query = query.filter(post::creator_id.eq(creator_id));
}

if !self.local_user.map(|l| l.show_nsfw).unwrap_or(false) {
query = query
.filter(post::nsfw.eq(false))
Expand Down
Loading

0 comments on commit 10cb992

Please sign in to comment.