我要載入如下所示的csv檔案:
acct. no.,1-15 days,16-30 days,31-60 days,61-90 days,91-120 days,beyond 120 days
2314134101,898.89,8372.16,5584.23,7744.41,9846.54,2896.25
2414134128,5457.61,7488.26,9594.02,6234.78,273.7,2356.13
2513918869,2059.59,7578.59,9395.51,7159.15,5827.48,3041.62
1687950783,4846.85,8364.22,9892.55,7213.45,8815.33,7603.4
2764856043,5250.11,9946.49,8042.03,6058.64,9194.78,8296.2
2865446086,596.22,7670.04,8564.08,3263.85,9662.46,7027.22
,4725.99,1336.24,9356.03,1572.81,4942.11,6088.94
,8248.47,956.81,8713.06,2589.14,5316.68,1543.67
,538.22,1473.91,3292.09,6843.89,2687.07,9808.05
,9885.85,2730.72,6876,8024.47,1196.87,1655.29
但是如果你注意到了,有些欄位是不完整的。我想mysql只會跳過第一列缺失的那一行。當我執行命令時:
load data local infile 'test-long.csv' replace into table accounts
fields terminated by ',' lines terminated by '\r\n'
ignore 1 lines
(cf_535, cf_580, cf_568, cf_569, cf_571, cf_572);
mysql輸出為:
query ok, 41898 rows affected, 20948 warnings (0.78 sec)
records: 20949 deleted: 20949 skipped: 0 warnings: 20948
行數只有20949行,但mysql報告它受影響的行數為41898行。為什麼?而且,表中沒有真正的變化。我也看不到生成的警告是關於什麼的。我想使用loaddatainfile,因為更新每一行需要半秒鐘的時間,對於乙個記錄超過20000條的檔案,這一時間轉換為2.77小時。
更新:修改**以將auto-commit設定為「false」,並新增了db.commit()語句:
# tell mysqldb to turn off auto-commit
db.autocommit(false)
# set count to 1
count = 1
while count < len(contents):
if contents[count][0] != '':
cursor.execute("""
update accounts set cf_580 = %s, cf_568 = %s, cf_569 = %s, cf_571 = %s, cf_572 = %s
where cf_535 = %s""" % (contents[count][1], contents[count][2], contents[count][3], contents[count][4], contents[count][5], contents[count][0]))
count += 1
try:
db.commit()
except:
db.rollback()
mysql思考7 關於Uber選擇MySQL的思考
在資料庫圈子,大家都知道今年uber幹出來一件大事件,把postgresql切換到了mysql,當時社群裡一陣喧嘩。事情已經過去半年多了,這裡我不想去和大家再次討論這兩個關係型資料庫那個更好。只是想帶著大家思考一下選擇的背後。在該事件中,uber提出來遷移的乙個重要原因是 在大量更新的業務場景下po...
Mysq篇 了解Mysql(一)
in關鍵字和exists關鍵字select from a where a.idin select id from b 對外表a使用索引效率高,建議a為大表。select from a whereexists select from b where a.id b.id 對內表b使用索引效率高,建議b為...
mysql熱資料載入管理
5.6版本之後,提供了乙個新特性來快速預熱buffer pool緩衝池.在my.cnf裡面加入幾個引數 innodb buffer pool dump at shutdown 1 在關閉資料庫時把熱資料dump到本地磁碟 innodb buffer pool dump now 1 採用手工方式把熱資...