#!/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}")