MySQLdb

fedor nobody at here.com
Thu Jan 27 15:34:28 EST 2005


Hi Daniel,

You should probably take a look at the executemany method of the cursor. 
Your insert times might drop by a factor 20 . Here's an example.

Cheers,

Fedor

import time
import MySQLdb


db=MySQLdb.Connect(user="me",passwd="my password",db="test")
c=db.cursor()
n=0
tic=time.time()
for i in range(3000):
     n+=c.execute('INSERT INTO testtable VALUES (%s)', (i,))
toc=time.time()
t1=toc-tic
print 'separate sql statements: %s, inserted %s records' % (t1,n)



tic=time.time()
n=c.executemany('INSERT INTO testtable VALUES (%s)',  [(i,) for i in 
range(3000)])
toc=time.time()
t2=toc-tic
print 'all at once %s inserted %s records' % (t2,n)

OUTPUT>>>
separate sql statements: 0.571248054504, inserted 3000 records
all at once 0.0253219604492 inserted 3000 records





More information about the Python-list mailing list