Commit fdb19049 by 林冲

merchant_goods_code_processor.py 优化为批量

1 parent 03ed6ee6
...@@ -96,8 +96,8 @@ class MysqlHelper: ...@@ -96,8 +96,8 @@ class MysqlHelper:
""" """
try: try:
# 只在非 LAST_INSERT_ID 查询时输出日志 # 只在非 LAST_INSERT_ID 查询时输出日志
if "LAST_INSERT_ID" not in sql.upper(): # if "LAST_INSERT_ID" not in sql.upper():
logging.info(sql) # logging.info(sql)
self.cursor.execute(sql) self.cursor.execute(sql)
res = self.cursor.fetchone() res = self.cursor.fetchone()
return res return res
......
...@@ -228,20 +228,39 @@ class MerchantGoodsCodeProcessor: ...@@ -228,20 +228,39 @@ class MerchantGoodsCodeProcessor:
def round1_fill_from_merchant_goods(self): def round1_fill_from_merchant_goods(self):
""" """
第一轮补数:从商品上架数据和商户商品关联表补齐 第一轮补数:从商品上架数据和商户商品关联表补齐
批量处理,每批10000条
""" """
self.logger.info("=" * 80) self.logger.info("=" * 80)
self.logger.info("第一轮补数:从商品上架数据补齐...") self.logger.info("第一轮补数:从商品上架数据补齐...")
# 查询所有需要处理的上下文 # 查询总记录数
count_sql = f"SELECT COUNT(*) FROM {self.context_table};"
result = self.db.execute_query_one(count_sql)
total_count = result[0] if result else 0
self.logger.info(f"共 {total_count} 条记录需要处理")
batch_size = 10000
offset = 0
total_processed = 0
total_updated = 0
total_inserted = 0
total_skipped = 0
while offset < total_count:
self.logger.info(f"处理批次 {offset // batch_size + 1},范围 [{offset + 1}, {min(offset + batch_size, total_count)}]")
# 查询当前批次的上下文
query_sql = f""" query_sql = f"""
SELECT id, relation_id, rule_type, goods_type_group_rule_id, upgrade_goods_id, SELECT id, relation_id, rule_type, goods_type_group_rule_id, upgrade_goods_id,
merchant_id, scene_type, base_scene_code merchant_id, scene_type, base_scene_code
FROM {self.context_table} FROM {self.context_table}
ORDER BY id; ORDER BY id
LIMIT {batch_size} OFFSET {offset};
""" """
contexts = self.db.execute_query(query_sql) contexts = self.db.execute_query(query_sql)
self.logger.info(f"共 {len(contexts)} 条记录需要处理") if not contexts:
break
processed = 0 processed = 0
updated = 0 updated = 0
...@@ -281,10 +300,16 @@ class MerchantGoodsCodeProcessor: ...@@ -281,10 +300,16 @@ class MerchantGoodsCodeProcessor:
inserted += 1 inserted += 1
processed += 1 processed += 1
if processed % 100 == 0:
self.logger.info(f" 已处理 {processed}/{len(contexts)} 条")
self.logger.info(f"第一轮补数完成:处理 {processed} 条,更新 {updated} 条,插入 {inserted} 条,跳过 {skipped} 条") self.logger.info(f" 批次完成:处理 {processed} 条,更新 {updated} 条,插入 {inserted} 条,跳过 {skipped} 条")
total_processed += processed
total_updated += updated
total_inserted += inserted
total_skipped += skipped
offset += batch_size
self.logger.info(f"第一轮补数完成:总计处理 {total_processed} 条,更新 {total_updated} 条,插入 {total_inserted} 条,跳过 {total_skipped} 条")
def _query_merchant_goods_codes(self, merchant_id, base_scene_code, upgrade_goods_id): def _query_merchant_goods_codes(self, merchant_id, base_scene_code, upgrade_goods_id):
""" """
...@@ -400,21 +425,40 @@ class MerchantGoodsCodeProcessor: ...@@ -400,21 +425,40 @@ class MerchantGoodsCodeProcessor:
""" """
第二轮补数:从历史订单补下架商品 第二轮补数:从历史订单补下架商品
注意:第一轮已补的记录,如果订单中还有其他merchant_goods_code,第二轮仍需补充 注意:第一轮已补的记录,如果订单中还有其他merchant_goods_code,第二轮仍需补充
批量处理,每批10000条
""" """
self.logger.info("=" * 80) self.logger.info("=" * 80)
self.logger.info("第二轮补数:从历史订单补齐...") self.logger.info("第二轮补数:从历史订单补齐...")
# 查询所有上下文记录(包括第一轮已处理的) # 查询总记录数
count_sql = f"SELECT COUNT(*) FROM {self.context_table};"
result = self.db.execute_query_one(count_sql)
total_count = result[0] if result else 0
self.logger.info(f"共 {total_count} 条记录需要检查订单补齐")
batch_size = 10000
offset = 0
total_processed = 0
total_updated = 0
total_inserted = 0
total_skipped = 0
while offset < total_count:
self.logger.info(f"处理批次 {offset // batch_size + 1},范围 [{offset + 1}, {min(offset + batch_size, total_count)}]")
# 查询当前批次的上下文记录(包括第一轮已处理的)
query_sql = f""" query_sql = f"""
SELECT ctx.id, ctx.relation_id, ctx.rule_type, ctx.goods_type_group_rule_id, SELECT ctx.id, ctx.relation_id, ctx.rule_type, ctx.goods_type_group_rule_id,
ctx.upgrade_goods_id, ctx.merchant_id, ctx.scene_type, ctx.base_scene_code, ctx.upgrade_goods_id, ctx.merchant_id, ctx.scene_type, ctx.base_scene_code,
ctx.settle_rule_id, ctx.division_rule_id, ctx.reconciliation_rule_id ctx.settle_rule_id, ctx.division_rule_id, ctx.reconciliation_rule_id
FROM {self.context_table} ctx FROM {self.context_table} ctx
ORDER BY ctx.id; ORDER BY ctx.id
LIMIT {batch_size} OFFSET {offset};
""" """
contexts = self.db.execute_query(query_sql) contexts = self.db.execute_query(query_sql)
self.logger.info(f"共 {len(contexts)} 条记录需要检查订单补齐") if not contexts:
break
processed = 0 processed = 0
updated = 0 updated = 0
...@@ -471,10 +515,16 @@ class MerchantGoodsCodeProcessor: ...@@ -471,10 +515,16 @@ class MerchantGoodsCodeProcessor:
inserted += 1 inserted += 1
processed += 1 processed += 1
if processed % 100 == 0:
self.logger.info(f" 已处理 {processed}/{len(contexts)} 条")
self.logger.info(f"第二轮补数完成:处理 {processed} 条,更新 {updated} 条,插入 {inserted} 条,跳过 {skipped} 条") self.logger.info(f" 批次完成:处理 {processed} 条,更新 {updated} 条,插入 {inserted} 条,跳过 {skipped} 条")
total_processed += processed
total_updated += updated
total_inserted += inserted
total_skipped += skipped
offset += batch_size
self.logger.info(f"第二轮补数完成:总计处理 {total_processed} 条,更新 {total_updated} 条,插入 {total_inserted} 条,跳过 {total_skipped} 条")
def _check_first_round_processed(self, relation_id): def _check_first_round_processed(self, relation_id):
"""检查第一轮是否已处理""" """检查第一轮是否已处理"""
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!