Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cirecodeur's avatar

speed up the insertion of Excel data in Mysql with PHP/laravel

Hi everyone, I import Excel data into a MySQL database with PHP/Laravel. The problem is that it takes time. With a file of 55MB (+500,000 lines) the work is still in progress. Using queues, some tasks fail.Knowing that I want to import more than 10GB of data, how do I optimize it? How to harness the full power of processors? I tell myself when I see the activity of the processors that not all of them are used. NB: I have more than 180GB of Ram and 50-60 CPU. Thank you for the time you will devote to this post.

0 likes
10 replies
vincent15000's avatar

Hello ... well ... you speak about 180 Gb RAM and 50-60 CPU ... and on the other side a little database (55 Mb / 500 000 lines). I don't understand why you would need so much resources for a little database. I have already imported such files in my database, yes it takes time, and I have only 4 Gb RAM and 2 CPU ;).

Can you be more precise about the performances you need ?

cirecodeur's avatar

@vincent15000hello, thank you! some files contain 500,000 LINES (55MB), others more than 1million lines (50-60MB. I want to reduce the insertion time.

1 like
vincent15000's avatar

@cirecodeur I'm not sure that the answer is in Laravel. Perhaps you need some specific material architecture to dispatch the load. Have you thought about AWS ?

1 like
cirecodeur's avatar

@vincent15000 No, Actually, no. I thought if I could distribute the load on different processors it could be done. Multithreaded in php

Sinnbeck's avatar

You can start by using this package, as it should be pretty optimized to work with large files: https://github.com/spatie/simple-excel

You can use multiple processes perhaps (you would need to implement it yourself using spatie/async or similar) but my guess would, that you database would be a bottleneck

2 likes
cirecodeur's avatar

@Sinnbeck THANK your answer. I used maatwebsite/excel package. My real problem is to work on reducing import time. I'm supposed to be working more on reducing import time.

cirecodeur's avatar

@Tray2 hello, It's something regular for end user. they can upload more than 1GB of Excel(csv, xlx ...) files and the system will import to database. 1 file = 1 table

Tray2's avatar

@cirecodeur Then I would force them to upload CSV and then handle it all with a sheduled cron job, I would remove laravel from the ecuation.

Snapey's avatar

doing it through the framework will always be slower than bulk importing into sql - especially so if you need to check if the record already exists

a single php process will never take advantage of your processors. The amount of ram is probably irrelevant.

some ideas

  • correct but minimal indexing on the database
  • split the import into chunks (eg 10000 lines) and create one queued job per chunk
  • run multiple queue workers
  • if possible use upserts
  • don't process the rows at all if you can help it
2 likes

Please or to participate in this conversation.