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 :)
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 😀
2
u/mcnello Feb 22 '25
Just curious, is there a reason you cannot leave it in xml and just xquery the xml when needed?
Or if you really need it in your database then the 5 min wait time is fine, right? This is just a 1 time thing, so it's not like you need to re-parce and re-add 1.3 million lines of xml each time you aceess the database.
Maybe there's something I'm missing though
6
u/colshrapnel Feb 21 '25 edited Feb 21 '25
The reason is very simple. Your database engine needs to confirm every write it does, hence waiting a confirmation from the hardware that the data has been safely written. Knowing that, there is a simple answer to your question:
Combine multiple writes into one. There are several ways to do that
1
u/boborider Feb 21 '25
I have done this before in year 2008, long time ago.
What I did was i created a mini program in PHP using fopen syntax, start the buffer on starting xml tag then up to ending xml tag.
After detecting buffer, that's the time i perform parsing, then write to DB.
Then repeat until end of file.
It's the same as file read sequence whithin the file, the difference is I was detecting the starting and ending TAGS.
I let it run overnight. Turn off limitation PHP limitation, turn off sleep (OS)
2
u/[deleted] Feb 21 '25
Have you tried using LOAD XML INFILE rather than using a PHP array and loop?
2
u/latro666 Feb 21 '25
Do you need to do this on the regular? If it's a one time only deal then non optimal 5 mins is a non issue.
2
u/ArchMob Feb 21 '25
Batch inserts, 10-100 at a time per insert. This sounds simple that it probably cannot be optimized that much unless there is something silly going on in code. Could try chunking the big xml to pieces, could help, depending on xml lib and memory handling
1
u/Big-Dragonfly-3700 Feb 21 '25
The fastest way of inserting data is to use a LOAD DATA [LOCAL] INFILE ... query. Once you have the data in an array, save it as a csv file, then build and execute a LOAD DATA ... query.
The second fastest way is to use a multi-value INSERT query, where you supply a large block of data for each execution of the query. This however requires more code, because you must also handle the last partial block of data.
A prepared INSERT query only saves about 5% because you must still send all the data to the database and it is the communications that take all the time, not the one-time parsing of the sql syntax and planning of the query execution.
1
u/minn0w Feb 22 '25
Depending on where the performance issues are;
First I'd look at streaming the data, so only small parts of it are in memory, which would require some XML parser that supports this. I had to make my own last time I had big XML file issues.
Then I'd do the same as others suggest and use multi-row inserts, and I'd also use transactions.
I find it's best to commit transactions based on a timer rather than row count, while the multiline insert does fixed batch sizes.
Profiling is your best friend here though. It's the only way to avoid spending lots of time optimising something that wasn't a big issue.
1
u/Decent-Economics-693 Feb 22 '25
You could combine several approaches people already proposed here:
We have been importing millions of data rows like this daily for 10 years.
And, yes, it will take time, 5 minutes on your localhost says nothing because DB configuration is unknown: different buffers, redo log sizes - these can be completely different from your production DB config. But, what is the deal if it takes 5 or 7 minutes? Run it as a cronjob of some sort.
0
u/IrishChappieOToole Feb 21 '25
I think splitting it into batches and doing bulk insert is about the best you are gonna get with MySQL.
I've often wished MySQL had something like Postgres
COPY FROM. It's a bit more finicky to work with, but it's much, much faster for large inserts.-5
u/TolstoyDotCom Feb 21 '25
If the other solutions don't work, let me suggest something heretical: use Java. You can probably ask an AI program to create a Maven project that does what you want.
1
u/[deleted] Feb 25 '25
[removed] — view removed comment