orm.Article.objects.iterator()
Does this query execute the entire query and store the result in memory? Or select rows from the database one at a time?
I guess all at once. See if you can replace this loop with a database cursor that supplies data incrementally:
for example: http://docs.python.org/library/sqlite3.html#sqlite3.Cursor.fetchmany
db = blah.connect("host='%s' dbname='%s' user='%s' password='%s'" % ... new, old = db.cursor(), db.cursor() old.execute(""" SELECT * FROM whatever """) for row in old.fetchmany(size=500): (col1, col2, col3...) = row new = db.cursor() new.execute(""" INSERT INTO yourtable ( col1, col2, col3...) VALUES ( %s, %s, %s, %s, %s) """,(col1, col2, col3,...)) new.close() old.close()
It will be slow. I pulled this from an offline migration script from mine, therefore ymmv.
fetchmany is standard (PEP249). I havenโt done exactly what you are looking for, so thereโs still a bit of work to follow on this pattern: I donโt get hung up on the loop โ to get a set of 500 to the end โ so you need to do this for yourself.
source share