Browse Source

Optimize library query

inorichi 6 years ago
parent
commit
36aabf23e1

+ 8 - 2
app/src/main/java/eu/kanade/tachiyomi/data/database/DbOpenHelper.kt

@@ -17,7 +17,7 @@ class DbOpenHelper(context: Context)
         /**
          * Version of the database.
          */
-        const val DATABASE_VERSION = 7
+        const val DATABASE_VERSION = 8
     }
 
     override fun onCreate(db: SQLiteDatabase) = with(db) {
@@ -30,8 +30,9 @@ class DbOpenHelper(context: Context)
 
         // DB indexes
         execSQL(MangaTable.createUrlIndexQuery)
-        execSQL(MangaTable.createFavoriteIndexQuery)
+        execSQL(MangaTable.createLibraryIndexQuery)
         execSQL(ChapterTable.createMangaIdIndexQuery)
+        execSQL(ChapterTable.createUnreadChaptersIndexQuery)
         execSQL(HistoryTable.createChapterIdIndexQuery)
     }
 
@@ -60,6 +61,11 @@ class DbOpenHelper(context: Context)
         if (oldVersion < 7) {
             db.execSQL(TrackTable.addLibraryId)
         }
+        if (oldVersion < 8) {
+            db.execSQL("DROP INDEX IF EXISTS mangas_favorite_index")
+            db.execSQL(MangaTable.createLibraryIndexQuery)
+            db.execSQL(ChapterTable.createUnreadChaptersIndexQuery)
+        }
     }
 
     override fun onConfigure(db: SQLiteDatabase) {

+ 4 - 0
app/src/main/java/eu/kanade/tachiyomi/data/database/tables/ChapterTable.kt

@@ -49,6 +49,10 @@ object ChapterTable {
     val createMangaIdIndexQuery: String
         get() = "CREATE INDEX ${TABLE}_${COL_MANGA_ID}_index ON $TABLE($COL_MANGA_ID)"
 
+    val createUnreadChaptersIndexQuery: String
+        get() = "CREATE INDEX ${TABLE}_unread_by_manga_index ON $TABLE($COL_MANGA_ID, $COL_READ) " +
+                "WHERE $COL_READ = 0"
+
     val sourceOrderUpdateQuery: String
         get() = "ALTER TABLE $TABLE ADD COLUMN $COL_SOURCE_ORDER INTEGER DEFAULT 0"
 

+ 3 - 2
app/src/main/java/eu/kanade/tachiyomi/data/database/tables/MangaTable.kt

@@ -60,6 +60,7 @@ object MangaTable {
     val createUrlIndexQuery: String
         get() = "CREATE INDEX ${TABLE}_${COL_URL}_index ON $TABLE($COL_URL)"
 
-    val createFavoriteIndexQuery: String
-        get() = "CREATE INDEX ${TABLE}_${COL_FAVORITE}_index ON $TABLE($COL_FAVORITE)"
+    val createLibraryIndexQuery: String
+        get() = "CREATE INDEX library_${COL_FAVORITE}_index ON $TABLE($COL_FAVORITE) " +
+                "WHERE $COL_FAVORITE = 1"
 }