Writing records line by line to avoid memory overload