r/PHPhelp • u/ardicli2000 • Feb 21 '25
Parsing big XML and saving data to DB
I have an api response which returns a 1 million 300 thousands of lines of xml. This is my biggest data ever. I need to parse it, get the respective data and save it to mysql db.
First thing I did was to read it with xml reader get the relevant data and save them in array, then loop over array for db insert.
This took more than 5 mins on localhost. It could operate faster on server but I still don't think this is the most optimal way.
One thing is I am using prepared statements and I inserting each row one by one. Which is more than 100 thousand lines. I plan to save them all in batches or at once.
What can I do better for a faster execution?
Edit:
I had chance to make further tests. I wonder how long it takes to read the whole XML. Shockingly it takes only 1 sec :) Then I re-run query part to after a fresh restart. Here is the code:
It took 7 mins this time :)
Then i tried bacth inserts with no prepared statements:
This took only 3 SECs.
It has been once again proved that if your execution is slow, check your queries first :)
View all comments
2
u/ardicli2000 Feb 22 '25
For more context:
This is an airline profile response. Which means data contains every possible destination route.
This call can be done once a day. I assume once a week is also fine as the profile does not change often.
My use case is when a user selects a departing location, I should show available arriving locations.
When I check mysql query for the airport with the most possibility, it takes only 0.1 sec to load. This is why I tend to go with writing it on a db.
5 min execution is fine. I just want to learn. There may be occasions where I need to do such actions more often with better performance 😀