Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to store XML data in a MySQL database, with some specific requirements

I am receiving XML data from a service. The test data I am receiving back has about 300 XML nodes, clearly far too many to create individual rows for in a MySQL database.

The problem is that we ideally need to store all the data, and we will probably need to reference the data again at some point in the future - we can't just process through it once and delete the XML string.

What's the best way of storing this data in a MySQL database?

I have forecast that at the predicted rate within a few months, if we were to store the raw XML data in TEXT format, the database could grow to around 500MB. In the long run this feels impractical.

like image 998
Jack Avatar asked Aug 10 '11 10:08

Jack


People also ask

Can you store XML in MySQL?

MySQL doesn't support XML data types like Microsoft SQL Server or PostgreSQL. Many times it is required to store multiple nodes of data into a single column, in the form of XML. In the MySQL, You have to store XML in CLOB (Character Large Object) data type.

Can you store XML in database?

"You can store XML in a database designed specifically for XML, in a modified object database, or in a relational database."

How can we transfer XML data to MySQL table?

Loading XML file: We will use simplexml_load_file() function to convert the well-formed XML document into the given file to an object.

Can we use XML and MySQL together?

The most common way to store XML in MySQL is to use the LOAD_FILE() function to open an entire XML document, store it in a variable, and insert the variable into a table column.


2 Answers

You could create a blob column (i.e. mediumtext column). Instead of inserting XML purely as strings in the DB, you could zip the XML, then store in MySQL.

When you read from MySQL, you unzip it again. Since XML is text you'll get very high compression rates (close to 80% compression). The thought process being, disk IO takes a lot longer time than compression/un-compression which is predominantly Processor bound.

The downside being you will no longer be able to query or do full text search using SQL....

like image 155
Srikar Appalaraju Avatar answered Oct 18 '22 22:10

Srikar Appalaraju


The best way is to not store XML in the DB, but I have history with that particular issue.

Just store it as TEXT. 500 MB is nothing for MySql, especially with TEXT datatypes, since those aren't stored in the row buffer.

like image 38
John Green Avatar answered Oct 18 '22 21:10

John Green