Fixing slowness in saved post fetching. #4756

This commit is contained in:
Dessalines 2024-05-29 14:55:25 -04:00
parent 51970ffc81
commit 5a7a393901
3 changed files with 14 additions and 27 deletions

View file

@ -1,5 +1,4 @@
use crate::structs::{LocalUserView, PaginationCursor, PostView}; use crate::structs::{LocalUserView, PaginationCursor, PostView};
use chrono::{DateTime, Utc};
use diesel::{ use diesel::{
debug_query, debug_query,
dsl::{exists, not, IntervalDsl}, dsl::{exists, not, IntervalDsl},
@ -100,17 +99,6 @@ fn queries<'a>() -> Queries<
), ),
); );
let is_saved = |person_id| {
post_saved::table
.filter(
post_aggregates::post_id
.eq(post_saved::post_id)
.and(post_saved::person_id.eq(person_id)),
)
.select(post_saved::published.nullable())
.single_value()
};
let is_read = |person_id| { let is_read = |person_id| {
exists( exists(
post_read::table.filter( post_read::table.filter(
@ -162,14 +150,6 @@ fn queries<'a>() -> Queries<
Box::new(false.into_sql::<sql_types::Bool>()) Box::new(false.into_sql::<sql_types::Bool>())
}; };
let is_saved_selection: Box<
dyn BoxableExpression<_, Pg, SqlType = sql_types::Nullable<sql_types::Timestamptz>>,
> = if let Some(person_id) = my_person_id {
Box::new(is_saved(person_id))
} else {
Box::new(None::<DateTime<Utc>>.into_sql::<sql_types::Nullable<sql_types::Timestamptz>>())
};
let is_read_selection: Box<dyn BoxableExpression<_, Pg, SqlType = sql_types::Bool>> = let is_read_selection: Box<dyn BoxableExpression<_, Pg, SqlType = sql_types::Bool>> =
if let Some(person_id) = my_person_id { if let Some(person_id) = my_person_id {
Box::new(is_read(person_id)) Box::new(is_read(person_id))
@ -237,6 +217,13 @@ fn queries<'a>() -> Queries<
.inner_join(person::table) .inner_join(person::table)
.inner_join(community::table) .inner_join(community::table)
.inner_join(post::table) .inner_join(post::table)
.left_join(
post_saved::table.on(
post_aggregates::post_id
.eq(post_saved::post_id)
.and(post_saved::person_id.eq(my_person_id.unwrap_or(PersonId(-1)))),
),
)
.select(( .select((
post::all_columns, post::all_columns,
person::all_columns, person::all_columns,
@ -247,7 +234,7 @@ fn queries<'a>() -> Queries<
creator_is_admin, creator_is_admin,
post_aggregates::all_columns, post_aggregates::all_columns,
subscribed_type_selection, subscribed_type_selection,
is_saved_selection.is_not_null(), post_saved::person_id.nullable().is_not_null(),
is_read_selection, is_read_selection,
is_hidden_selection, is_hidden_selection,
is_creator_blocked_selection, is_creator_blocked_selection,
@ -426,10 +413,10 @@ fn queries<'a>() -> Queries<
}; };
// If its saved only, then filter, and order by the saved time, not the comment creation time. // If its saved only, then filter, and order by the saved time, not the comment creation time.
if let (true, Some(person_id)) = (options.saved_only, my_person_id) { if let (true, Some(_person_id)) = (options.saved_only, my_person_id) {
query = query query = query
.filter(is_saved(person_id).is_not_null()) .filter(post_saved::person_id.is_not_null())
.then_order_by(is_saved(person_id).desc()); .then_order_by(post_saved::published.desc());
} }
// Only hide the read posts, if the saved_only is false. Otherwise ppl with the hide_read // Only hide the read posts, if the saved_only is false. Otherwise ppl with the hide_read
// setting wont be able to see saved posts. // setting wont be able to see saved posts.

View file

@ -59,13 +59,13 @@ pub struct CancellableTask {
impl CancellableTask { impl CancellableTask {
/// spawn a task but with graceful shutdown /// spawn a task but with graceful shutdown
pub fn spawn<F, R: Debug>( pub fn spawn<F, R>(
timeout: Duration, timeout: Duration,
task: impl FnOnce(CancellationToken) -> F + Send + 'static, task: impl FnOnce(CancellationToken) -> F + Send + 'static,
) -> CancellableTask ) -> CancellableTask
where where
F: Future<Output = LemmyResult<R>> + Send + 'static, F: Future<Output = LemmyResult<R>> + Send + 'static,
R: Send + 'static, R: Send + Debug + 'static,
{ {
let stop = CancellationToken::new(); let stop = CancellationToken::new();
let stop2 = stop.clone(); let stop2 = stop.clone();

View file

@ -53,7 +53,7 @@ services:
lemmy-ui: lemmy-ui:
# use "image" to pull down an already compiled lemmy-ui. make sure to comment out "build". # use "image" to pull down an already compiled lemmy-ui. make sure to comment out "build".
image: dessalines/lemmy-ui:0.19.3 image: dessalines/lemmy-ui:0.19.4-rc.3
# platform: linux/x86_64 # no arm64 support. uncomment platform if using m1. # platform: linux/x86_64 # no arm64 support. uncomment platform if using m1.
# use "build" to build your local lemmy ui image for development. make sure to comment out "image". # use "build" to build your local lemmy ui image for development. make sure to comment out "image".
# run: docker compose up --build # run: docker compose up --build