Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 加速大型进程运行从数据库获取的某些数据_Python_Mysql_Django - Fatal编程技术网

Python 加速大型进程运行从数据库获取的某些数据

Python 加速大型进程运行从数据库获取的某些数据,python,mysql,django,Python,Mysql,Django,所以我在一个项目中工作,在这个项目中,我必须读取一个包含1000万条记录的大型数据库(对我来说,它很大)。我不能真正地过滤它们,因为我必须单独对待它们。对于每条记录,我必须应用一个公式,然后根据记录的某些条件将结果写入多个文件 我已经实现了一些算法,完成整个过程大约需要2-3天。这是一个问题,因为我正在尝试优化一个已经花费了这段时间的流程。一天可以接受 到目前为止,我已经在数据库上尝试了索引,线程化(记录上的进程,而不是I/O操作)。我不能再短一点了 我使用的是django,由于它的懒惰行为,我

所以我在一个项目中工作,在这个项目中,我必须读取一个包含1000万条记录的大型数据库(对我来说,它很大)。我不能真正地过滤它们,因为我必须单独对待它们。对于每条记录,我必须应用一个公式,然后根据记录的某些条件将结果写入多个文件

我已经实现了一些算法,完成整个过程大约需要2-3天。这是一个问题,因为我正在尝试优化一个已经花费了这段时间的流程。一天可以接受

到目前为止,我已经在数据库上尝试了索引,线程化(记录上的进程,而不是I/O操作)。我不能再短一点了

我使用的是django,由于它的懒惰行为,我无法衡量真正开始处理数据需要多少时间。我还想知道,我是否可以在收到数据后立即开始处理数据,而不必等到所有数据加载到内存后才能实际处理它。这也可能是我对在python上编写操作的理解。最后可能是我需要一台更好的机器(我怀疑,我有4核和4GB内存,它应该能够提供更好的速度)

有什么想法吗?我非常感谢您的反馈。:)

编辑:代码

说明:

我提到的记录是客户的身份证(护照),条件是公司的不同终端(国家)之间是否有协议。这个过程是一个散列

第一种策略尝试处理整个数据库。。。我们在开始时已经为处理算法的条件部分做了一些准备(国家之间的协议)。然后通过是否属于一个集合进行大型验证

由于我一直在努力自己改进它,所以我尝试在第二个策略中将问题分成几个部分,按部分处理查询(获取属于某个国家的记录,并将与之有协议的记录写入文件)

没有描述线程化策略,因为它是为单个国家设计的,与没有线程化策略相比,我得到了糟糕的结果。老实说,我有一种直觉,它必须是一种记忆和sql的东西

def create_all_files(strategy=0):
    if strategy == 0:
        set_countries_agreements = set()
        file_countries = open(os.path.join(PROJECT_ROOT, 'list_countries'))
        set_countries_temp = set(line.strip() for line in file_countries)
        file_countries.close()
        set_countries = sorted_nicely(set_countries_temp)

        for each_country in set_countries:
            set_agreements = frozenset(get_agreements(each_country))
            set_countries_agreements.add(set_agreements)

        print("All agreements obtained")

        set_passports = Passport.objects.all()

        print("All passports obtained")


        for each_passport in set_passports:
            for each_agreement in set_countries_agreements:
                for each_country in each_agreement:
                    if each_passport.nationality == each_country:
                        with open(os.path.join(PROJECT_ROOT, 'generated_indexes/%s' % iter(each_agreement).next()), "a") as f:
                            f.write(generate_hash(each_passport.nationality + "<" + each_passport.id_passport, each_country) + "\n")
                    print(".")
                print("_")
            print("-")
        print("~")

    if strategy == 1:

        file_countries = open(os.path.join(PROJECT_ROOT, 'list_countries'))
        set_countries_temp = set(line.strip() for line in file_countries)
        file_countries.close()
        set_countries = sorted_nicely(set_countries_temp)

        while len(set_countries)!= 0:
            country = set_countries.pop()
            list_countries = get_agreements(country)
            list_passports = Passport.objects.filter(nationality=country)
            for each_passport in list_passports:
                for each_country in list_countries:
                    with open(os.path.join(PROJECT_ROOT, 'generated_indexes/%s' % each_country), "a") as f:
                        f.write(generate_hash(each_passport.nationality + "<" + each_passport.id_passport, each_country) + "\n")
                        print("r")
                print("c")
            print("p")
        print("P")
def创建所有文件(策略=0):
如果策略==0:
set_countries_agreements=set()
file\u countries=open(os.path.join(PROJECT\u ROOT,'list\u countries'))
set_countries_temp=set(文件_countries中的行的line.strip())
文件\u countries.close()
设置国家/地区=分类良好(设置国家/地区温度)
对于set_国家/地区中的每个_国家/地区:
set_agreements=frozenset(获取_agreements(每个_国家))
设置\国家\协议。添加(设置\协议)
打印(“获得的所有协议”)
set_passports=Passport.objects.all()
打印(“获得的所有护照”)
对于set_passport中的每个_passport:
对于set_国家/地区的每个_协议:
对于每个联合国教科文组织协议中的每个联合国教科文组织国家:
如果每个护照国籍==每个国家:
以open(os.path.join(PROJECT\u ROOT,'生成的\u索引/%s'%iter(每个\u协议).next(),“a”)作为f:

f、 在你的问题中,你正在描述一个过程。我建议你使用一个工具

提到Christian Thomsen编写的一些python ETL工具,我认为它运行良好,性能令人印象深刻。请测试它,然后返回结果


我不能不提就发布这个答案。如果您计划通过节点分发任务,这个编程模型可以满足您的要求。

看起来您为每个国家都有一个文件,您可以向其中添加哈希,而不是打开和关闭这些文件的句柄1000多万次,您应该打开每个文件一次,然后关闭这些文件他们都在最后

countries = {}  # country -> file
with open(os.path.join(PROJECT_ROOT, 'list_countries')) as country_file:
    for line in country_file:
        country = line.strip()
        countries[country] = open(os.path.join(PROJECT_ROOT, 'generated_indexes/%s' % country), "a")

for country in countries:
    agreements = get_agreements(country)
    for postcode in Postcode.objects.filter(nationality=country):
        for agreement in agreements:
            countries[agreement].write(generate_hash(passport.nationality + "<" + passport.id_passport, country_agreement) + "\n")

for country, file in countries.items():
    file.close()
countries={}#country->file
将open(os.path.join(PROJECT_ROOT,'list_countries')作为国家/地区文件打开:
对于国家/地区_文件中的行:
国家=行。带()
国家[国家]=开放(os.path.join(PROJECT\u ROOT,'生成的\u索引/%s'%country',“a”)
对于国家中的国家:
协议=获取协议(国家)
对于postcode.objects.filter(国籍=国家)中的邮政编码:
对于协议中的协议:

国家/地区[agreement].write(generate_hash(passport.national+)“您能在问题中粘贴一些代码吗?没有它很难提出优化建议。您的查询/主循环将是有用的。请不要使用[Python]之类的东西来“标记”您的问题,它无助于提升或突出显示您的问题。感谢您的回答,我不知道他的模型。这是一个好主意,我实际上可以看到从附加到编写的更改有了改进。将使用您的答案修改我的代码,从danihp改为(希望如此)获得改进次数。我想知道,这是计算您正在使用的机器的最佳页面大小的一种方法吗?我可以确认,我确实注意到使用这种方法进行写作有很大的改进。我只生成修改后的文件,根据我的计算,我将在今天下午完成。这很好。我还使用了原始SQL来获取结果。仅获取散列所需的数据也提高了速度。您可以使用获取单个
Postcode
对象的大小,以指示您可以轻松地在内存中保存多少。与许多优化一样,始终存在一些跟踪和错误