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.
Brief summary
This adds indices to significantly improve database library query time when sorting by size or duration.
Which issue is fixed?
There's no issue. I encountered this while working on PR #3726.
In-depth Description
The following query in
libraryItemsBookFilters.js
is where most of books fetch time is spent and its performance heavily depends onsortOrder
. When fast scrolling on the library page happens, this can lead to choking on the server if a number of page requests are handled concurrently.I fixed a couple of the easy cases by introducing indices on size and duration.
This leads to a significant improvement in the query performance when paging through the library with sortBy size or duration.
I only dealt with size and duration for now because they were the easiest. Other sort orders are either already covered by existing indices, or they're trickier to optimize because of the existing database schema (e.g. sorting by author). Maybe I'll try to improve them in a future PR.
How have you tested this?
I tested by running 35 consecutive page requests, by scrolling down the library page. There was no parallel fetching.
Sorting by size
Current:
After adding index on
(library_id, media_type, size)
on tablelibraryItems
:Sorting by duration
Current:
After adding index on
duration
on tablebooks
:As you can the indices improve performance significantly.
One thing to note here is that even with the added index, the query performance slowly degrades with increasing offset.
This is because the larger the offset, the more records need to be examined to satisfy the query (this is a known issue when working with offsets). There's a technique to deal with this (keyset pagination), but it is a bit more tricky to implement. If you think it's worth the effort, I'll look into it.