[英]SQL - Update all records of a single column
I'm trying to write a python script that parses through a file and updates a database with the new values obtained from the parsed file. 我正在尝试编写一个python脚本来解析文件,并使用从解析文件中获取的新值更新数据库。 My code looks like this:
我的代码如下所示:
startTime = datetime.now()
db = <Get DB Handle>
counter = 0
with open('CSV_FILE.csv') as csv_file:
data = csv_file.read().splitlines()
for line in data:
data1 = line.split(',')
execute_string = "update table1 set col1=" + data1[1] +
" where col0 is '" + data1[0] + "'"
db.execute(execute_string)
counter = counter+1
if(counter % 1000 == 0 and counter != 0):
print ".",
print ""
print datetime.now() - startTime
But that operation took about 10 mins to finish. 但是该操作大约需要10分钟才能完成。 Any way I can tweak my SQL query to quicken it?
有什么办法可以调整SQL查询以加快速度吗?
Read lines in batch size (may be size of 1000) and try bulk_update query for mysql. 以批处理大小(可能是1000个)读取行,并尝试对mysql进行bulk_update查询。 I think that process will be faster than current one as in lesser queries you'll be updating more data.
我认为该过程将比当前过程更快,因为在较少的查询中您将更新更多的数据。
I agree you need batching. 我同意您需要批量处理。 Have a look at this question and best answer How can I do a batch insert into an Oracle database using Python?
查看此问题和最佳答案如何使用Python将批处理插入Oracle数据库?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.