How do I read a large file from disk to database without running out of memory

后端 未结 6 844
野性不改
野性不改 2021-01-13 14:53

I feel embarrassed to ask this question as I feel like I should already know. However, given I don\'t....I want to know how to read large files from disk to a database witho

6条回答
  •  悲哀的现实
    2021-01-13 15:54

    pseudo code:

    while (!EOF) {
       while (chosenRecords.size() < WRITE_BUFFER_LIST_SIZE) {
          MyRecord record = chooseOrSkipRecord(file.readln());
          if (record != null) {
             chosenRecords.add(record)
          }
       }  
       insertRecords(chosenRecords) // <== writes data and clears the list
    }
    

    WRITE_BUFFER_LIST_SIZE is just a constant that you set... bigger means bigger batches and smaller means smaller batches. A size of 1 is RBAR :).

    If your operation is big enough that failing partway through is a realistic possibility, or if failing partway through could cost someone a non-trivial amount of money, you probably want to also write to a second table the total number of records processed so far from the file (including the ones you skipped) as part of the same transaction so that you can pick up where you left off in the event of partial completion.

提交回复
热议问题