Sobes.tech
Senior

How to efficiently unload a large volume of data from a database into memory, generate a file, and serve it to the frontend? Can compression be used?

sobes.tech AI

Answer from AI

To efficiently unload a large volume of data from a database without overloading memory, it is advisable to use streaming or pagination, that is, load data in batches and write it directly to a file without holding the entire volume in memory.

An example approach:

  • Use a cursor or pagination to fetch data in parts.
  • Write each batch directly to a file (e.g., CSV or JSON).
  • Use compression on the fly if possible, such as gzip, to reduce file size and decrease network load when serving to the frontend.

Yes, compression can and should be used, especially for large files. It will reduce traffic and speed up data transfer. For example, you can create a gzip archive during the file writing process without saving an uncompressed version to disk.

Here's an example in Python using a generator and gzip:

import gzip
import json

def fetch_data_in_batches(cursor, batch_size=1000):
    while True:
        batch = cursor.fetchmany(batch_size)
        if not batch:
            break
        yield batch

with gzip.open('output.json.gz', 'wt', encoding='utf-8') as f:
    for batch in fetch_data_in_batches(db_cursor):
        for record in batch:
            f.write(json.dumps(record) + '\n')

Thus, memory is used efficiently, and the file is created immediately in compressed form.

How to efficiently unload a large volume of data from… - sobes.tech