-
Notifications
You must be signed in to change notification settings - Fork 493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: optimize query #633
Conversation
optimize the number of SQL queries when creating backups
cache categories to avoid query many times
@@ -295,8 +297,11 @@ class MangaRestorer( | |||
categories: List<Long>, | |||
backupCategories: List<BackupCategory>, | |||
) { | |||
val dbCategories = getCategories.await() | |||
val dbCategoriesByName = dbCategories.associateBy { it.name } | |||
if (this.dbCategoriesMapByNameCache == null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having this inline with a force non-null is really messy. Put it behind a function which fills the cache on first call instead. then this reduces to something like
val dbCategoriesByName = dbCategoriesByName()
Even though it is still effectively a side effect, it makes it more clear that this is just a cache fill. I would have suggested turning it into a delegated property, but suspend makes it difficult.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have moved this up to BackupRestorer
} | ||
}.groupBy({ x -> x.first }, { x -> x.second }) | ||
} else { | ||
null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace this with an empty map
manga_syncQueries.getTracks(backupTrackMapper) | ||
}.groupBy({ x -> x.first }, { x -> x.second }) | ||
} else { | ||
null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here as well, replace this with an empty map
private suspend fun backupManga( | ||
manga: Manga, | ||
options: BackupOptions, | ||
categoriesMap: Map<Long, List<Category>>?, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make these non-null
@@ -48,16 +70,18 @@ class MangaBackupCreator( | |||
} | |||
|
|||
if (options.categories) { | |||
assert(categoriesMap != null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the assert and null checks to the category map
mangaObject.categories = categoriesForManga.map { it.order } | ||
} | ||
} | ||
|
||
if (options.tracking) { | ||
val tracks = handler.awaitList { manga_syncQueries.getTracksByMangaId(manga.id, backupTrackMapper) } | ||
if (tracks.isNotEmpty()) { | ||
assert(trackingMap != null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same thing, remove the null checks
suspend fun backupMangas(mangas: List<Manga>, options: BackupOptions): List<BackupManga> { | ||
val categoriesMap: Map<Long, List<Category>>? = if (options.categories) { | ||
handler.awaitList { | ||
categoriesQueries.getCategoriesWithMangaId { mangaId, categoryId, name, order, flags -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this up to an interactor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks unnecessary and overly designed, as it will only be used during backup (load all into memory).
Even after refactoring, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it's a debugger issue. After disconnecting the debugger, creating a backup is fast, almost close to 1 second. |
Close now, as this may cause bugs. See #647. |
optimize the number of SQL queries when creating backups.
Before:
For each manga, execute one SQL query
Now:
For all manga, execute one SQL query