Home > SQL Server Tips > Database Management and Administration > XML data type in SQL Server 2005 vs. VARCHAR (MAX)
SQL Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

DATABASE MANAGEMENT AND ADMINISTRATION

XML data type in SQL Server 2005 vs. VARCHAR (MAX)


Michelle Gutzait
01.28.2008
Rating: -4.33- (out of 5)


Expert advice on database administration
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


As a database administrator, I tend to look closely at performance issues and how to make sure the use of XML does not affect SQL Server performance. In this tip, I'll take you through an example using two tables, one inserted and queried with XML and the other with the VARCHAR (MAX) data type. Take a look at the storage, CPU and I/O results and make the best choice for your SQL Server environment.

Note: The tests used here use only base tables, with no indexes.

If you're interested in a comparison of performance implications using T-SQL commands and XML AUTO in SQL Server, check out my previous tip.

The XML data type
The XML data type logically differs from the VARCHAR (MAX) in the checks done by SQL Server to make sure the content is indeed valid XML.

Testing environment description
For my testing, I will use XML I copied from an *.rdl file (Reporting Services Report), which is 265KB long. I will create two tables with the same structure, except one table uses XML data type and the other uses VARCHAR (MAX) data type for the MyXML field:

id int identity not null,
MyXML VARCHAR(MAX) null
)
go

create table TryXMLDatatype (
id int identity not null,
MyXML XML null
) Go

I will insert the XML data the same way to each table:

set statistics io on
Go
declare @XML XML

-- My big XML (for space reasons I am not including all of it here) SET @XML = '<?xml version="1.0" encoding="utf-8"?>
………
'
insert into TryXMLDatatype (MyXML) values (@XML)
go

declare @Varch VARCHAR(MAX)

-- My big XML (for space reasons I am not including all of it here)
SET @Varch = '<?xml version="1.0" encoding="utf-8"?>
………
'
insert into TryXMLDatatype (MyXML) values (@Varch)
Go

Insert XML data into the tables
I monitored the above insert commands in SQL Profiler and with statistics I/O, running them twice.

Statistics I/O results:
Table 'TryXMLDatatype'. Scan count 0, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 18, lob physical reads 0, lob read-ahead reads 0.

Table 'TryVACRCHARDatatype'. Scan count 0, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 90, lob physical reads 0, lob read-ahead reads 0.

Execution plans show the exact same plan for both commands.

Profiler results (two executions of each insert):

[IMAGE]

Note: The XML insertion takes more CPU but less reads and writes. Also, inserting the XML data type has more duration. I will analyze this behavior in the next section.

Querying the tables
I ran SELECT * FROM in each ...


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google



RELATED CONTENT
XML in SQL Server
Processing XML files with SQL Server functions
Top 10 SQL Server development tips of 2008
Manage traces in SQL Server 2005 Analysis Services with XMLA commands
Create a computed column in SQL Server using XML data
Create DDL table in SQL Server 2005 to audit DDL trigger activity
Retrieve XML data values with XQuery in SQL Server 2005
T-SQL commands vs. XML AUTO in SQL Server
Basic Transact-SQL programming constructs: 15 tips, 15 minutes
T-SQL identifiers
Character string data types

Microsoft SQL Server Performance Monitoring and Tuning
Using traces in SQL Server Profiler
SQL Server Mailbag: CALs, witnesses and unwanted changes
SQL Server Mailbag: Data restoration and DB property management
Working with IntelliSense in SQL Server 2008 Management Studio
SQL Server Mailbag: Stored procedures, triggers and SSRS reports
Troubleshooting Distributed Transaction Coordinator errors in SQL Server
Clearing the Windows page file and its effect on server performance
Optimizing SQL Server indexes –- even when they're not your indexes
Performance implications of transaction log autogrowth in SQL Server
The short course on how SQL Server really works

Microsoft SQL Server 2005
End of life comes for SQL Server 2005 SP2, 2008
SQL Server Reporting Services Fast Guide
SQL Server Service Broker Tutorial and Reference Guide
Tips for tuning SQL Server 2005 to improve reporting performance
SQL Server consolidation: Why it's an optimization technique
Parent-child dimensions in SQL Server 2005 with Analysis Services MDX
Enforcing data integrity in a SQL Server database
SSIS error message due to installation problem on SQL Server 2005
Should you upgrade to SQL Server 2005 or SQL Server 2008?
Basics for working with DATETIME and SMALLDATETIME in SQL Server 2005
Microsoft SQL Server 2005 Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
Commerce XML  (SearchSQLServer.com)
DSTP  (SearchSQLServer.com)
XQuery  (SearchSQLServer.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


table and monitored with Profiler and Statistics I/O:

SELECT * FROM TryXMLDatatype
Go
SELECT * FROM TryVACRCHARDatatype
Go

Results:

[IMAGE]

Note: The values in the fields are different!

Statistics I/O results:

Table 'TryXMLDatatype'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 56, lob physical reads 0, lob read-ahead reads 18. Table 'TryVACRCHARDatatype'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 200, lob physical reads 0, lob read-ahead reads 0.

Less I/O is issued with the XML data type.

Again, execution plans show the exact same plan for both commands.

[IMAGE]

Why is there such a difference between the reads of the two tables?

Note: The duration of extracting the XML is higher. The duration is subjected to many different factors depending on the activity on the machine. I'll ignore this difference, mainly because no CPU is shown. If CPU activity was involved, it might explain the difference in duration, but this is not the case.

Let's query the length of the columns in each table:

SELECT datalength(MyXML) FROM TryXMLDatatype
Go
SELECT datalength(MyXML) FROM TryVACRCHARDatatype
Go

[IMAGE]

Surprisingly, the XML field has far fewer characters than the other. Here is the explanation:

XML -- less I/O:

When inserting the XML data type, the "extra" data such as the " and tabs are removed from the field too. The result is a much more economical storage.

I tried to copy the value from the XML column to the VARCHAR(MAX) column as follows:

truncate table TryVARCHARDatatype
Go
insert into TryVARCHARDatatype (MyXML)
select convert(varchar(max),MyXML) from TryXMLDatatype
Go

The result:

[IMAGE]

Looks the same, but…

SELECT datalength(MyXML) FROM TryXMLDatatype
Go
SELECT datalength(MyXML) FROM TryVACRCHARDatatype
Go

[IMAGE]

This is better than before (136,268 instead of 271,210), but still not the same. If I convert the XML to a VARCHAR(MAX) and calculate the length:

SELECT
datalength(convert(varchar(max),MyXML)) FROM TryXMLDatatype
go
SELECT datalength(MyXML)FROM TryVARCHARDatatype
Go

[IMAGE]

Same length.
This means that the XML datatype is stored in a more efficient way. And now that both tables contain the same data, let's view the Profiler trace of the select * from both tables (three executions):

[IMAGE]

There are about 20% fewer reads for the XML datatype.

The statistics I/O:

Table 'TryXMLDatatype'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 56, lob physical reads 0, lob read-ahead reads 18.

Table 'TryVARCHARDatatype'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 100, lob physical reads 0, lob read-ahead reads 0.

Again, it shows a more efficient read operation with the XML datatype.

What about querying the XML converted to VARCHAR(MAX):

SELECT convert(varchar(max),MyXML) FROM TryXMLDatatype
Go
SELECT MyXML FROM TryVARCHARDatatype
Go

[IMAGE]

Table 'TryXMLDatatype'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 20, lob physical reads 0, lob read-ahead reads 9.

Table 'TryVARCHARDatatype'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 100, lob physical reads 0, lob read-ahead reads 0.

The only metric with a constant behavior is the reads I/O. Furthermore, it is more likely for the XML convertion query to use CPU for the conversion -- that's normal.

Conclusion
The XML data type is meant to store pure XML data, excluding unnecessary headers and trailing characters. This results in a more cost-effective storage by means of I/O, yet there is still the CPU cost for checking that the data is valid XML.

ABOUT THE AUTHOR:   

[IMAGE]Michelle Gutzait works as a principal database consultant for Victrix, a Montreal-based IT consulting firm specializing in information security, infrastructure, communication networks and applicative solutions and collaboration. Gutzait has been involved in IT for 20 years as a developer, business analyst and database consultant. For the past 10 years, she has worked exclusively with SQL Server, including SQL infrastructure and database design, performance tuning, security, high availability, disaster recovery, VLDBs, replication, T-SQL coding, DTS/SSIS packages, administrative and infrastructure tools development and reporting services.
Copyright 2008 TechTarget


Rate this Tip
To rate tips, you must be a member of SearchSQLServer.com.
Register now to start rating these tips. Log in if you are already a member.




DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



SQL Server Development - .NET, C#, T-SQL, Visual Basic
HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts