Skip to content

Commit

Permalink
Fix library query sort by title, add indexes for books and libraryItems
Browse files Browse the repository at this point in the history
  • Loading branch information
advplyr committed Aug 10, 2023
1 parent 8c9fc3d commit aac2879
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
22 changes: 21 additions & 1 deletion server/models/Book.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,27 @@ module.exports = (sequelize) => {
genres: DataTypes.JSON
}, {
sequelize,
modelName: 'book'
modelName: 'book',
indexes: [
{
fields: [{
name: 'title',
collate: 'NOCASE'
}]
},
{
fields: [{
name: 'titleIgnorePrefix',
collate: 'NOCASE'
}]
},
{
fields: ['publishedYear']
},
{
fields: ['duration']
}
]
})

return Book
Expand Down
15 changes: 14 additions & 1 deletion server/models/LibraryItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ module.exports = (sequelize) => {
*/
static getLibraryItemsIncrement(offset, limit) {
return this.findAll({
benchmark: true,
logging: (sql, timeMs) => {
console.log(`[Query] Elapsed ${timeMs}ms.`)
},
include: [
{
model: sequelize.models.book,
Expand All @@ -89,7 +93,7 @@ module.exports = (sequelize) => {
['createdAt', 'ASC'],
// Ensure author & series stay in the same order
[sequelize.models.book, sequelize.models.author, sequelize.models.bookAuthor, 'createdAt', 'ASC'],
[sequelize.models.book, sequelize.models.series, 'bookSeries', 'createdAt', 'ASC'],
[sequelize.models.book, sequelize.models.series, 'bookSeries', 'createdAt', 'ASC']
],
offset,
limit
Expand Down Expand Up @@ -702,6 +706,15 @@ module.exports = (sequelize) => {
},
{
fields: ['mediaId']
},
{
fields: ['libraryId', 'mediaType']
},
{
fields: ['birthtime']
},
{
fields: ['mtime']
}
]
})
Expand Down
6 changes: 5 additions & 1 deletion server/utils/queries/libraryItemsBookFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ module.exports = {
if (global.ServerSettings.sortingIgnorePrefix) {
return [[Sequelize.literal('titleIgnorePrefix COLLATE NOCASE'), dir]]
} else {
return [[Sequelize.literal('title COLLATE NOCASE'), dir]]
return [[Sequelize.literal('`book`.`title` COLLATE NOCASE'), dir]]
}
} else if (sortBy === 'sequence') {
const nullDir = sortDesc ? 'DESC NULLS FIRST' : 'ASC NULLS LAST'
Expand Down Expand Up @@ -546,6 +546,10 @@ module.exports = {
distinct: true,
attributes: bookAttributes,
replacements,
benchmark: true,
logging: (sql, timeMs) => {
console.log(`[Query] Elapsed ${timeMs}ms.`)
},
include: [
{
model: Database.models.libraryItem,
Expand Down

0 comments on commit aac2879

Please sign in to comment.