Files
stock/backend/app/db.py
cirry 2f9c8bee2b 日K加载提速:adj_factor 覆盖索引 + 冷路径并发 + 热路径进程内缓存直返
- adj_factor 覆盖索引 (ts_code,trade_date) INCLUDE (adj_factor):根治堆碎片化
  (单股 6516 行散 6516 块,满载时位图堆扫 1.5s+),Index Only Scan ~2ms;
  因子查询全部改 2 列投影,lag() 窗口只取变点(6516→32 行)
- preview 冷路径 2 会话 2 波并发,信息卡合并为 LEFT JOIN LATERAL 一条
- 鉴权会话 60s 进程内缓存(登出/全端登出即时失效),全站请求省 ~80ms
- pvj/chipsj/stocksj/facetsj 存 model_dump_json 原串直返(与 response_model
  字节一致),热路径 230-2190ms → 1-2ms;get_version 本地缓存+bump 即时可见
- 连接池 10+20;smoke_test 适配鉴权缓存
2026-09-02 16:52:28 +08:00

27 lines
1004 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""async SQLAlchemy 引擎与会话。"""
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.orm import DeclarativeBase
from .config import settings
class Base(DeclarativeBase):
"""所有 ORM 模型的基类。"""
# echo=False远程 PG 的空闲连接可能被中间层断开pre_ping + recycle 自动剔除死连接
# pool_size/max_overflowpreview 冷路径每请求开 2 个会话2 波并发Redis 故障时全请求变冷,
# 默认 5+10 不够并发冷请求用(连接池满了只会排队,不会崩)
engine = create_async_engine(
settings.database_url, echo=False, future=True,
pool_pre_ping=True, pool_recycle=1800,
pool_size=10, max_overflow=20,
)
async_session = async_sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
async def get_session() -> AsyncSession:
"""FastAPI 依赖:提供一个事务会话。"""
async with async_session() as session:
yield session