方法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 原生方法最快,但写起来比较麻烦
最后修改时间:2021-09-09 18:14:17
文章转载自sususuki,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




