Repository navigation
Improve performance on shelf views - #4059
Merged
Merged
Conversation
ilkka-ollakka
approved these changes
Jul 21, 2026
ilkka-ollakka
left a comment
Contributor
There was a problem hiding this comment.
great improvement!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 msAnd 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 msWhat type of Pull Request is this?
Does this PR change settings or dependencies, or break something?
Details of breaking or configuration changes (if any of above checked)
Documentation
Tests