原始数据建表优化

This commit is contained in:
lhx
2025-11-18 09:27:06 +08:00
parent d00ae38e16
commit 815cd7801c
2 changed files with 63 additions and 37 deletions

View File

@@ -3,7 +3,7 @@ from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
from .config import settings from .config import settings
engine = create_engine(settings.DATABASE_URL, echo=False, pool_size=15, max_overflow=30, pool_timeout=60, pool_recycle=300) engine = create_engine(settings.DATABASE_URL, pool_pre_ping=True, echo=False, pool_size=30, max_overflow=60, pool_timeout=30, pool_recycle=3600)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base() Base = declarative_base()

View File

@@ -37,7 +37,9 @@ class OriginalDataService(BaseService[OriginalData]):
logger.info(f"Table {table_name} already exists") logger.info(f"Table {table_name} already exists")
return True return True
# 表不存在,创建表 # 表不存在,创建表 - 添加重试机制
max_retries = 3
for attempt in range(max_retries):
try: try:
create_table_sql = f""" create_table_sql = f"""
CREATE TABLE `{table_name}` ( CREATE TABLE `{table_name}` (
@@ -54,15 +56,32 @@ class OriginalDataService(BaseService[OriginalData]):
INDEX `idx_account_id` (`account_id`) INDEX `idx_account_id` (`account_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='原始数据表_账号{account_id}' ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='原始数据表_账号{account_id}'
""" """
db.execute(text(create_table_sql)) # 使用引擎直接执行不依赖db会话的事务
db.commit() with engine.begin() as conn: # 创建独立连接
logger.info(f"Table {table_name} created successfully") conn.execute(text(create_table_sql))
logger.info(f"Table {table_name} created successfully on attempt {attempt + 1}")
return True return True
except Exception as e: except Exception as e:
db.rollback() logger.warning(f"Attempt {attempt + 1} to create table {table_name} failed: {str(e)}")
logger.error(f"Failed to create table {table_name}: {str(e)}")
# 检查是否是表已存在错误(并发情况下可能发生)
if "already exists" in str(e).lower() or "exist" in str(e).lower():
logger.info(f"Table {table_name} was created by another process")
return True
if attempt == max_retries - 1:
db.rollback() # 确保回滚当前事务
logger.error(f"Failed to create table {table_name} after {max_retries} attempts: {str(e)}")
raise Exception(f"创建原始数据表失败: {str(e)}") raise Exception(f"创建原始数据表失败: {str(e)}")
# 短暂延迟后重试
import time
time.sleep(0.1 * (attempt + 1))
return False
def create_table_for_account(self, db: Session, account_id: int) -> Dict[str, Any]: def create_table_for_account(self, db: Session, account_id: int) -> Dict[str, Any]:
""" """
为指定账号创建原始数据表 为指定账号创建原始数据表
@@ -83,12 +102,20 @@ class OriginalDataService(BaseService[OriginalData]):
'message': f'账号ID {account_id} 不存在' 'message': f'账号ID {account_id} 不存在'
} }
# 创建表 # 创建表 - 现在使用增强的错误处理
self._ensure_table_exists(db, account_id) table_created = self._ensure_table_exists(db, account_id)
if table_created:
return { return {
'success': True, 'success': True,
'message': f'原始数据表 {self._get_table_name(account_id)} 创建成功' 'message': f'原始数据表 {self._get_table_name(account_id)} 创建成功'
} }
else:
return {
'success': False,
'message': f'原始数据表 {self._get_table_name(account_id)} 创建失败'
}
except Exception as e: except Exception as e:
logger.error(f"Failed to create table for account {account_id}: {str(e)}") logger.error(f"Failed to create table for account {account_id}: {str(e)}")
return { return {
@@ -296,13 +323,12 @@ class OriginalDataService(BaseService[OriginalData]):
'failed_items': [] 'failed_items': []
} }
# 确保表存在 # 确保表存在 - 现在使用增强的错误处理和重试机制
try: table_created = self._ensure_table_exists(db, account_id)
self._ensure_table_exists(db, account_id) if not table_created:
except Exception as e:
return { return {
'success': False, 'success': False,
'message': f'创建原始数据表失败: {str(e)}', 'message': '创建原始数据表失败',
'total_count': total_count, 'total_count': total_count,
'success_count': 0, 'success_count': 0,
'failed_count': total_count, 'failed_count': total_count,