import MysqLdb as mdb
import sys
import csv
con = None
command = ''
new_name_list = []
old_name_list = []
duplicates = []
update_list = []
file = 'csv_file.csv'
listReader = csv.reader(open(file,'r'))
for row in listReader:
new_name_list.append(row)
try:
con = mdb.connect('localhost','root','mypassword','mydb')
con.autocommit(True)
cur = con.cursor()
cur.execute("SELECT fil_name FROM file WHERE fil_name like 'boy%' and fil_job_id=1")
numrows = int(cur.rowcount)
for i in range(numrows):
file_name = cur.fetchone()
old_name_list.append(file_name[0])
d = dict(new_name_list)
for n in old_name_list:
try:
print n + " has been updated to " + d[n]
command = "UPDATE file SET fil_name='" + d[n] + "' WHERE fil_name='" + n + "'"
cur.execute(command)
except KeyError:
duplicates.append(n)
except mdb.Error,e:
print "Error %d: %s" % (e.args[0],e.args[1])
sys.exit(1)
finally:
if con:
con.close()
每个打印出现大约需要2-3秒,这使我认为更新执行正在缓慢进行.我有很多要更新的值,这不应该是它应该执行的速度(假设我能够快速打印出d [n]的所有值)
反正加速更新了吗?
编辑:数据库正在使用InnoDB引擎
最佳答案
根据你的描述,每次打印花费2~3秒,所以我认为问题可能是这样的:
原文链接:https://www.f2er.com/mysql/433639.html>您的表文件的fil_name列是否已编入索引?
> make auto_commit为true,每次更新都是提交的事务.
如果大小写为1,则只需在该列上创建索引,不要在更新时进行表扫描.
如果案例是2,@ dave给出了一个很好的答案.