Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
dj
/
dealaccountdata
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 57b3f260
authored
May 16, 2026
by
林冲
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
merchant_goods_code_processor.py改为商户维度处理
1 parent
91986133
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
96 additions
and
46 deletions
scene_refactor_data/db_config.yaml
scene_refactor_data/merchant_goods_code_processor.py
scene_refactor_data/db_config.yaml
View file @
57b3f26
...
@@ -10,19 +10,19 @@
...
@@ -10,19 +10,19 @@
# db: "dj_smartcarlife_scene_pro"
# db: "dj_smartcarlife_scene_pro"
# charset: "utf8mb4"
# charset: "utf8mb4"
test_smartcarlife_db
:
host
:
"
192.168.3.227"
port
:
3306
user
:
"
dev_admin"
password
:
"
1f7e1ed2"
db
:
"
dj_smartcarlife_scene_test_0506_done_copy"
charset
:
"
utf8mb4"
#test_smartcarlife_db:
#test_smartcarlife_db:
# host: "192.168.
1.228
"
# host: "192.168.
3.227
"
# port: 330
7
# port: 330
6
# user: "
wechat_user
"
# user: "
dev_admin
"
# password: "
b4WRTkl
"
# password: "
1f7e1ed2
"
# db: "dj_smartcarlife"
# db: "dj_smartcarlife
_scene_test_0506_done_copy
"
# charset: "utf8mb4"
# charset: "utf8mb4"
test_smartcarlife_db
:
host
:
"
192.168.1.228"
port
:
3307
user
:
"
wechat_user"
password
:
"
b4WRTkl"
db
:
"
dj_smartcarlife"
charset
:
"
utf8mb4"
scene_refactor_data/merchant_goods_code_processor.py
View file @
57b3f26
...
@@ -29,7 +29,7 @@ class MerchantGoodsCodeProcessor:
...
@@ -29,7 +29,7 @@ class MerchantGoodsCodeProcessor:
self
.
context_table
=
'tmp_goods_relation_context'
self
.
context_table
=
'tmp_goods_relation_context'
self
.
detail_table
=
'tmp_merchant_goods_code_detail'
self
.
detail_table
=
'tmp_merchant_goods_code_detail'
self
.
exception_table
=
'tmp_merchant_goods_code_exception'
self
.
exception_table
=
'tmp_merchant_goods_code_exception'
self
.
query_keys_table
=
'tmp_query_keys'
# 用于批量查询的临时表
#
self.query_keys_table = 'tmp_query_keys' # 用于批量查询的临时表
# 内存缓存:商品数据
# 内存缓存:商品数据
self
.
goods_code_cache
=
None
# {(merchant_id, base_scene_code, upgrade_goods_id): [merchant_goods_code, ...]}
self
.
goods_code_cache
=
None
# {(merchant_id, base_scene_code, upgrade_goods_id): [merchant_goods_code, ...]}
...
@@ -97,23 +97,23 @@ class MerchantGoodsCodeProcessor:
...
@@ -97,23 +97,23 @@ class MerchantGoodsCodeProcessor:
"""
"""
# 4. 批量查询键临时表(内存表,用于JOIN优化)
# 4. 批量查询键临时表(内存表,用于JOIN优化)
create_query_keys_sql
=
f
"""
#
create_query_keys_sql = f"""
CREATE TABLE IF NOT EXISTS {self.query_keys_table} (
#
CREATE TABLE IF NOT EXISTS {self.query_keys_table} (
merchant_id BIGINT,
#
merchant_id BIGINT,
base_scene_code VARCHAR(20) COLLATE utf8mb4_unicode_ci,
#
base_scene_code VARCHAR(20) COLLATE utf8mb4_unicode_ci,
upgrade_goods_id BIGINT,
#
upgrade_goods_id BIGINT,
settle_rule_id BIGINT,
#
settle_rule_id BIGINT,
reconciliation_rule_id BIGINT,
#
reconciliation_rule_id BIGINT,
KEY idx_merchant_goods (merchant_id, base_scene_code, upgrade_goods_id),
#
KEY idx_merchant_goods (merchant_id, base_scene_code, upgrade_goods_id),
KEY idx_finance (settle_rule_id, base_scene_code, upgrade_goods_id),
#
KEY idx_finance (settle_rule_id, base_scene_code, upgrade_goods_id),
KEY idx_reconciliation (reconciliation_rule_id, base_scene_code, upgrade_goods_id)
#
KEY idx_reconciliation (reconciliation_rule_id, base_scene_code, upgrade_goods_id)
) ENGINE=MEMORY DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='批量查询键临时表';
#
) ENGINE=MEMORY DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='批量查询键临时表';
"""
#
"""
self
.
db
.
execute_update
(
create_context_sql
)
self
.
db
.
execute_update
(
create_context_sql
)
self
.
db
.
execute_update
(
create_detail_sql
)
self
.
db
.
execute_update
(
create_detail_sql
)
self
.
db
.
execute_update
(
create_exception_sql
)
self
.
db
.
execute_update
(
create_exception_sql
)
self
.
db
.
execute_update
(
create_query_keys_sql
)
#
self.db.execute_update(create_query_keys_sql)
self
.
logger
.
info
(
"临时表创建完成"
)
self
.
logger
.
info
(
"临时表创建完成"
)
def
drop_temp_tables
(
self
):
def
drop_temp_tables
(
self
):
...
@@ -381,7 +381,7 @@ class MerchantGoodsCodeProcessor:
...
@@ -381,7 +381,7 @@ class MerchantGoodsCodeProcessor:
def
round1_fill_from_merchant_goods
(
self
):
def
round1_fill_from_merchant_goods
(
self
):
"""
"""
第一轮补数:从商品上架数据和商户商品关联表补齐
第一轮补数:从商品上架数据和商户商品关联表补齐
批量处理,每批5000条,使用游标分页
先按商户+规则类型维度分组,再对每组进行批量处理(每批5000条,使用游标分页)
"""
"""
import
gc
import
gc
...
@@ -394,24 +394,63 @@ class MerchantGoodsCodeProcessor:
...
@@ -394,24 +394,63 @@ class MerchantGoodsCodeProcessor:
total_count
=
result
[
0
]
if
result
else
0
total_count
=
result
[
0
]
if
result
else
0
self
.
logger
.
info
(
f
"共 {total_count} 条记录需要处理"
)
self
.
logger
.
info
(
f
"共 {total_count} 条记录需要处理"
)
batch_size
=
5000
# 优化:从100000降到5000
# 第一步:查询所有需要处理的商户+规则类型组合
merchant_rule_sql
=
f
"""
SELECT DISTINCT merchant_id, rule_type
FROM {self.context_table}
ORDER BY merchant_id, rule_type;
"""
merchant_rule_groups
=
self
.
db
.
execute_query
(
merchant_rule_sql
)
if
not
merchant_rule_groups
:
self
.
logger
.
warning
(
"未找到需要处理的商户+规则类型组合"
)
return
self
.
logger
.
info
(
f
"共 {len(merchant_rule_groups)} 个商户+规则类型组合需要处理"
)
# 全局统计
global_total_processed
=
0
global_total_updated
=
0
global_total_inserted
=
0
global_total_skipped
=
0
group_num
=
0
# 第二步:遍历每个商户+规则类型组合
for
merchant_id
,
rule_type
in
merchant_rule_groups
:
group_num
+=
1
self
.
logger
.
info
(
f
"="
*
60
)
self
.
logger
.
info
(
f
"处理第 {group_num}/{len(merchant_rule_groups)} 组:merchant_id={merchant_id}, rule_type={rule_type}"
)
# 查询当前组的记录数
group_count_sql
=
f
"""
SELECT COUNT(*) FROM {self.context_table}
WHERE merchant_id = {merchant_id} AND rule_type = {rule_type};
"""
result
=
self
.
db
.
execute_query_one
(
group_count_sql
)
group_total_count
=
result
[
0
]
if
result
else
0
self
.
logger
.
info
(
f
" 当前组共 {group_total_count} 条记录"
)
# 第三步:对当前组进行游标分页处理
batch_size
=
5000
last_id
=
0
last_id
=
0
batch_num
=
0
batch_num
=
0
total
_processed
=
0
group
_processed
=
0
total
_updated
=
0
group
_updated
=
0
total
_inserted
=
0
group
_inserted
=
0
total
_skipped
=
0
group
_skipped
=
0
while
True
:
while
True
:
batch_num
+=
1
batch_num
+=
1
self
.
logger
.
info
(
f
"处理批次 {batch_num},已处理 {total_processed}/{
total_count} 条"
)
self
.
logger
.
info
(
f
" 处理批次 {batch_num},已处理 {group_processed}/{group_
total_count} 条"
)
# 使用游标分页代替OFFSET
# 使用游标分页,增加 merchant_id 和 rule_type 过滤条件
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}
WHERE id > {last_id}
WHERE merchant_id = {merchant_id}
AND rule_type = {rule_type}
AND id > {last_id}
ORDER BY id
ORDER BY id
LIMIT {batch_size};
LIMIT {batch_size};
"""
"""
...
@@ -428,17 +467,17 @@ class MerchantGoodsCodeProcessor:
...
@@ -428,17 +467,17 @@ class MerchantGoodsCodeProcessor:
query_keys
=
set
()
query_keys
=
set
()
ctx_map
=
{}
# relation_id -> context
ctx_map
=
{}
# relation_id -> context
for
ctx
in
contexts
:
for
ctx
in
contexts
:
ctx_id
,
relation_id
,
rule_type
,
goods_type_group_rule_id
,
upgrade_goods_id
,
\
ctx_id
,
relation_id
,
rule_type_val
,
goods_type_group_rule_id
,
upgrade_goods_id
,
\
merchant_id
,
scene_type
,
base_scene_code
=
ctx
merchant_id_val
,
scene_type
,
base_scene_code
=
ctx
query_keys
.
add
((
merchant_id
,
base_scene_code
,
upgrade_goods_id
))
query_keys
.
add
((
merchant_id_val
,
base_scene_code
,
upgrade_goods_id
))
ctx_map
[
relation_id
]
=
{
ctx_map
[
relation_id
]
=
{
'ctx_id'
:
ctx_id
,
'ctx_id'
:
ctx_id
,
'relation_id'
:
relation_id
,
'relation_id'
:
relation_id
,
'rule_type'
:
rule_type
,
'rule_type'
:
rule_type_val
,
'goods_type_group_rule_id'
:
goods_type_group_rule_id
,
'goods_type_group_rule_id'
:
goods_type_group_rule_id
,
'upgrade_goods_id'
:
upgrade_goods_id
,
'upgrade_goods_id'
:
upgrade_goods_id
,
'merchant_id'
:
merchant_id
,
'merchant_id'
:
merchant_id_val
,
'scene_type'
:
scene_type
,
'scene_type'
:
scene_type
,
'base_scene_code'
:
base_scene_code
'base_scene_code'
:
base_scene_code
}
}
...
@@ -460,6 +499,8 @@ class MerchantGoodsCodeProcessor:
...
@@ -460,6 +499,8 @@ class MerchantGoodsCodeProcessor:
for
relation_id
,
ctx
in
ctx_map
.
items
():
for
relation_id
,
ctx
in
ctx_map
.
items
():
key
=
(
ctx
[
'merchant_id'
],
ctx
[
'base_scene_code'
],
ctx
[
'upgrade_goods_id'
])
key
=
(
ctx
[
'merchant_id'
],
ctx
[
'base_scene_code'
],
ctx
[
'upgrade_goods_id'
])
merchant_goods_codes
=
goods_code_map
.
get
(
key
,
[])
merchant_goods_codes
=
goods_code_map
.
get
(
key
,
[])
# 打印要拆分的商品和上架ID
self
.
logger
.
info
(
f
" relation_id={relation_id}, key={key}, merchant_goods_codes={merchant_goods_codes}"
)
if
not
merchant_goods_codes
:
if
not
merchant_goods_codes
:
skipped
+=
1
skipped
+=
1
...
@@ -509,10 +550,10 @@ class MerchantGoodsCodeProcessor:
...
@@ -509,10 +550,10 @@ class MerchantGoodsCodeProcessor:
self
.
logger
.
info
(
f
" 批次完成:处理 {processed} 条,更新 {updated} 条,插入 {inserted} 条,跳过 {skipped} 条"
)
self
.
logger
.
info
(
f
" 批次完成:处理 {processed} 条,更新 {updated} 条,插入 {inserted} 条,跳过 {skipped} 条"
)
total
_processed
+=
processed
group
_processed
+=
processed
total
_updated
+=
updated
group
_updated
+=
updated
total
_inserted
+=
inserted
group
_inserted
+=
inserted
total
_skipped
+=
skipped
group
_skipped
+=
skipped
# 内存管理:清理大对象
# 内存管理:清理大对象
del
updates
,
inserts
,
details
,
ctx_map
,
goods_code_map
,
query_keys
,
contexts
del
updates
,
inserts
,
details
,
ctx_map
,
goods_code_map
,
query_keys
,
contexts
...
@@ -522,7 +563,16 @@ class MerchantGoodsCodeProcessor:
...
@@ -522,7 +563,16 @@ class MerchantGoodsCodeProcessor:
gc
.
collect
()
gc
.
collect
()
self
.
logger
.
info
(
f
" 已完成 {batch_num} 批次,执行垃圾回收"
)
self
.
logger
.
info
(
f
" 已完成 {batch_num} 批次,执行垃圾回收"
)
self
.
logger
.
info
(
f
"第一轮补数完成:总计处理 {total_processed} 条,更新 {total_updated} 条,插入 {total_inserted} 条,跳过 {total_skipped} 条"
)
# 当前组处理完成
self
.
logger
.
info
(
f
" 当前组完成:处理 {group_processed} 条,更新 {group_updated} 条,插入 {group_inserted} 条,跳过 {group_skipped} 条"
)
global_total_processed
+=
group_processed
global_total_updated
+=
group_updated
global_total_inserted
+=
group_inserted
global_total_skipped
+=
group_skipped
self
.
logger
.
info
(
"="
*
80
)
self
.
logger
.
info
(
f
"第一轮补数完成:总计处理 {global_total_processed} 条,更新 {global_total_updated} 条,插入 {global_total_inserted} 条,跳过 {global_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
):
"""
"""
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment