r/PHPhelp • u/Saitama2042 • Jul 24 '25
Need help to export large Data
Hi,
For one of our applications, we need to export large amounts of data from DB to a CSV file. About 2.5 M records in the DB table. At the csv file we need to dump 27 columns. We need to join about 7 tables.
We will use a job for exporting and after completion we shall send email notification to the user.
We are using Laravel 10 , my question is - can a CSV file hold 2.5M data? Even dump them, will the file be open able?
What will be the appropriate solution?
Thanks
6
Upvotes
12
u/colshrapnel Jul 24 '25 edited Jul 24 '25
That's a strange question. A file itself can hold 25 or 250M of data. The limitations are from the operation system (like, FAT32 limits file size to
2M (oh my, what a blunder!)4G) and the program that would handle that file (Microsoft Excel is limited to 1M rows).2
u/Big_Tadpole7174 Jul 25 '25
A CSV file can definitely hold 2.5M records, but you're going to run into some practical issues. With 27 columns across that many records, you're probably looking at a file that's 1-3GB in size depending on your data. The bigger problem is that most people won't be able to open it - Excel caps out at around 1 million rows, and even if they use something like LibreOffice, it's going to be painfully slow.
1
u/Just_Information334 Jul 25 '25
What is your DB?
You may want to use native tools depending on which one. For example postgres has COPY to directly write in a file. Mysql has SELECT INTO OUTFILE for the same thing. It requires an access to the DB server and it having some writable folder but it should be the fastest way to generate your csv file.
2
u/A35G_it Jul 24 '25 edited Jul 24 '25
Tested with 3.6M records, exported to CSV, 18 columns, for a total of 440MB of files.
Opened without problems both with Text Editor and with MS Excel (no joke, MS Excel ran out of available rows 🤦♂️🤣)
1
u/pragmaticphpdev Jul 25 '25
There are few softwares like EMEditor available to open and deal with large files.
1
u/[deleted] Jul 25 '25
This is not so much a Laravel problem. Most DB servers have export tools; e.g., pt-archiver (with --no-delete) can export large tables quickly and efficiently.
CSV size is not a problem as long as you have a drive that is large enough. You could also chunk the exported data using the db export tool or something like the "split" utility on Linux systems.
You can also do the same in Laravel by paging and writing x amount of records at a time based on server resources; this might be slow.
1
u/dutchman76 Jul 25 '25
Not everyone opens csv files in Excel, my import code can handle 10x that no problem. So a lot depends on what the file will be used for
0
u/Ivor-Ashe Jul 24 '25
If you’re running this a lot it might make more sense to create a view in the database and check your indexing for the joins.