Repository navigation
bookmarks に処理の状態・最後に開いた日時・保存した日時のインデックスを足し、よく読み直す問い合わせで全件を読まずに済むようにする(DB v30) - #104
Merged
Merged
Conversation
…(DB v30) - aiStatus: 処理の状況(処理待ち・処理中・失敗)。ほとんどの行は処理済みなので、全件を見ずに済む - lastAccessedAt: 再発見(ウィジェット・通知の候補。古い順の先頭だけ)、最近読んだもの - createdAt: 未読の新しい順の先頭だけ(「次に読む」)、保存した期間での絞り込み - MIGRATION_29_30 は CREATE INDEX だけ(列・行は変えない)。30.json を足した。バックアップの版は変わらない - マイグレーションのテストと、インデックスあり・なしで同じ問い合わせを比べるテスト(BookmarkIndexPerformanceTest)を足した Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
概要
「ときどきとても重くなる」の調査の4本目(#98・#101・#102 の続き)。
bookmarksにはcontentUriのインデックスしか無く、よく読み直す問い合わせが毎回全件を読んでいた。aiStatus・lastAccessedAt・createdAtのインデックスを足す。スキーマの変更(DB v29 → v30)。バックアップの版は変わらない(v22 のまま)。事前に確かめたこと: 開いている PR で
AppDatabase.VERSIONを上げているものは無い(#7 は develop → main、#99・#103 はスキーマに触れない)。原因(根拠)
EXPLAIN QUERY PLAN(下の測ったテストで出したもの):WHERE aiStatus IN ('PENDING', 'PROCESSING')):SCAN bookmarks(全件)。処理待ちが数十件でも、毎回 5,000 件を読むORDER BY lastAccessedAt, id LIMIT 30):SCAN bookmarks / USE TEMP B-TREE FOR ORDER BY(全件を読んで並べ替えてから先頭 30 件)aiStatus・lastAccessedAt・createdAtは、行の中で本文(content)より後ろにある。本文が1ページに収まらない行では、SQLite はこれらの列を読むのに本文のはみ出したページまでたどる。端末の DB は SQLCipher で暗号化しているので、ページを読むたびに復号も入るこれらの問い合わせは Room の Flow で、ブックマークが変わるたびに読み直される(処理の状況の画面とウィジェットの進み具合、ウィジェットの候補、一覧上部の「次に読む」)。
変更内容
BookmarkEntityにIndex("aiStatus")・Index("lastAccessedAt")・Index("createdAt")MIGRATION_29_30:CREATE INDEX IF NOT EXISTSを3つ(30.json の createSql と同じ)。列・行は変えない。AppDatabase.VERSION = 30、30.jsonを足したid)も持つので、ORDER BY lastAccessedAt, id・ORDER BY createdAt DESC, id DESCもそのまま使えるobserveFiltered)の並べ替えはCASE式なので、インデックスでは速くならない(この PR の対象外)測った結果
BookmarkIndexPerformanceTest(Robolectric、ファイルの DB): 5,000 件(本文 約 2,000 字、処理待ち 32 件・失敗 10 件)。同じ DB で、インデックスあり → 3つを消したもの(v29 まで)と比べ、結果が同じことも確かめている。中央値:getStale)SEARCH bookmarks USING COVERING INDEX index_bookmarks_aiStatus (aiStatus=?)、再発見の候補はSCAN bookmarks USING COVERING INDEX index_bookmarks_lastAccessedAt(並べ替えの一時の木が要らなくなる)テスト
MigrationTest.migrate29To30_addsBookmarkIndexesAndKeepsRows: v29 の DB の行がそのまま残り、v30 のスキーマ(インデックスも)と一致し、インデックスが4つある。MigrationPathTestは v6 からの全部の版を v30 まで上げて確かめる./gradlew :app:assembleDebug :app:testDebugUnitTest :app:lintDebug(絞り込みなし)を通した: 単体テスト 2,598 件・失敗 0(スキップ 4)、Lint の問題なし。一覧・処理の状況の問い合わせで本文を読まないようにし、一覧の読む時間の目安は本文が変わったものだけ求め直す #102 を取り込む前の結果。取り込んだ後のものも手元で流している(CI でも確かめる)端末で確かめること(未検証)
🤖 Generated with Claude Code