【问题标题】:Python Peewee MySQL bulk updatePython Peewee MySQL 批量更新
【发布时间】:2017-08-01 01:51:20
【问题描述】:

我正在使用 Python 2.7、Peewee 和 MySQL。如果 csv 中存在订单号,我的程序会从 csv 文件中读取并更新该字段。可能有 2000-3000 次更新,我正在使用幼稚的方法来一一更新记录,这非常慢。我已经从使用 Peewee 更新转移到原始查询,这会更快一些。但是,它仍然非常缓慢。我想知道如何在不使用循环的情况下以更少的事务更新记录。

def mark_as_uploaded_to_zoho(self, which_file):
    print "->Started marking the order as uploaded to zoho."
    with open(which_file, 'rb') as file:
        reader = csv.reader(file, encoding='utf-8')
        next(reader, None) ## skipping the header

        for r in reader:
            order_no = r[0]
            query = '''UPDATE sales SET UploadedToZoho=1 WHERE OrderNumber="%s" and UploadedToZoho=0''' %order_no
            SalesOrderLine.raw(query).execute()

    print "->Marked as uploaded to zoho."

【问题讨论】:

    标签: python peewee


    【解决方案1】:

    新的最佳答案是将Model.bulk_update 用于事务。可以更改 batch_size 参数以调整性能。

    # CSV generator for efficient memory usage
    def gen_csv(file_name, header=True, **kwargs):
      with open(file_name, 'r') as f:
        reader = csv.reader(f, **kwargs)
        if header:
          next(reader, None)
        for row in reader:
          yield row
    
    # Update based on order number
    with database.atomic():
      rows = gen_csv(file_path, encoding='utf-8')
      SalesOrderLine.bulk_update(rows, fields=['OrderNumber'], batch_size=200)
    

    【讨论】:

    • 问的问题是关于批量更新,而不是插入。
    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 2023-01-14
    • 1970-01-01
    • 2011-09-11
    • 1970-01-01
    相关资源
    最近更新 更多