方法1
一條一條更新,耗時(shí)長,效率低
最笨的方法 ,5000條數(shù)據(jù), 耗時(shí)2.1s
方法2
用數(shù)據(jù)庫方法,執(zhí)行更新,耗時(shí)低
原生數(shù)據(jù)庫方法, 5000條數(shù)據(jù), 耗時(shí)0.27s
UPDATE tableName
SET orderId = CASE id
WHEN 1 THEN 3
WHEN 2 THEN 4
WHEN 3 THEN 5
END
title = CASE id
WHEN 1 THEN 'New Title 1'
WHEN 2 THEN 'New Title 2'
WHEN 3 THEN 'New Title 3'
END
WHERE id IN (1,2,3)
***更新操作一定要帶上 where_in,不然就等著跑路吧***
方法3
python自帶方法 , 5000條數(shù)據(jù), 耗時(shí)0.83s
一次執(zhí)行多條更新,耗時(shí)相對低
sql = '''update mcn_article set cover_pics = %s where id = %s '''
args = [('https://asdf.png', 0),('https://asdf.png', 1),('https://hhh.png', 2)]
mydb.cursor.executemany(sql, args)
結(jié)論:
還是 sql 原生方法最快,但寫起來比較麻煩