Skip to content

Improve performance on shelf views - #4059

Merged
ilkka-ollakka merged 4 commits into
mainfrom
shelf-sort-query
Jul 21, 2026
Merged

ilkka-ollakka merged 4 commits into
mainfrom
shelf-sort-query

Conversation

@mouse-reeve

Copy link
Copy Markdown
Member

Description

Only annotates the querysets with the field that is needed for sorting. Example result when looking at all books sorted by title (which doesn't require any annotation -- rating, for example, is a little bit less stark of a difference). Before:

SELECT DISTINCT "bookwyrm_book"."id", "bookwyrm_book"."created_date", "bookwyrm_book"."updated_date", "bookwyrm_book"."remote_id", "bookwyrm_book"."origin_id", "bookwyrm_book"."openlibrary_key", "bookwyrm_book"."finna_key", "bookwyrm_book"."libris_key", "bookwyrm_book"."inventaire_id", "bookwyrm_book"."librarything_key", "bookwyrm_book"."goodreads_key", "bookwyrm_book"."bnf_id", "bookwyrm_book"."viaf", "bookwyrm_book"."wikidata", "bookwyrm_book"."asin", "bookwyrm_book"."aasin", "bookwyrm_book"."isfdb", "bookwyrm_book"."search_vector", "bookwyrm_book"."last_edited_by_id", "bookwyrm_book"."connector_id", "bookwyrm_book"."title", "bookwyrm_book"."sort_title", "bookwyrm_book"."subtitle", "bookwyrm_book"."description", "bookwyrm_book"."languages", "bookwyrm_book"."series", "bookwyrm_book"."series_number", "bookwyrm_book"."subjects", "bookwyrm_book"."subject_places", "bookwyrm_book"."cover", "bookwyrm_book"."preview_image", "bookwyrm_book"."first_published_date", "bookwyrm_book"."published_date", "bookwyrm_book"."first_published_date_precision", "bookwyrm_book"."published_date_precision", "bookwyrm_edition"."book_ptr_id", "bookwyrm_edition"."isbn_10", "bookwyrm_edition"."isbn_13", "bookwyrm_edition"."oclc_number", "bookwyrm_edition"."pages", "bookwyrm_edition"."physical_format", "bookwyrm_edition"."physical_format_detail", "bookwyrm_edition"."publishers", "bookwyrm_edition"."parent_work_id", "bookwyrm_edition"."edition_rank", MAX("bookwyrm_shelfbook"."shelved_date") AS "shelved_date", ( SELECT U0."rating" AS "rating" FROM "bookwyrm_review" U0 INNER JOIN "bookwyrm_status" U3 ON (U0."status_ptr_id" = U3."id") WHERE (U0."book_id" = ("bookwyrm_edition"."book_ptr_id") AND NOT U3."deleted" AND U0."rating" IS NOT NULL AND U3."user_id" = 1) ORDER BY U3."published_date" DESC LIMIT 1 ) AS "rating", ( SELECT U0."start_date" AS "start_date" FROM "bookwyrm_readthrough" U0 WHERE (U0."book_id" = ("bookwyrm_edition"."book_ptr_id") AND U0."user_id" = 1) ORDER BY 1 ASC LIMIT 1 ) AS "start_date", ( SELECT U0."finish_date" AS "finish_date" FROM "bookwyrm_readthrough" U0 WHERE (U0."book_id" = ("bookwyrm_edition"."book_ptr_id") AND U0."user_id" = 1) ORDER BY U0."start_date" ASC LIMIT 1 ) AS "finish_date", ( SELECT U2."name" AS "authors__name" FROM "bookwyrm_book" U0 LEFT OUTER JOIN "bookwyrm_book_authors" U1 ON (U0."id" = U1."book_id") LEFT OUTER JOIN "bookwyrm_author" U2 ON (U1."author_id" = U2."id") WHERE U0."id" = ("bookwyrm_edition"."book_ptr_id") LIMIT 1 ) AS "author" FROM "bookwyrm_edition" INNER JOIN "bookwyrm_shelfbook" ON ("bookwyrm_edition"."book_ptr_id" = "bookwyrm_shelfbook"."book_id") INNER JOIN "bookwyrm_book" ON ("bookwyrm_edition"."book_ptr_id" = "bookwyrm_book"."id") WHERE (NOT ("bookwyrm_edition"."parent_work_id" IN (SELECT U0."book_ptr_id" AS "id" FROM "bookwyrm_work" U0 INNER JOIN "bookwyrm_user_blocked_books" U1 ON (U0."book_ptr_id" = U1."work_id") WHERE U1."user_id" = 1) AND "bookwyrm_edition"."parent_work_id" IS NOT NULL) AND "bookwyrm_shelfbook"."shelf_id" IN (SELECT U0."id" FROM "bookwyrm_shelf" U0 WHERE U0."user_id" = 1)) GROUP BY "bookwyrm_book"."id", "bookwyrm_edition"."book_ptr_id" ORDER BY "bookwyrm_book"."sort_title" ASC LIMIT 15;

 Limit  (cost=10693.42..10695.30 rows=15 width=3317) (actual time=5635.667..5635.696 rows=15 loops=1)
   ->  Unique  (cost=10693.42..10695.80 rows=19 width=3317) (actual time=5635.666..5635.693 rows=15 loops=1)
 Planning Time: 2.317 ms
 Execution Time: 5635.843 ms

And after:

SELECT DISTINCT "bookwyrm_book"."id", "bookwyrm_book"."created_date", "bookwyrm_book"."updated_date", "bookwyrm_book"."remote_id", "bookwyrm_book"."origin_id", "bookwyrm_book"."openlibrary_key", "bookwyrm_book"."finna_key", "bookwyrm_book"."libris_key", "bookwyrm_book"."inventaire_id", "bookwyrm_book"."librarything_key", "bookwyrm_book"."goodreads_key", "bookwyrm_book"."bnf_id", "bookwyrm_book"."viaf", "bookwyrm_book"."wikidata", "bookwyrm_book"."asin", "bookwyrm_book"."aasin", "bookwyrm_book"."isfdb", "bookwyrm_book"."search_vector", "bookwyrm_book"."last_edited_by_id", "bookwyrm_book"."connector_id", "bookwyrm_book"."title", "bookwyrm_book"."sort_title", "bookwyrm_book"."subtitle", "bookwyrm_book"."description", "bookwyrm_book"."languages", "bookwyrm_book"."series", "bookwyrm_book"."series_number", "bookwyrm_book"."subjects", "bookwyrm_book"."subject_places", "bookwyrm_book"."cover", "bookwyrm_book"."preview_image", "bookwyrm_book"."first_published_date", "bookwyrm_book"."published_date", "bookwyrm_book"."first_published_date_precision", "bookwyrm_book"."published_date_precision", "bookwyrm_edition"."book_ptr_id", "bookwyrm_edition"."isbn_10", "bookwyrm_edition"."isbn_13", "bookwyrm_edition"."oclc_number", "bookwyrm_edition"."pages", "bookwyrm_edition"."physical_format", "bookwyrm_edition"."physical_format_detail", "bookwyrm_edition"."publishers", "bookwyrm_edition"."parent_work_id", "bookwyrm_edition"."edition_rank" FROM "bookwyrm_edition" INNER JOIN "bookwyrm_shelfbook" ON ("bookwyrm_edition"."book_ptr_id" = "bookwyrm_shelfbook"."book_id") INNER JOIN "bookwyrm_book" ON ("bookwyrm_edition"."book_ptr_id" = "bookwyrm_book"."id") WHERE (NOT ("bookwyrm_edition"."parent_work_id" IN (SELECT U0."book_ptr_id" AS "id" FROM "bookwyrm_work" U0 INNER JOIN "bookwyrm_user_blocked_books" U1 ON (U0."book_ptr_id" = U1."work_id") WHERE U1."user_id" = 1) AND "bookwyrm_edition"."parent_work_id" IS NOT NULL) AND "bookwyrm_shelfbook"."shelf_id" IN (SELECT U0."id" FROM "bookwyrm_shelf" U0 WHERE U0."user_id" = 1)) ORDER BY "bookwyrm_book"."sort_title" ASC LIMIT 15;

 Limit  (cost=5314.10..5315.79 rows=15 width=2765) (actual time=33.291..33.315 rows=15 loops=1)
   ->  Unique  (cost=5314.10..5316.24 rows=19 width=2765) (actual time=33.290..33.313 rows=15 loops=1)
 Planning Time: 1.839 ms
 Execution Time: 33.459 ms

What type of Pull Request is this?

  • Bug Fix
  • Enhancement
  • Plumbing / Internals / Dependencies
  • Refactor

Does this PR change settings or dependencies, or break something?

  • This PR changes or adds default settings, configuration, or .env values
  • This PR changes or adds dependencies
  • This PR introduces other breaking changes

Details of breaking or configuration changes (if any of above checked)

Documentation

  • New or amended documentation will be required if this PR is merged
  • I have created a matching pull request in the Documentation repository
  • I intend to create a matching pull request in the Documentation repository after this PR is merged

Tests

  • My changes do not need new tests
  • All tests I have added are passing
  • I have written tests but need help to make them pass
  • I have not written tests and need help to write them

@hughrun hughrun added the performance Things related to performance, optimization, load times, etc label Jul 20, 2026

@ilkka-ollakka ilkka-ollakka left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great improvement!

@ilkka-ollakka
ilkka-ollakka merged commit 3cd6f1f into main Jul 21, 2026
9 checks passed
@mouse-reeve
mouse-reeve deleted the shelf-sort-query branch July 21, 2026 04:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Things related to performance, optimization, load times, etc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants