MySQLdb批量插入数据

MySQLdb批量插入数据_redice's Blog

MySQLdb批量插入数据

测试了一下MySQLdb的executemany()方法,速度是惊人的:10分钟内插入了将近100万的数据。赶快来做个标记!

测试环境:Amazon RDS(Amazon Relational Database Service),数据表中原本有1900万条数据,测试完毕后,数据条数如下:

 
而之前我用execute()逐条插入同样多的数据竟然花了一个多周!!!

executemany()的用法如下:

  1. cursor.executemany(  
  2.       """INSERT INTO breakfast (name, spam, eggs, sausage, price) 
  3.       VALUES (%s, %s, %s, %s, %s)""",  
  4.       [  
  5.       ("Spam and Sausage Lover's Plate"5187.95 ),  
  6.       ("Not So Much Spam Plate"3203.95 ),  
  7.       ("Don't Wany ANY SPAM! Plate"0435.95 )  
  8.       ] )  

http://mysql-python.sourceforge.net/MySQLdb.html

1.2.3版的MySQL-python存在bug,"values"要小写才有效,详情见下面内容:

http://stackoverflow.com/questions/3945642/why-is-executemany-slow-in-python-mysqldb

原文地址:https://www.cnblogs.com/lexus/p/2725674.html