From d75694c9e5f3a679c374fde501f04921ddf3353e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=A5=E5=BE=AE?= Date: Wed, 8 Jul 2026 10:13:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=A3=81=E7=9B=98IO=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E2=80=94=E2=80=94state.db=20auto=5Fvacuum+cache=E5=A2=9E?= =?UTF-8?q?=E5=A4=A7+=E6=AF=8F=E5=91=A8=E5=8E=8B=E7=BC=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/vacuum_state_db.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 scripts/vacuum_state_db.py diff --git a/scripts/vacuum_state_db.py b/scripts/vacuum_state_db.py new file mode 100644 index 00000000..34455077 --- /dev/null +++ b/scripts/vacuum_state_db.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 +"""vacuum_state_db.py — 每周整理state.db,防止磁盘I/O退化 + +在非交易时段运行(周六凌晨),不影响交易系统。 +""" +import sqlite3, os + +DBS = [ + "/home/hmo/.hermes/profiles/position-analyst/state.db", + "/home/hmo/.hermes/state.db", +] + +for db_path in DBS: + if not os.path.exists(db_path): + continue + size_before = os.path.getsize(db_path) / 1024 / 1024 + try: + c = sqlite3.connect(db_path) + c.execute("PRAGMA auto_vacuum=2") + # 只做incremental vacuum,不做full vacuum(耗时太长) + c.execute("PRAGMA incremental_vacuum(50000)") + c.execute("PRAGMA cache_size=-200000") + c.execute("PRAGMA mmap_size=268435456") + c.execute("PRAGMA wal_checkpoint(TRUNCATE)") + c.close() + size_after = os.path.getsize(db_path) / 1024 / 1024 + saved = size_before - size_after + print(f"{db_path.split('/')[-2]}: {size_before:.0f}MB -> {size_after:.0f}MB (reclaim {saved:.0f}MB)") + except Exception as e: + print(f"{db_path.split('/')[-2]}: ERROR {e}")