wowprompt

Large CSV export streamed from a database without running out of memory

Writes an export endpoint or job that streams rows from the database straight into a CSV download in chunks, so exports of hundreds of thousands of rows finish without timeouts or memory errors, with safe headers, filters and Excel-friendly output. Use it for admin panels and reports where "Export to CSV" crashes or hangs on real data.

Export code that reads rows in batches and writes them to the response as they arrive, with headers, filters, formula-injection protection and a background job option
The output this prompt produced, unedited.

The prompt

Fill in these first

  • Language, framework and database: [e.g. PHP 8.3 with PDO and MySQL, or Python with FastAPI and PostgreSQL]
  • Table or query to export: [paste the query or table and columns]
  • Filters users can choose: [e.g. date range, status]
  • Expected size: [e.g. up to 500,000 rows]
  • Who may export: [e.g. admin role only]
  • Hosting limits: [e.g. 60-second request timeout, 256 MB memory]

Task

Write an export that uses steady, low memory however many rows there are.

Output format

  1. The endpoint code with comments: permission check, filter validation, headers, then a loop that reads a batch, writes it, and flushes.
  2. Keyset pagination for the batches (WHERE id > last id) rather than OFFSET, with the reason in one line.
  3. CSV writing with a UTF-8 byte order mark so Excel opens Indian language text correctly.
  4. Protection against formula injection: cells starting with =, +, - or @ are prefixed.
  5. A background job version for exports that would pass the request timeout: create the file, notify, and give an expiring download link.
  6. A test plan: empty result, one row, a very large result, and special characters.

Rules

  • Prepared statements for every filter; no string-built SQL.
  • Never load the full result into an array.
  • Log who exported what and when, without logging the data.
csv exportstream large csvdatabase exportmemory efficient exportadmin report downloadexcel friendly csv

How to run this

Copy the prompt and paste it into Gemini, then set the aspect ratio to 3:4. No reference photo is needed for this one.

The prompt is written in Markdown with each part under its own heading, which is what the copy action hands over. Change one section at a time, keeping the rest intact, so you can tell which change did the work.

Related prompts

All productivity