方法1
一条一条更新,耗时长,效率低
最笨的方法 ,5000条数据, 耗时2.1s
方法2
用数据库方法,执行更新,耗时低
原生数据库方法, 5000条数据, 耗时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条数据, 耗时0.83s
一次执行多条更新,耗时相对低
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)
结论:
还是 sql 原生方法最快,但写起来比较麻烦
网友评论