See if different SQL query fixes performance regression (#4246)

* See if different SQL query fixes performance regression

* Fix formatting

* Hopefully fix failing federation test

* Hopefully solve federation test problem

* Use already-existing coalesce function

* Update person_view.rs

---------

Co-authored-by: SleeplessOne1917 <insomnia-void@protonmail.com>
Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
Co-authored-by: Nutomic <me@nutomic.com>
Co-authored-by: Dessalines <tyhou13@gmx.com>
This commit is contained in:
SleeplessOne1917 2023-12-12 12:09:52 -05:00 committed by GitHub
parent 30d58865b8
commit 8d52c7e7c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 12 deletions

View file

@ -1,6 +1,5 @@
use crate::structs::PersonView;
use diesel::{
dsl::exists,
pg::Pg,
result::Error,
BoolExpressionMethods,
@ -13,7 +12,17 @@ use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::PersonId,
schema::{local_user, person, person_aggregates},
utils::{fuzzy_search, limit_and_offset, now, DbConn, DbPool, ListFn, Queries, ReadFn},
utils::{
functions::coalesce,
fuzzy_search,
limit_and_offset,
now,
DbConn,
DbPool,
ListFn,
Queries,
ReadFn,
},
SortType,
};
use serde::{Deserialize, Serialize};
@ -48,21 +57,15 @@ fn post_to_person_sort_type(sort: SortType) -> PersonSortType {
fn queries<'a>(
) -> Queries<impl ReadFn<'a, PersonView, PersonId>, impl ListFn<'a, PersonView, ListMode>> {
let creator_is_admin = exists(
local_user::table.filter(
person::id
.eq(local_user::person_id)
.and(local_user::admin.eq(true)),
),
);
let all_joins = move |query: person::BoxedQuery<'a, Pg>| {
query
.inner_join(person_aggregates::table)
.left_join(local_user::table)
.filter(person::deleted.eq(false))
.select((
person::all_columns,
person_aggregates::all_columns,
creator_is_admin,
coalesce(local_user::admin.nullable(), false),
))
};
@ -77,7 +80,7 @@ fn queries<'a>(
match mode {
ListMode::Admins => {
query = query
.filter(creator_is_admin.eq(true))
.filter(local_user::admin.eq(true))
.filter(person::deleted.eq(false))
.order_by(person::published);
}

@ -1 +1 @@
Subproject commit b3343aef72e5a7e5df34cf328b910ed798027270
Subproject commit 7a38baa7341cfa9299df5a80ebe42fe298380e40