feat: 查询-n批次从正则改用like以优化性能
This commit is contained in:
parent
e2a92b6faa
commit
43abcbaa48
|
|
@ -17,6 +17,8 @@ import re
|
|||
from django.db.models import Q
|
||||
import django.utils.timezone as timezone
|
||||
from apps.utils.sql import query_all_dict
|
||||
import logging
|
||||
myLogger = logging.getLogger('log')
|
||||
|
||||
# Create your models here.
|
||||
class SfLog(CommonADModel):
|
||||
|
|
@ -837,41 +839,46 @@ class BatchLog(BaseModel):
|
|||
|
||||
@classmethod
|
||||
def batches_to(cls, batch:str):
|
||||
|
||||
# query = """
|
||||
# SELECT batch FROM wpm_batchst
|
||||
# WHERE batch ~ %s
|
||||
# """
|
||||
query = """
|
||||
SELECT batch
|
||||
FROM wpm_batchst
|
||||
WHERE batch ~ %s
|
||||
ORDER BY
|
||||
-- 先按前缀部分排序(例如 'A')
|
||||
SUBSTRING(batch FROM '^(.*)-') DESC,
|
||||
-- 再按后缀的数值部分排序(将 '2', '11' 转为整数)
|
||||
CAST(SUBSTRING(batch FROM '-([0-9]+)$') AS INTEGER) DESC
|
||||
""" # 排序可在sql层处理
|
||||
query_ = """SELECT batch FROM wpm_batchst WHERE batch ~ %s"""
|
||||
pattern = f'^{batch}-[0-9]+$'
|
||||
SELECT
|
||||
batch,
|
||||
CAST(substring(batch FROM LENGTH(%s) + 2) AS INTEGER) AS batch_num
|
||||
FROM wpm_batchst
|
||||
WHERE batch LIKE %s AND translate(
|
||||
substring(batch FROM LENGTH(%s) + 2),
|
||||
'0123456789',
|
||||
''
|
||||
) = ''
|
||||
ORDER BY batch_num DESC
|
||||
"""
|
||||
|
||||
"""可以用如下方法直接查询
|
||||
"""
|
||||
# batches = BatchLog.objects.filter(source__batch=batch, relation_type="split").values_list("target__batch", flat=True).distinct()
|
||||
# batches = sorted(list(batches), key=custom_key)
|
||||
batches_r = query_all_dict(query_, params=(pattern,))
|
||||
batches = [b["batch"] for b in batches_r]
|
||||
batches = sorted(list(batches), key=custom_key)
|
||||
last_batch_num = None
|
||||
if batches:
|
||||
last_batch = batches[-1]
|
||||
last_batch_list = last_batch.split("-")
|
||||
if last_batch_list:
|
||||
try:
|
||||
last_batch_num = int(last_batch_list[-1])
|
||||
except Exception:
|
||||
pass
|
||||
return {"batches": batches, "last_batch_num": last_batch_num, "last_batch": last_batch}
|
||||
return {"batches": [], "last_batch_num": None, "last_batch": None}
|
||||
prefix = batch
|
||||
params = (
|
||||
prefix,
|
||||
f"{prefix}-%",
|
||||
prefix
|
||||
)
|
||||
|
||||
try:
|
||||
rows = query_all_dict(query, params=params)
|
||||
except Exception as e:
|
||||
myLogger.error(f"BatchLog.batches_to error: {(str(e), query, params)}")
|
||||
raise
|
||||
|
||||
if not rows:
|
||||
return {
|
||||
"batches": [],
|
||||
"last_batch_num": None,
|
||||
"last_batch": None,
|
||||
}
|
||||
|
||||
batches = [r["batch"] for r in rows]
|
||||
last = rows[0]
|
||||
|
||||
return {
|
||||
"batches": batches,
|
||||
"last_batch_num": last["batch_num"],
|
||||
"last_batch": last["batch"],
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -92,11 +92,20 @@ class WprViewSet(CustomListModelMixin, RetrieveModelMixin, ComplexQueryMixin, Cu
|
|||
# 使用原始sql
|
||||
query = """
|
||||
SELECT id, number_out FROM wpmw_wpr
|
||||
WHERE number_out ~ %s order by number_out desc limit 1
|
||||
WHERE number_out LIKE %s
|
||||
AND translate(
|
||||
substring(number_out FROM LENGTH(%s) + 2),
|
||||
'0123456789',
|
||||
''
|
||||
) = ''
|
||||
order by number_out desc limit 1
|
||||
"""
|
||||
pattern = f"^{prefix}[0-9]+$"
|
||||
params = (
|
||||
f"{prefix}-%",
|
||||
prefix
|
||||
)
|
||||
number_outs = []
|
||||
wpr_qs_last = query_one_dict(query, [pattern])
|
||||
wpr_qs_last = query_one_dict(query, [params])
|
||||
if wpr_qs_last:
|
||||
number_outs.append(wpr_qs_last["number_out"])
|
||||
# 查找未出库的记录
|
||||
|
|
@ -106,9 +115,15 @@ class WprViewSet(CustomListModelMixin, RetrieveModelMixin, ComplexQueryMixin, Cu
|
|||
query2 = """
|
||||
select mioitemw.id, mioitemw.number_out from inm_mioitemw mioitemw left join inm_mioitem mioitem on mioitem.id = mioitemw.mioitem_id
|
||||
left join inm_mio mio on mio.id = mioitem.mio_id
|
||||
where mio.submit_time is null and mioitemw.number_out ~ %s order by mioitemw.number_out desc limit 1
|
||||
where mio.submit_time is null and mioitemw.number_out LIKE %s
|
||||
AND translate(
|
||||
substring(mioitemw.number_out FROM LENGTH(%s) + 2),
|
||||
'0123456789',
|
||||
''
|
||||
) = ''
|
||||
order by mioitemw.number_out desc limit 1
|
||||
"""
|
||||
mioitemw_last = query_one_dict(query2, [pattern])
|
||||
mioitemw_last = query_one_dict(query2, [params])
|
||||
if mioitemw_last:
|
||||
number_outs.append(mioitemw_last["number_out"])
|
||||
if number_outs:
|
||||
|
|
|
|||
Loading…
Reference in New Issue