How to avoid OOM (Out of memory) error when retrieving all records from huge table?

前端 未结 6 1404
甜味超标
甜味超标 2020-12-14 19:13

I am given a task to convert a huge table to custom XML file. I will be using Java for this job.

If I simply issue a \"SELECT * FROM customer\", it may return huge a

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-14 19:35

    At which stage is the OOM error occurring, is it on data retrieval or processing data to XML file?

    If its data retrieval, get the data in batches. Get the total number of rows first, order the selects by the primary key and limit the rows selected to chewable sizes.

    If its at creating the XML file, send the XML node of each customer to System.out.println, don't hold it in memory. Launch the program via commad line and redirect all output to a file;

    java MyConverter > results.txt
    

    As you loop through the record all is saved in the file.

提交回复
热议问题