From 052c7c19b3699c83902e122ac8eadd519bef323f Mon Sep 17 00:00:00 2001 From: lanceyq <1982376970@qq.com> Date: Mon, 30 Mar 2026 17:42:47 +0800 Subject: [PATCH] [fix] Avoid unnecessary index creation costs --- api/app/core/memory/agent/utils/write_tools.py | 5 ----- api/app/main.py | 9 +++++++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/api/app/core/memory/agent/utils/write_tools.py b/api/app/core/memory/agent/utils/write_tools.py index abbcc54d..3af9326e 100644 --- a/api/app/core/memory/agent/utils/write_tools.py +++ b/api/app/core/memory/agent/utils/write_tools.py @@ -151,11 +151,6 @@ async def write( # Step 3: Save all data to Neo4j database step_start = time.time() - from app.repositories.neo4j.create_indexes import create_all_indexes - try: - await create_all_indexes() - except Exception as e: - logger.error(f"Error creating indexes: {e}", exc_info=True) # 添加死锁重试机制 max_retries = 3 diff --git a/api/app/main.py b/api/app/main.py index f4c23ca8..2fdf40b6 100644 --- a/api/app/main.py +++ b/api/app/main.py @@ -62,6 +62,15 @@ async def lifespan(app: FastAPI): logger.info("预定义模型加载已禁用 (LOAD_MODEL=false)") logger.info("应用程序启动完成") + + # 初始化 Neo4j 索引和约束(仅启动时执行一次) + try: + from app.repositories.neo4j.create_indexes import create_all_indexes + await create_all_indexes() + logger.info("Neo4j 索引和约束初始化完成") + except Exception as e: + logger.warning(f"Neo4j 索引初始化失败(服务仍可启动,但查询性能可能受影响): {e}") + yield # 应用关闭事件 logger.info("应用程序正在关闭")