feat(database): 为 SQLite 添加 WAL 模式支持和性能优化配置

增加 SQLite 数据库性能优化功能,包括:
- 启用 WAL 模式提高并发性能
- 设置合理的超时时间和同步级别
- 添加数据库定期维护功能
- 在会话初始化时自动应用优化配置
- 增加连接超时时间从30秒到60秒

这些优化显著提升了 SQLite 数据库在高并发场景下的性能和稳定性。
This commit is contained in:
Windpicker-owo
2025-09-28 22:04:44 +08:00
parent 28bce19d27
commit 8b034e21c6
2 changed files with 81 additions and 7 deletions

View File

@@ -194,7 +194,7 @@ async def db_query(
# 首先获取要更新的记录
result = await session.execute(query)
records_to_update = result.scalars().all()
# 更新每个记录
affected_rows = 0
for record in records_to_update:
@@ -202,7 +202,7 @@ async def db_query(
if hasattr(record, field):
setattr(record, field, value)
affected_rows += 1
return affected_rows
elif query_type == "delete":
@@ -217,13 +217,13 @@ async def db_query(
# 首先获取要删除的记录
result = await session.execute(query)
records_to_delete = result.scalars().all()
# 删除记录
affected_rows = 0
for record in records_to_delete:
session.delete(record)
affected_rows += 1
return affected_rows
elif query_type == "count":
@@ -416,4 +416,4 @@ async def store_action_info(
except Exception as e:
logger.error(f"[SQLAlchemy] 存储动作信息时发生错误: {e}")
traceback.print_exc()
return None
return None