简体   繁体   English

Django MySQL-将多个记录一起保存在数据库中

[英]Django MySQL - Save Multiple Records together in DB

I am trying to save Multiple records in Django MySQL. 我正在尝试在Django MySQL中保存多个记录。 I am able to save each record individually, But it becomes too slow as the array size increases. 我可以单独保存每条记录,但是随着数组大小的增加,它变得太慢了。

Code: [Updated] 代码:[已更新]

def MultipleSave(array1, array2, key1, key2):
    insert_list = []
    l = len(array1)
    for i in range(0, l):
        try:
            str1 = array1[i]
            str2 = array2[i]

            try:
                new_record = UserString.objects.filter(original=str1)
                new_record = new_record[0]

                setattr(new_record, key2, str2)
                new_record.save()
            except Exception as e:            
                new_record = UserString(original=str1)

                setattr(new_record, key1, str1)
                setattr(new_record, key2, str2)
                insert_list.append(new_record)

        except Exception as e:
            print ('Exception occured: "%s"' % e)

    if len(insert_list) > 0:
        UserString.objects.bulk_create(insert_list)

[Update]: This updated Code now creates all new records at once using bulk_create. [更新]:现在,此更新的代码使用bulk_create一次创建所有新记录。 But existing records are still updated one at a time. 但是现有记录仍会一次更新一次。

Is there an alternate way to Save/Update all records at once in the DB rather than for each record ? 是否有另一种方法可以一次保存/更新数据库中的所有记录,而不是针对每个记录? Or Alternatively, How Can this Code be optimized ? 或者,如何优化此代码?

Any suggestions are welcomed. 任何建议都欢迎。

Thanks, 谢谢,

This problem got solved by creating a new list update_list containing all the records which needs to be updated then saving all the records at one go using transaction.atomic() 通过创建一个包含所有需要更新的记录的新列表update_list,然后使用transaction.atomic()一次性保存所有记录,解决了该问题。

Updated Code[Working]: (in Django 1.9) 更新的代码[工作中] :(在Django 1.9中)

def MultipleSave(array1, array2, key1, key2):
    insert_list = []
    update_list = []
    l = len(array1)
    for i in range(0, l):
        try:
            str1 = array1[i]
            str2 = array2[i]

            try:
                new_record = UserString.objects.filter(original=str1)
                new_record = new_record[0]

                setattr(new_record, key2, str2)
                update_list.append(new_record)
            except Exception as e:            
                new_record = UserString(original=str1)

                setattr(new_record, key1, str1)
                setattr(new_record, key2, str2)
                insert_list.append(new_record)

        except Exception as e:
            print ('Exception occured: "%s"' % e)

    if len(insert_list) > 0:
        UserString.objects.bulk_create(insert_list)

    with transaction.atomic():
        for record in update_list:
            record.save()

Hope this might be helpful. 希望这会有所帮助。 Thanks, 谢谢,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM