You need to enable JavaScript to run this app. Python 快速递归校验并更新数据库中缺失文件路径的性能优化
优惠活动
大模型
产品
解决方案
定价
更多

Python 快速递归校验并更新数据库中缺失文件路径的性能优化

业务逻辑说明

输入文件路径示例为/A/B/C/D/E/F,处理规则如下:

  • 首先校验全路径/A/B/C/D/E/F是否存在于应用数据库的system_file_paths集合中,若不存在则进入向上回溯流程
  • 移除路径末尾的/F,校验父路径/A/B/C/D/E是否存在于system_file_paths中:
    • 若存在,则创建子路径F的DB记录
    • 返回路径F对应的记录ID
    • 若/A/B/C/D/E不存在,进入下一步回溯
  • 从原路径中移除/E/F,校验路径/A/B/C/D是否存在于system_file_paths中:
    • 若存在,依次创建后续缺失的E、F路径记录
    • 返回路径F对应的记录ID
    • 若/A/B/C/D不存在,进入下一步回溯
  • 从原路径中移除/D/E/F,校验路径/A/B/C是否存在于system_file_paths列表中:
    • 若存在,依次创建后续缺失的D、E、F路径记录
    • 返回路径F对应的记录ID
    • 若/A/B/C不存在,进入下一步回溯
  • 持续按上述规则向上回溯,直到检查到根路径/A;若根路径仍不存在,则从根路径开始逐级创建所有缺失的子路径记录

注意:上述操作不涉及操作系统层面的目录创建,所有更新均在应用数据库中完成

当前问题

现有实现代码可正常跑通业务逻辑,但在百万级文件、单条路径层级长度可达上千的场景下,性能严重下降。希望通过适配的算法优化,减少冗余校验次数,大幅提升处理性能。

附现有测试实现代码:

# 随机生成测试用系统输入路径
import uuid
def file_path_list(num):
    file_path_list = []
    temp_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
    for i in range(num):
        temp = "{0}".format(i)
        for j in temp_list:
            temp += "{0}".format(j)
        file_path_list.append(temp)

    temp_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h','1','2','3','4']
    for i in range(num):
        temp = "{0}".format(i)
        for j in temp_list:
            temp += "{0}".format(j)

        file_path_list.append(temp)

    temp_list = ['a', 'b', 'c', 'd', 'x', 'y', 'z']
    for i in range(num):
        temp = "{0}".format(i)
        for j in temp_list:
            temp += "{0}".format(j)
        file_path_list.append(temp)
    
    return file_path_list

path_trees = file_path_list(10)

system_file_paths = {}
for path in path_trees:
    system_file_paths[path] = str(uuid.uuid4())

# ************             测试用系统路径生成完成       ***************

# 待处理输入文件
file_path = 'A/B/C/D/E/F'

def update_db(path_name):
    _id = str(uuid.uuid4())
    system_file_paths[path_name] = _id
    return _id


# 校验文件路径是否存在
def check_file_path_exists(file_path):
    if file_path in system_file_paths:
        return True
    return False

# 寻找优化方案,通过适配算法减少重复校验,提升该方法的处理性能
def process_to_update_db(file_path):
    # 拆分文件路径
    file_path_split = file_path.split("/")
    # 倒序遍历路径层级
    for reverse_path_indent in range(len(file_path_split) -1 , -1, -1):

        # 遍历到根节点时,逐级创建所有缺失路径
        if reverse_path_indent == 0:
            if not check_file_path_exists(file_path_split[0]):
                _id = update_db(file_path_split[0])
            for path_indent in range(1, len(file_path_split)):
                file_path = "/".join(file_path_split[0: path_indent + 1])
                _id = update_db(file_path)
            return _id

        file_path_check = "/".join(file_path_split[0: reverse_path_indent])
        if not check_file_path_exists(file_path_check):
            continue
        else:
            for path_indent in range(reverse_path_indent, len(file_path_split)):
                file_path = "/".join(file_path_split[0: path_indent + 1])
                _id = update_db(file_path)
            return _id




if not check_file_path_exists(file_path):
    _id = process_to_update_db(file_path)
    print(system_file_paths)
    # 校验路径是否创建成功
    if check_file_path_exists(file_path):
        print(0)
    else:
        print(1)

内容的提问来源于stack exchange,提问作者Vidya

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.01 06:06:03