Using correct threshold counter. 

Avoids problems such as "MySQL server has gone away" if too much data has to be written at the end.