簡體   English   中英

使用python將大量數據插入MySQL表的有效方法是什么?

[英]What is an efficient way to insert large amounts of data into a MySQL table using python?

我有一個大型數據庫,其中包含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()

如前所述,MySQL轉儲是一種選擇。 如果仍然要使用python,則應使用cursor.executemany(operation,seq_of_params) ,這是一種將大量值插入表的更有效的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM