我有一个包含 10+ 百万行数据的大型数据库。我正在使用 python 脚本将几百万行此数据传输到另一台服务器上的另一个数据库。我正在使用游标对象来获取和插入数据。执行此操作的有效方法是什么?
我现在在做什么:
sql2 = 'INSERT IGNORE INTO user_session(id, record_time, app_id_for_os, user_id, device_id, action) VALUES(%s, %s, %s, %s, %s, %s)'
ts3 = time.time()
for row in readCur:
_id = row['id']
record_time = row['record_time']
app_id_for_os = row['app_id_for_os']
user_id = row['user_id']
device_id = row['device_id']
action = row['action']
writeCur.execute(sql2,(_id, record_time, app_id_for_os, user_id, device_id, action))
cnx2.commit()
最佳答案
如上所述,MySQLdump 是一种选择。 如果你仍然想使用 python,你应该使用 cursor.executemany(operation, seq_of_params) ,这是向表中插入大量值的更有效方法。
关于python - 使用 python 将大量数据插入 MySQL 表的有效方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31998552/