Skip to content

Commit

Permalink
Addressing slow profile queries. #2777 (#2830)
Browse files Browse the repository at this point in the history
* Addressing slow profile queries. #2777

* Addressing PR comments.
  • Loading branch information
dessalines authored Apr 25, 2023
1 parent 17527d0 commit 1b5437c
Show file tree
Hide file tree
Showing 16 changed files with 175 additions and 178 deletions.
4 changes: 1 addition & 3 deletions crates/apub/src/fetcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ where
.splitn(2, '@')
.collect_tuple()
.expect("invalid query");
let name = name.to_string();
let domain = format!("{}://{}", context.settings().get_protocol_string(), domain);
let actor = DbActor::read_from_name_and_domain(context.pool(), &name, &domain).await;
let actor = DbActor::read_from_name_and_domain(context.pool(), name, domain).await;
if actor.is_ok() {
Ok(actor?.into())
} else if local_user_view.is_some() {
Expand Down
45 changes: 26 additions & 19 deletions crates/db_schema/src/impls/community.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
newtypes::{CommunityId, DbUrl, PersonId},
schema::community::dsl::{actor_id, community, deleted, local, name, removed},
schema::{community, instance},
source::{
actor_language::CommunityLanguage,
community::{
Expand All @@ -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 All @@ -29,12 +29,15 @@ impl Crud for Community {
type IdType = CommunityId;
async fn read(pool: &DbPool, community_id: CommunityId) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
community.find(community_id).first::<Self>(conn).await
community::table
.find(community_id)
.first::<Self>(conn)
.await
}

async fn delete(pool: &DbPool, community_id: CommunityId) -> Result<usize, Error> {
let conn = &mut get_conn(pool).await?;
diesel::delete(community.find(community_id))
diesel::delete(community::table.find(community_id))
.execute(conn)
.await
}
Expand All @@ -47,9 +50,9 @@ impl Crud for Community {
};

// Can't do separate insert/update commands because InsertForm/UpdateForm aren't convertible
let community_ = insert_into(community)
let community_ = insert_into(community::table)
.values(form)
.on_conflict(actor_id)
.on_conflict(community::actor_id)
.do_update()
.set(form)
.get_result::<Self>(conn)
Expand All @@ -69,7 +72,7 @@ impl Crud for Community {
form: &Self::UpdateForm,
) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
diesel::update(community.find(community_id))
diesel::update(community::table.find(community_id))
.set(form)
.get_result::<Self>(conn)
.await
Expand Down Expand Up @@ -121,14 +124,14 @@ impl Community {
use crate::schema::community::dsl::{featured_url, moderators_url};
use CollectionType::*;
let conn = &mut get_conn(pool).await?;
let res = community
let res = community::table
.filter(moderators_url.eq(url))
.first::<Self>(conn)
.await;
if let Ok(c) = res {
return Ok((c, Moderators));
}
let res = community
let res = community::table
.filter(featured_url.eq(url))
.first::<Self>(conn)
.await;
Expand Down Expand Up @@ -280,8 +283,8 @@ impl ApubActor for Community {
async fn read_from_apub_id(pool: &DbPool, object_id: &DbUrl) -> Result<Option<Self>, Error> {
let conn = &mut get_conn(pool).await?;
Ok(
community
.filter(actor_id.eq(object_id))
community::table
.filter(community::actor_id.eq(object_id))
.first::<Community>(conn)
.await
.ok()
Expand All @@ -295,25 +298,29 @@ impl ApubActor for Community {
include_deleted: bool,
) -> Result<Community, Error> {
let conn = &mut get_conn(pool).await?;
let mut q = community
let mut q = community::table
.into_boxed()
.filter(local.eq(true))
.filter(lower(name).eq(lower(community_name)));
.filter(community::local.eq(true))
.filter(lower(community::name).eq(community_name.to_lowercase()));
if !include_deleted {
q = q.filter(deleted.eq(false)).filter(removed.eq(false));
q = q
.filter(community::deleted.eq(false))
.filter(community::removed.eq(false));
}
q.first::<Self>(conn).await
}

async fn read_from_name_and_domain(
pool: &DbPool,
community_name: &str,
protocol_domain: &str,
for_domain: &str,
) -> Result<Community, Error> {
let conn = &mut get_conn(pool).await?;
community
.filter(lower(name).eq(lower(community_name)))
.filter(actor_id.like(format!("{protocol_domain}%")))
community::table
.inner_join(instance::table)
.filter(lower(community::name).eq(community_name.to_lowercase()))
.filter(instance::domain.eq(for_domain))
.select(community::all_columns)
.first::<Self>(conn)
.await
}
Expand Down
87 changes: 37 additions & 50 deletions crates/db_schema/src/impls/person.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
use crate::{
newtypes::{CommunityId, DbUrl, PersonId},
schema::person::dsl::{
actor_id,
avatar,
banner,
bio,
deleted,
display_name,
local,
matrix_user_id,
name,
person,
updated,
},
schema::{instance, local_user, person, person_follower},
source::person::{
Person,
PersonFollower,
Expand All @@ -23,14 +11,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 All @@ -40,21 +21,23 @@ impl Crud for Person {
type IdType = PersonId;
async fn read(pool: &DbPool, person_id: PersonId) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
person
.filter(deleted.eq(false))
person::table
.filter(person::deleted.eq(false))
.find(person_id)
.first::<Self>(conn)
.await
}
async fn delete(pool: &DbPool, person_id: PersonId) -> Result<usize, Error> {
let conn = &mut get_conn(pool).await?;
diesel::delete(person.find(person_id)).execute(conn).await
diesel::delete(person::table.find(person_id))
.execute(conn)
.await
}
async fn create(pool: &DbPool, form: &PersonInsertForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
insert_into(person)
insert_into(person::table)
.values(form)
.on_conflict(actor_id)
.on_conflict(person::actor_id)
.do_update()
.set(form)
.get_result::<Self>(conn)
Expand All @@ -66,7 +49,7 @@ impl Crud for Person {
form: &PersonUpdateForm,
) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
diesel::update(person.find(person_id))
diesel::update(person::table.find(person_id))
.set(form)
.get_result::<Self>(conn)
.await
Expand All @@ -75,7 +58,6 @@ impl Crud for Person {

impl Person {
pub async fn delete_account(pool: &DbPool, person_id: PersonId) -> Result<Person, Error> {
use crate::schema::local_user;
let conn = &mut get_conn(pool).await?;

// Set the local user info to none
Expand All @@ -87,15 +69,15 @@ impl Person {
.execute(conn)
.await?;

diesel::update(person.find(person_id))
diesel::update(person::table.find(person_id))
.set((
display_name.eq::<Option<String>>(None),
avatar.eq::<Option<String>>(None),
banner.eq::<Option<String>>(None),
bio.eq::<Option<String>>(None),
matrix_user_id.eq::<Option<String>>(None),
deleted.eq(true),
updated.eq(naive_now()),
person::display_name.eq::<Option<String>>(None),
person::avatar.eq::<Option<String>>(None),
person::banner.eq::<Option<String>>(None),
person::bio.eq::<Option<String>>(None),
person::matrix_user_id.eq::<Option<String>>(None),
person::deleted.eq(true),
person::updated.eq(naive_now()),
))
.get_result::<Self>(conn)
.await
Expand All @@ -115,9 +97,9 @@ impl ApubActor for Person {
async fn read_from_apub_id(pool: &DbPool, object_id: &DbUrl) -> Result<Option<Self>, Error> {
let conn = &mut get_conn(pool).await?;
Ok(
person
.filter(deleted.eq(false))
.filter(actor_id.eq(object_id))
person::table
.filter(person::deleted.eq(false))
.filter(person::actor_id.eq(object_id))
.first::<Person>(conn)
.await
.ok()
Expand All @@ -131,25 +113,28 @@ impl ApubActor for Person {
include_deleted: bool,
) -> Result<Person, Error> {
let conn = &mut get_conn(pool).await?;
let mut q = person
let mut q = person::table
.into_boxed()
.filter(local.eq(true))
.filter(lower(name).eq(lower(from_name)));
.filter(person::local.eq(true))
.filter(lower(person::name).eq(from_name.to_lowercase()));
if !include_deleted {
q = q.filter(deleted.eq(false))
q = q.filter(person::deleted.eq(false))
}
q.first::<Self>(conn).await
}

async fn read_from_name_and_domain(
pool: &DbPool,
person_name: &str,
protocol_domain: &str,
for_domain: &str,
) -> Result<Person, Error> {
let conn = &mut get_conn(pool).await?;
person
.filter(lower(name).eq(lower(person_name)))
.filter(actor_id.like(format!("{protocol_domain}%")))

person::table
.inner_join(instance::table)
.filter(lower(person::name).eq(person_name.to_lowercase()))
.filter(instance::domain.eq(for_domain))
.select(person::all_columns)
.first::<Self>(conn)
.await
}
Expand Down Expand Up @@ -186,12 +171,14 @@ impl Followable for PersonFollower {
}

impl PersonFollower {
pub async fn list_followers(pool: &DbPool, person_id_: PersonId) -> Result<Vec<Person>, Error> {
use crate::schema::{person, person_follower, person_follower::person_id};
pub async fn list_followers(
pool: &DbPool,
for_person_id: PersonId,
) -> Result<Vec<Person>, Error> {
let conn = &mut get_conn(pool).await?;
person_follower::table
.inner_join(person::table.on(person_follower::follower_id.eq(person::id)))
.filter(person_id.eq(person_id_))
.filter(person_follower::person_id.eq(for_person_id))
.select(person::all_columns)
.load(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
25 changes: 8 additions & 17 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,6 +268,10 @@ impl<'a> CommentQuery<'a> {
query = query.filter(comment::content.ilike(fuzzy_search(&search_term)));
};

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

if let Some(listing_type) = self.listing_type {
match listing_type {
ListingType::Subscribed => {
Expand All @@ -299,10 +292,6 @@ impl<'a> CommentQuery<'a> {
)
}
}
};

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

if self.saved_only.unwrap_or(false) {
Expand Down Expand Up @@ -610,6 +599,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 +613,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
Loading

0 comments on commit 1b5437c

Please sign in to comment.