When should I use the XML data type in Microsoft SQL Server?
|
migrated from stackoverflow.com Mar 4 '11 at 15:39
|
I have used XML type fields mostly for logging purposes related to ASMX web services or WCF services. You can save the request or the response messages. Most of the times, you save the XML in the DB for reference purposes - not for your regular business activity (not to query it every 5 seconds from the code). If you find yourself doing so, determine the fields that you usually query or use most of the time and create separate columns for them. That way, when you are saving the XML to the DB, you can extract these fields and save them to their corresponding columns. Later on while retrieving them you wouldn't need to query the XML document you can just use the columns you created for this purpose. |
||||
|
|
|
I've only encountered a few scenarios where I would actively pursue the use of the XML data type in SQL Server. This is off the top of my head, in order from least to most interesting:
All that said, 99% of the time, I have absolutely no need for an XML field when designing a schema. |
|||
|
|
|
I've done a similar thing where I serialize object data to XML for storage. This removes the burden of having tables, columns, and relationships in place to hold data for complex objects. The process can be helped by using a schema that the result XML conforms to, and the database tables can be used for meta information about the objects in question. In my case, expanding database schema to accommodate the object layout would be a big task! |
|||
|
|
|
Personally, I've never used the XML field type in SQL Server before, and I've done a lot of database programming since they added this feature. There always seemed to me to be a programmatic and performance overhead to using XML content in a database natively. I honestly thought it was a gimmick, because everyone was on the XML train at one point in the early 2000's. "Oh XML is hot, so let's add it to SQL Server so we don't look passe." The best business case I can see might be in recording XML-based data messages you've received where you might want to peek in their structure and data, but want maintain integrity of original message for business or legal reasons. The overhead might be too great to translate the XML into a table structure, but using the XML capabilities in SQL Server might just reduce the cost of examining the data enough to warrant using it. There's probably dozens of other reasons why you'd want to use it, but frankly I think you should consider other paths to happiness before going hog-wild with XML in SQL Server unless you have very specific business reasons for doing so. Use a varchar(max) or text field if it makes more sense. Just my opinion, of course :) |
|||
|
|
|
These are the scenarios that come to mind immiediately at a whim when considering what conditions would need to exist to permit Xml fields in Sql Server:
|
|||
|
|
|
The few times that I've seen the XML data type in SQL server it's basically been used as a field that has been queried just like any other in the database, which can have some very bad performance implications if you have a large amount of data. There's a reason it's called SQL server and not XML server. :-) The queries that go against the XML are simply not as fast as a doing regular select query. I could see it being used to store XML that isn't queried that much though, say storing the full XML document in a XML data type, but having the searchable fields(keys) stored separately with the XML document. That way you can query your information faster and pull up the XML document when you get to the point where you want to see the full document. I've actually had to go back and do this with a number apps where people have tried to use the XML data type as a heavily queried field and the data has grown to the point where the query has gotten too slow. |
|||
|
|
