perf: P1 性能优化批量修复 + kefu 视图拆分
P1 性能优化(避免循环内 N+1 / N 次 DB 查询): - shop_order_views._batch_images: 移除 ThreadPoolExecutor 掩盖的 N 次查询,改单次批量 + Python 侧分组 - product_query.DashouHuiyuanList: 5N 查询 -> 3 次批量预取 + 本地闭包判断 - roles.GetRolePermissionView / 用户列表: 循环内 RolePermission/Permission/UserRole/Role -> 批量 __in 预取 - guanli / zuzhang 杜次分红 4.2/4.3: 循环内 update_or_create + 单条 delete -> bulk_create + bulk_update + 批量 delete - admin_config QQ 群配置: 循环内 exists/first/save/create -> 批量预取 + bulk_create + bulk_update - jituan 服务层多处合并统计查询、批量预取 map - rank 多处循环 N+1 改批量预取 - backend 多处循环内 count/create 改批量 - config/orders/merchant_ops 多处循环 N+1 改批量预取 其他改动: - users/views/kefu.py 拆分为 kefu_base/kefu_dashou/kefu_orders/kefu_punishment/kefu_withdraw 5 个文件 - 删除遗留脚本 check_prod_uid.py / create_rbac_tables.sql
This commit is contained in:
@@ -532,12 +532,18 @@ class AdGetOrderList(APIView):
|
||||
# 注意:这里统计所有订单,不考虑筛选条件
|
||||
try:
|
||||
# 平台订单统计 (Platform=1)
|
||||
platform_total = Order.query.filter(Platform=1).count()
|
||||
platform_completed = Order.query.filter(Platform=1, Status=3).count()
|
||||
_agg = Order.query.aggregate(
|
||||
platform_total=Count('id', filter=Q(Platform=1)),
|
||||
platform_completed=Count('id', filter=Q(Platform=1, Status=3)),
|
||||
merchant_total=Count('id', filter=Q(Platform=2)),
|
||||
merchant_completed=Count('id', filter=Q(Platform=2, Status=3)),
|
||||
)
|
||||
platform_total = _agg['platform_total']
|
||||
platform_completed = _agg['platform_completed']
|
||||
|
||||
# 商家订单统计 (Platform=2)
|
||||
merchant_total = Order.query.filter(Platform=2).count()
|
||||
merchant_completed = Order.query.filter(Platform=2, Status=3).count()
|
||||
merchant_total = _agg['merchant_total']
|
||||
merchant_completed = _agg['merchant_completed']
|
||||
|
||||
platform_stats = {
|
||||
'total': platform_total,
|
||||
@@ -1170,9 +1176,14 @@ class CfGuanLi(APIView):
|
||||
)
|
||||
|
||||
# 7. 获取统计信息(总是需要统计)
|
||||
zongshu = PenaltyRecord.query.count()
|
||||
daichuli = PenaltyRecord.query.filter(ApplyStatus__in=[0, 3]).count()
|
||||
yichuli = PenaltyRecord.query.filter(ApplyStatus__in=[1, 2]).count()
|
||||
_agg = PenaltyRecord.query.aggregate(
|
||||
zongshu=Count('id'),
|
||||
daichuli=Count('id', filter=Q(ApplyStatus__in=[0, 3])),
|
||||
yichuli=Count('id', filter=Q(ApplyStatus__in=[1, 2])),
|
||||
)
|
||||
zongshu = _agg['zongshu']
|
||||
daichuli = _agg['daichuli']
|
||||
yichuli = _agg['yichuli']
|
||||
|
||||
# 8. 如果只请求统计信息,直接返回
|
||||
if qingqiu_tongji:
|
||||
@@ -1687,6 +1698,13 @@ class AdckyhxqView(APIView):
|
||||
yonghu_id=uid
|
||||
).order_by('-CreateTime')
|
||||
|
||||
# 批量预取会员详情
|
||||
_huiyuan_ids = [r.huiyuan_id for r in member_records]
|
||||
_huiyuan_map = {
|
||||
h.huiyuan_id: h
|
||||
for h in Huiyuan.query.filter(huiyuan_id__in=_huiyuan_ids).only('huiyuan_id', 'jieshao')
|
||||
}
|
||||
|
||||
member_list = []
|
||||
for record in member_records:
|
||||
# 调用方法检查是否过期,并更新状态
|
||||
@@ -1694,9 +1712,7 @@ class AdckyhxqView(APIView):
|
||||
|
||||
if not is_expired: # 只返回未过期的
|
||||
# 查询会员详情
|
||||
huiyuan = Huiyuan.query.filter(
|
||||
huiyuan_id=record.huiyuan_id
|
||||
).first()
|
||||
huiyuan = _huiyuan_map.get(record.huiyuan_id)
|
||||
|
||||
if huiyuan:
|
||||
member_list.append({
|
||||
@@ -2282,15 +2298,13 @@ class AddtxshView(APIView):
|
||||
has_more = (offset + current_count) < total_count
|
||||
|
||||
# 获取状态统计(统计与搜索无关,依然按筛选条件统计)
|
||||
_agg = Tixianjilu.query.filter(leixing=leixing).aggregate(
|
||||
awaiting=Count('id', filter=Q(zhuangtai=1)),
|
||||
processed=Count('id', filter=Q(zhuangtai__in=[2, 3])),
|
||||
)
|
||||
status_counts = {
|
||||
'awaiting': Tixianjilu.query.filter(
|
||||
zhuangtai=1,
|
||||
leixing=leixing
|
||||
).count(),
|
||||
'processed': Tixianjilu.query.filter(
|
||||
zhuangtai__in=[2, 3],
|
||||
leixing=leixing
|
||||
).count()
|
||||
'awaiting': _agg['awaiting'],
|
||||
'processed': _agg['processed'],
|
||||
}
|
||||
|
||||
# 构建响应数据
|
||||
|
||||
Reference in New Issue
Block a user