I'm trying to take the results from a very large mysql query and write them to a file in Python, but I keep running into memory overload errors. I've easily climbed up to 60+ GB ram utilization until I run out of memory and it crashes. How do I do this effectively?
Here's my code snippet:
with connection.cursor() as cursor:
cursor.execute(my_query)
row = cursor.fetchone()
while row is not None:
with open(my_file,'a+') as f:
f.writelines(row)
row = cursor.fetchone()
This still overloads my memory even though I would have thought it would only have one line at a time in memory.
I've also tried using f.flush() at every line, as well as reopening the file and then running f.close() on every line.
Thank you so much!!