我有下面的python脚本,它逐行读取文件并执行mysql更新查询。它非常慢,每个查询似乎需要超过1秒。知道为什么这么慢吗?
with open(fname) as f:
for line in f:
line = line.rstrip()
email, name = line.split(':')[0], line.split(':')[-1]
try:
cursor.execute("UPDATE user SET name=%s WHERE email=%s", (name, email))
except mariadb.Error as error:
print("Error: {}".format(error))发布于 2015-10-21 11:52:47
您应该能够通过使用索引来解决性能问题:
create index idx_user_email on user(email);1秒的更新是很长的时间。
发布于 2015-10-21 12:03:44
您可以尝试使用多个线程。
import threading
with open(fname) as f:
for line in f:
line = line.rstrip()
email, name = line.split(':')[0], line.split(':')[-1]
thread = threading.Thread(target=updateUser, args=[name, email] )
thread.start()
def updateUser(name, email):
try:
cursor.execute("UPDATE user SET name=%s WHERE email=%s", (name, email))
except mariadb.Error as error:
print("Error: {}".format(error))它可以避免在查询数据库时浪费时间。因此,当数据库正在处理查询时,您的程序将准备另一个查询。
发布于 2015-10-21 12:54:58
如果你想疯狂的话,你也可以关掉日记:
db= sqlite3.connect('database.db')
cursor = db.cursor()
cursor.execute("PRAGMA synchronous = OFF")
cursor.execute("PRAGMA journal_mode = OFF")https://stackoverflow.com/questions/33258498
复制相似问题