mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-29 15:51:20 +00:00
Optimize Community::set_featured_posts (#4579)
* Don't lock excess rows in Community::set_featured_posts * Update community.rs * Update community.rs * Update community.rs * Update community.rs
This commit is contained in:
parent
60f9a97dfa
commit
007e9b7aab
1 changed files with 11 additions and 24 deletions
|
@ -29,6 +29,7 @@ use diesel::{
|
||||||
select,
|
select,
|
||||||
sql_types,
|
sql_types,
|
||||||
update,
|
update,
|
||||||
|
BoolExpressionMethods,
|
||||||
ExpressionMethods,
|
ExpressionMethods,
|
||||||
NullableExpressionMethods,
|
NullableExpressionMethods,
|
||||||
QueryDsl,
|
QueryDsl,
|
||||||
|
@ -150,30 +151,16 @@ impl Community {
|
||||||
for p in &posts {
|
for p in &posts {
|
||||||
debug_assert!(p.community_id == community_id);
|
debug_assert!(p.community_id == community_id);
|
||||||
}
|
}
|
||||||
conn
|
// Mark the given posts as featured and all other posts as not featured.
|
||||||
.build_transaction()
|
let post_ids = posts.iter().map(|p| p.id);
|
||||||
.run(|conn| {
|
|
||||||
Box::pin(async move {
|
|
||||||
update(
|
|
||||||
// first remove all existing featured posts
|
|
||||||
post::table,
|
|
||||||
)
|
|
||||||
.filter(post::dsl::community_id.eq(community_id))
|
|
||||||
.set(post::dsl::featured_community.eq(false))
|
|
||||||
.execute(conn)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
// then mark the given posts as featured
|
|
||||||
let post_ids: Vec<_> = posts.iter().map(|p| p.id).collect();
|
|
||||||
update(post::table)
|
update(post::table)
|
||||||
.filter(post::dsl::id.eq_any(post_ids))
|
.filter(post::dsl::community_id.eq(community_id))
|
||||||
.set(post::dsl::featured_community.eq(true))
|
// This filter is just for performance
|
||||||
|
.filter(post::dsl::featured_community.or(post::dsl::id.eq_any(post_ids.clone())))
|
||||||
|
.set(post::dsl::featured_community.eq(post::dsl::id.eq_any(post_ids)))
|
||||||
.execute(conn)
|
.execute(conn)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}) as _
|
|
||||||
})
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue