I need to extract a big amount of data(>1GB) from a database to a csv file. I'm using this script:
rs_cursor = rs_db.cursor()
rs_cursor.execute("""SELECT %(sql_fields)s
FROM table1""" % {"sql_fields": sql_fields})
sqlData = rs_cursor.fetchall()
rs_cursor.close()
c = csv.writer(open(filename, "wb"))
c.writerow(headers)
for row in sqlData:
c.writerow(row)
The problem comes when is writing the file the system runs out of memory. In this case, is there any other and more efficient way to create a large csv file?
sqlData, not the fact the you write this data to a file. Where does this data come from? Do you have any control over it? if you do, you should be looking into reading it in chunks or as a generator.pymssqlyou can usefetchmanywith thesizeargument so it doesn't return the whole table at once, see its docs You can also consider usingWHEREin order toSELECTfrom the table in chunks.