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:
@@ -574,11 +574,36 @@ def maybe_settle_club_rewards(club_id: str, shenfen: str, riqi: str) -> Optional
|
||||
return None
|
||||
|
||||
min_metric = scheme.min_metric or Decimal('0')
|
||||
club_map = load_user_club_map([r['yonghuid'] for r in rows])
|
||||
_all_uids = [r['yonghuid'] for r in rows]
|
||||
club_map = load_user_club_map(_all_uids)
|
||||
expire_at = timezone.now() + timedelta(days=scheme.claim_days or 30)
|
||||
balance_field = BALANCE_FIELD_MAP[shenfen]
|
||||
claims_to_create = []
|
||||
|
||||
# 批量预取活跃用户集合,避免循环内 exists 查询
|
||||
if shenfen == 'dashou':
|
||||
_active_uids = set(
|
||||
UserDashou.objects.filter(user__UserUID__in=_all_uids, zhanghaozhuangtai=1)
|
||||
.values_list('user__UserUID', flat=True)
|
||||
)
|
||||
elif shenfen == 'guanshi':
|
||||
_active_uids = set(
|
||||
UserGuanshi.objects.filter(user__UserUID__in=_all_uids, zhuangtai=1)
|
||||
.values_list('user__UserUID', flat=True)
|
||||
)
|
||||
elif shenfen == 'zuzhang':
|
||||
_active_uids = set(
|
||||
UserZuzhang.objects.filter(user__UserUID__in=_all_uids, zhuangtai=1)
|
||||
.values_list('user__UserUID', flat=True)
|
||||
)
|
||||
elif shenfen == 'shangjia':
|
||||
_active_uids = set(
|
||||
UserShangjia.objects.filter(user__UserUID__in=_all_uids, zhuangtai=1)
|
||||
.values_list('user__UserUID', flat=True)
|
||||
)
|
||||
else:
|
||||
_active_uids = set()
|
||||
|
||||
for idx, row in enumerate(rows):
|
||||
mingci = idx + 1
|
||||
metric = Decimal(str(row['metric'] or 0))
|
||||
@@ -590,7 +615,7 @@ def maybe_settle_club_rewards(club_id: str, shenfen: str, riqi: str) -> Optional
|
||||
uid = row['yonghuid']
|
||||
if club_map.get(uid) != club_id:
|
||||
continue
|
||||
if not user_has_active_shenfen(uid, shenfen):
|
||||
if uid not in _active_uids:
|
||||
continue
|
||||
idem = f'{club_id}|{scheme.id}|{period_key}|{uid}'
|
||||
claims_to_create.append(RankRewardClaim(
|
||||
@@ -618,14 +643,14 @@ def maybe_settle_club_rewards(club_id: str, shenfen: str, riqi: str) -> Optional
|
||||
sort_field=scheme.sort_field,
|
||||
claim_count=0,
|
||||
)
|
||||
created = 0
|
||||
for claim in claims_to_create:
|
||||
claim.settlement_id = settlement.id
|
||||
try:
|
||||
claim.save()
|
||||
created += 1
|
||||
except IntegrityError:
|
||||
pass
|
||||
# 批量创建 claims,用 ignore_conflicts 处理幂等冲突
|
||||
if claims_to_create:
|
||||
for claim in claims_to_create:
|
||||
claim.settlement_id = settlement.id
|
||||
RankRewardClaim.objects.bulk_create(claims_to_create, ignore_conflicts=True)
|
||||
created = RankRewardClaim.objects.filter(settlement_id=settlement.id).count()
|
||||
else:
|
||||
created = 0
|
||||
settlement.claim_count = created
|
||||
settlement.save(update_fields=['claim_count'])
|
||||
return settlement
|
||||
|
||||
Reference in New Issue
Block a user