feat: optimize SQLite PRAGMAs (WAL, mmap 256MB, cache 32MB, temp_store) and add compound indexes
This commit is contained in:
1 parent
438dfdb055
commit
078fa08fbd
3 files changed
+135
-16
No files matched your search
@@ -1042,3 +1042,76 @@ def test_http_connection_pooling():
|
||||
assert c3 is not c1
|
||||
assert not c3.is_closed
|
||||
asyncio.run(close_http_client())
|
||||
|
||||
|
||||
def test_sqlite_pragma_tuning_and_composite_indexes():
|
||||
from app.database import get_db, get_db_connection
|
||||
|
||||
# 1. Verify PRAGMAs on a newly opened connection
|
||||
conn = get_db_connection()
|
||||
try:
|
||||
j_mode = conn.execute("PRAGMA journal_mode;").fetchone()[0]
|
||||
sync = conn.execute("PRAGMA synchronous;").fetchone()[0]
|
||||
mmap = conn.execute("PRAGMA mmap_size;").fetchone()[0]
|
||||
cache = conn.execute("PRAGMA cache_size;").fetchone()[0]
|
||||
temp = conn.execute("PRAGMA temp_store;").fetchone()[0]
|
||||
fk = conn.execute("PRAGMA foreign_keys;").fetchone()[0]
|
||||
|
||||
assert j_mode.lower() == "wal"
|
||||
assert sync == 1 # NORMAL
|
||||
assert mmap == 268435456 # 256MB mmap
|
||||
assert cache == -32000 # 32MB cache
|
||||
assert temp == 2 # MEMORY
|
||||
assert fk == 1 # foreign keys ON
|
||||
|
||||
# 2. Verify all composite indexes exist in sqlite_master
|
||||
index_rows = conn.execute(
|
||||
"SELECT name, tbl_name, sql FROM sqlite_master WHERE type = 'index'"
|
||||
).fetchall()
|
||||
index_names = {r["name"] for r in index_rows}
|
||||
|
||||
expected_indexes = {
|
||||
"idx_vocab_user_created",
|
||||
"idx_vocab_user_srs_state",
|
||||
"idx_vocab_user_srs_created",
|
||||
"idx_vocab_user_term_nocase",
|
||||
"idx_books_user_updated",
|
||||
"idx_srs_card",
|
||||
"idx_srs_reviewed",
|
||||
"idx_sessions_user",
|
||||
}
|
||||
for idx in expected_indexes:
|
||||
assert idx in index_names, f"Expected index {idx} not found in database!"
|
||||
|
||||
# 3. Verify EXPLAIN QUERY PLAN uses the composite indexes
|
||||
plan1 = conn.execute(
|
||||
"EXPLAIN QUERY PLAN SELECT * FROM vocabulary_cards WHERE user_id = ? ORDER BY created_at DESC LIMIT 20",
|
||||
(1,),
|
||||
).fetchall()
|
||||
plan1_str = " ".join([r[3] for r in plan1])
|
||||
assert "idx_vocab_user_created" in plan1_str
|
||||
|
||||
plan2 = conn.execute(
|
||||
"EXPLAIN QUERY PLAN SELECT * FROM vocabulary_cards WHERE user_id = ? AND srs_state = 'new' ORDER BY created_at DESC LIMIT 10",
|
||||
(1,),
|
||||
).fetchall()
|
||||
plan2_str = " ".join([r[3] for r in plan2])
|
||||
assert "idx_vocab_user_srs_created" in plan2_str or "idx_vocab_user_created" in plan2_str
|
||||
|
||||
plan3 = conn.execute(
|
||||
"EXPLAIN QUERY PLAN SELECT * FROM books WHERE user_id = ? ORDER BY updated_at DESC",
|
||||
(1,),
|
||||
).fetchall()
|
||||
plan3_str = " ".join([r[3] for r in plan3])
|
||||
assert "idx_books_user_updated" in plan3_str
|
||||
|
||||
plan4 = conn.execute(
|
||||
"EXPLAIN QUERY PLAN SELECT * FROM srs_logs WHERE card_id = ?",
|
||||
(1,),
|
||||
).fetchall()
|
||||
plan4_str = " ".join([r[3] for r in plan4])
|
||||
assert "idx_srs_card" in plan4_str
|
||||
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
Reference in new issue
Block a user