You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

数据库系统中多维度条件过滤与cut_Size累加实现方案问询

嘿,这个场景我太熟悉了——之前做金属制品库存统计系统时,也碰到过类似的多属性过滤+统计需求,嵌套If在cut_Dims这种高基数属性面前完全撑不住,很快就会变成“面条代码”。给你几个实用的思路,从逻辑方案到伪代码都有:

核心思路:用「规则匹配器」替代硬编码嵌套判断

把每个属性的过滤条件抽象成独立的规则,而不是写死在嵌套If里。不管cut_Dims有几百种取值,只需要维护规则集合,主统计逻辑完全不用改动。

1. 基础版:通用过滤+统计伪代码

首先定义物品的数据结构(用伪代码表示):

// 物品结构
struct Item {
    item_Type: Enum(Pipe, Rod, Tube)
    cut_Size: Number  // 要累加的数值
    finish: Enum(#3, #8, 2B)
    sub_Type: Union(
        PipeSchedule(Schedule40, Schedule20),
        RodShape(Square, Rectangular, Round),
        TubeShape(Square, Rectangular, Round)
    )
    cut_Dims: String  // 数百种取值,用字符串存储即可
}

然后把过滤规则做成键值对集合,每个键对应物品的属性名,值是一个判断函数(或匹配条件):

// 示例:某一次统计的过滤规则
filter_rules = {
    "item_Type": lambda x: x == Pipe,
    "finish": lambda x: x == #3,
    "sub_Type": lambda x: x == Schedule40,
    // cut_Dims的判断直接用「是否在允许列表中」
    "cut_Dims": lambda x: x in ["D100x5", "D150x6", "D200x8", ...]  // 这里放数百种允许的取值
}

最后是通用的统计函数,遍历所有物品并检查是否符合所有规则:

function calculate_total_cut_size(items: List[Item], filter_rules: Dict):
    total = 0
    for item in items:
        is_match = True
        // 逐个检查所有规则
        for attr_name, check_func in filter_rules.items():
            // 获取物品对应的属性值,传入判断函数
            if not check_func(getattr(item, attr_name)):
                is_match = False
                break  // 只要有一个规则不满足,直接跳过当前物品
        if is_match:
            total += item.cut_Size
    return total

这种方式的好处是:

  • 新增cut_Dims的取值?只需要更新允许列表,不用改统计逻辑
  • 新增其他属性的过滤条件?直接在filter_rules里加新的键值对就行
  • 完全避免了嵌套If的臃肿,代码可读性高

2. 性能优化版:预构建cut_Dims索引

如果物品数量特别大(比如几万甚至几十万条),每次遍历都检查cut_Dims是否在列表里会有点慢。可以提前给物品按cut_Dims分组,构建索引:

// 预构建cut_Dims到物品列表的索引(只需要初始化一次)
cut_dims_index = {}
for item in items:
    if item.cut_Dims not in cut_dims_index:
        cut_dims_index[item.cut_Dims] = []
    cut_dims_index[item.cut_Dims].append(item)

然后统计时先通过索引筛选出符合cut_Dims的候选物品,再过滤其他属性:

function calculate_total_with_index(items: List[Item], filter_rules: Dict):
    total = 0
    candidate_items = []
    
    // 先处理cut_Dims规则,快速筛选候选物品
    if "cut_Dims" in filter_rules:
        // 找出所有符合条件的cut_Dims取值
        allowed_dims = [dim for dim in cut_dims_index.keys() if filter_rules["cut_Dims"](dim)]
        // 把对应物品合并到候选列表
        for dim in allowed_dims:
            candidate_items.extend(cut_dims_index[dim])
    else:
        candidate_items = items
    
    // 再过滤其他属性
    for item in candidate_items:
        is_match = True
        for attr_name, check_func in filter_rules.items():
            if attr_name == "cut_Dims":
                continue  // 已经通过索引过滤过了
            if not check_func(getattr(item, attr_name)):
                is_match = False
                break
        if is_match:
            total += item.cut_Size
    return total

这个优化能大幅减少需要遍历的物品数量,尤其是当cut_Dims的过滤范围比较窄的时候。

3. 进阶:规则可配置化(应对数千种统计组合)

如果有数千种统计需求,手动写filter_rules肯定不现实。可以把每个统计任务的规则存在数据库表或JSON配置文件里,比如:

{
    "task_id": "stat_task_001",
    "description": "统计Schedule40、#3光洁度、指定cut_Dims的Pipe总cut_Size",
    "rules": {
        "item_Type": "Pipe",
        "finish": "#3",
        "sub_Type": "Schedule40",
        "cut_Dims": ["D100x5", "D150x6", "D200x8", ...]
    }
}

然后写一个简单的规则解析器,把配置里的规则转换成对应的判断函数:

function parse_rules(config_rules: Dict):
    filter_rules = {}
    for attr_name, condition in config_rules.items():
        if attr_name == "cut_Dims":
            // 如果是cut_Dims,判断是否在配置的列表中
            filter_rules[attr_name] = lambda x: x in condition
        else:
            // 其他枚举类型属性,直接判断相等
            filter_rules[attr_name] = lambda x: x == condition
    return filter_rules

这样每次新增统计任务,只需要加一条配置,完全不用改代码,完美应对数千种组合的需求。

内容的提问来源于stack exchange,提问作者J. Rousselot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:31:08