【发布时间】: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."
【问题讨论】: