MySQLdb批量插入数据
日期:2012-05-01 |
来源:未知 |
作者:redice |
523 人围观 |
0 人鼓掌了!
测试了一下MySQLdb的executemany()方法,速度是惊人的:10分钟内插入了将近100万的数据。赶快来做个标记!
测试环境:Amazon RDS(Amazon Relational Database Service),数据表中原本有1900万条数据,测试完毕后,数据条数如下:
而之前我用execute()逐条插入同样多的数据竟然花了一个多周!!!executemany()的用法如下:
- cursor.executemany(
- """INSERT INTO breakfast (name, spam, eggs, sausage, price)
- VALUES (%s, %s, %s, %s, %s)""",
- [
- ("Spam and Sausage Lover's Plate", 5, 1, 8, 7.95 ),
- ("Not So Much Spam Plate", 3, 2, 0, 3.95 ),
- ("Don't Wany ANY SPAM! Plate", 0, 4, 3, 5.95 )
- ] )
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