perf: precompute mangas.sort_author for the author sort
?sort=author used a correlated min(lower(a.name)) subquery as the ORDER BY key, evaluated per filter-matching row before LIMIT. Materialize it into mangas.sort_author (indexed by mangas_sort_author_idx), maintained by triggers on manga_authors and backfilled in the migration, so the sort is a plain indexed column read. Author names are immutable, so only join changes matter. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2
backend/Cargo.lock
generated
2
backend/Cargo.lock
generated
@@ -1558,7 +1558,7 @@ checksum = "c41e0c4fef86961ac6d6f8a82609f55f31b05e4fce149ac5710e439df7619ba4"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mangalord"
|
name = "mangalord"
|
||||||
version = "0.124.23"
|
version = "0.124.24"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"argon2",
|
"argon2",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mangalord"
|
name = "mangalord"
|
||||||
version = "0.124.23"
|
version = "0.124.24"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
default-run = "mangalord"
|
default-run = "mangalord"
|
||||||
|
|
||||||
|
|||||||
51
backend/migrations/0037_mangas_sort_author.sql
Normal file
51
backend/migrations/0037_mangas_sort_author.sql
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
-- Precomputed author sort key for `?sort=author`.
|
||||||
|
--
|
||||||
|
-- The author sort used a correlated `min(lower(a.name))` subquery as the ORDER
|
||||||
|
-- BY key, evaluated per filter-matching row before LIMIT — it scaled worse than
|
||||||
|
-- the indexed date/title sorts. Materialize the same value on `mangas` so the
|
||||||
|
-- sort is a plain indexed column read.
|
||||||
|
--
|
||||||
|
-- `sort_author` = the alphabetically-first attached author's lowercased name,
|
||||||
|
-- or NULL when the manga has no authors (kept last via NULLS LAST in the query).
|
||||||
|
-- Author names are immutable (authors are upserted by unique lowercased name and
|
||||||
|
-- never renamed), so the value only changes when the manga_authors join changes
|
||||||
|
-- — maintained by the triggers below.
|
||||||
|
|
||||||
|
ALTER TABLE mangas ADD COLUMN sort_author text;
|
||||||
|
|
||||||
|
CREATE INDEX mangas_sort_author_idx ON mangas (sort_author, id);
|
||||||
|
|
||||||
|
-- Backfill from existing links.
|
||||||
|
UPDATE mangas m
|
||||||
|
SET sort_author = (
|
||||||
|
SELECT min(lower(a.name))
|
||||||
|
FROM manga_authors ma
|
||||||
|
JOIN authors a ON a.id = ma.author_id
|
||||||
|
WHERE ma.manga_id = m.id
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE OR REPLACE FUNCTION refresh_manga_sort_author(mid uuid) RETURNS void AS $$
|
||||||
|
BEGIN
|
||||||
|
UPDATE mangas
|
||||||
|
SET sort_author = (
|
||||||
|
SELECT min(lower(a.name))
|
||||||
|
FROM manga_authors ma
|
||||||
|
JOIN authors a ON a.id = ma.author_id
|
||||||
|
WHERE ma.manga_id = mid
|
||||||
|
)
|
||||||
|
WHERE id = mid;
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
|
CREATE OR REPLACE FUNCTION mangas_sort_author_on_ma_change() RETURNS trigger AS $$
|
||||||
|
BEGIN
|
||||||
|
-- On a manga cascade-delete the UPDATE simply no-ops (row already gone).
|
||||||
|
PERFORM refresh_manga_sort_author(COALESCE(NEW.manga_id, OLD.manga_id));
|
||||||
|
RETURN NULL;
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
|
CREATE TRIGGER manga_authors_sort_author_ins AFTER INSERT ON manga_authors
|
||||||
|
FOR EACH ROW EXECUTE FUNCTION mangas_sort_author_on_ma_change();
|
||||||
|
CREATE TRIGGER manga_authors_sort_author_del AFTER DELETE ON manga_authors
|
||||||
|
FOR EACH ROW EXECUTE FUNCTION mangas_sort_author_on_ma_change();
|
||||||
@@ -168,16 +168,11 @@ pub async fn list(pool: &PgPool, query: &ListQuery) -> AppResult<(Vec<Manga>, Op
|
|||||||
SortField::Created => "created_at",
|
SortField::Created => "created_at",
|
||||||
SortField::Updated => "updated_at",
|
SortField::Updated => "updated_at",
|
||||||
SortField::Title => "lower(title)",
|
SortField::Title => "lower(title)",
|
||||||
// Sorts on the alphabetically-first attached author. As an ORDER BY
|
// Sorts on the alphabetically-first attached author. Precomputed into
|
||||||
// key this correlated subquery is evaluated per filter-matching row
|
// `mangas.sort_author` (migration 0037, maintained by triggers on
|
||||||
// before LIMIT applies, so it scales worse than the indexed date/title
|
// manga_authors) and index-backed by `mangas_sort_author_idx`, so this
|
||||||
// sorts — revisit with a LATERAL join or precomputed sort-name column
|
// is a plain column read rather than a per-row correlated subquery.
|
||||||
// if the library grows large.
|
SortField::Author => "sort_author",
|
||||||
SortField::Author => {
|
|
||||||
"(SELECT min(lower(a.name)) \
|
|
||||||
FROM manga_authors ma JOIN authors a ON a.id = ma.author_id \
|
|
||||||
WHERE ma.manga_id = mangas.id)"
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
let dir = match query.order {
|
let dir = match query.order {
|
||||||
SortOrder::Asc => "ASC",
|
SortOrder::Asc => "ASC",
|
||||||
|
|||||||
@@ -326,6 +326,54 @@ async fn patch_updates_status_authors_and_genres(pool: PgPool) {
|
|||||||
assert_eq!(body["genres"][0]["name"], "Drama");
|
assert_eq!(body["genres"][0]["name"], "Drama");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[sqlx::test(migrations = "./migrations")]
|
||||||
|
async fn patch_authors_updates_precomputed_sort_author(pool: PgPool) {
|
||||||
|
// The precomputed mangas.sort_author (used by ?sort=author) must track
|
||||||
|
// author edits: changing the alphabetically-first author reorders the
|
||||||
|
// author sort. Two mangas whose relative author order flips after a PATCH.
|
||||||
|
let h = common::harness(pool.clone());
|
||||||
|
let (_, cookie) = common::register_user(&h.app).await;
|
||||||
|
|
||||||
|
let a = id_of(&create_manga(&h.app, &cookie, json!({ "title": "A", "authors": ["Zeta"] })).await);
|
||||||
|
let _b = id_of(&create_manga(&h.app, &cookie, json!({ "title": "B", "authors": ["Mid"] })).await);
|
||||||
|
|
||||||
|
// Initially: Mid < Zeta, so B before A.
|
||||||
|
let ids = list_titles(&h.app, "sort=author&order=asc").await;
|
||||||
|
assert_eq!(ids, vec!["B", "A"]);
|
||||||
|
|
||||||
|
// Re-author A to "Alpha", which now sorts first.
|
||||||
|
let resp = h
|
||||||
|
.app
|
||||||
|
.clone()
|
||||||
|
.oneshot(common::patch_json_with_cookie(
|
||||||
|
&format!("/api/v1/mangas/{a}"),
|
||||||
|
json!({ "authors": ["Alpha"] }),
|
||||||
|
&cookie,
|
||||||
|
))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
// Now A (Alpha) sorts before B (Mid) — proving sort_author was recomputed.
|
||||||
|
let ids = list_titles(&h.app, "sort=author&order=asc").await;
|
||||||
|
assert_eq!(ids, vec!["A", "B"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn list_titles(app: &axum::Router, query: &str) -> Vec<String> {
|
||||||
|
let resp = app
|
||||||
|
.clone()
|
||||||
|
.oneshot(common::get(&format!("/api/v1/mangas?{query}")))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let body = common::body_json(resp).await;
|
||||||
|
body["items"]
|
||||||
|
.as_array()
|
||||||
|
.unwrap()
|
||||||
|
.iter()
|
||||||
|
.map(|m| m["title"].as_str().unwrap().to_string())
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
#[sqlx::test(migrations = "./migrations")]
|
#[sqlx::test(migrations = "./migrations")]
|
||||||
async fn patch_404_on_unknown_id(pool: PgPool) {
|
async fn patch_404_on_unknown_id(pool: PgPool) {
|
||||||
let h = common::harness(pool);
|
let h = common::harness(pool);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "mangalord-frontend",
|
"name": "mangalord-frontend",
|
||||||
"version": "0.124.23",
|
"version": "0.124.24",
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
Reference in New Issue
Block a user