sp_xml_preparedocument VS nodes()

I am considering converting a stored proc which uses sp_xml_preparedocument to a select using the node() syntax. I made two sql scripts, one of each method with identical input xml and ran a query plan on each of them. To my surprise, the Sp_xml_preparedocument seemed to produce the better query plan. The Estimated subtree cost for sp_xml... was 3.36433 for 10000 rows and the estimated subtree cost for nodes() was 421.126 for 60 rows. Am I missing something or is sp_xml_preparedocument still the best method to shred an xml document?

sp_xml code:

DECLARE

@idocINT,

@trans_idint,

@after_docxml,

@record_typeint,

@sm_session_idnvarchar(50),

@trigger_start_timedatetime,

@user_idint,

@fot_serial_idvarchar(50),

@system_idint,

@uudata_delimitervarchar(2),

@retcint

SELECT @after_doc='<ROOT><transaction fot_flag="" fot_serial_id="91bec7f9-edd7-4abb-b912-cef35f753e6b" record_type="1" user_id="6" sm_session_id="a31000ef-fa7f-41c8-8de2-2572c471ed6d" uudata_delimiter="#;" system_id="1"><voice_transaction alias_id="4" trans_size="16453" machine_id="7" comp_code="0" tran_start="2006-04-26T09:11:27" tran_stop="2006-04-26T09:11:44" line_id="1" ext_id="6409" tr_filename="extT8.1_04_26_2006_09;11;27.wav" tr_path="\\IRTXQFNTI001\Recordings\0001" /><screen_transaction alias_id="" trans_size="" comp_code="" tran_start="" tran_stop="" screen_delay="" ip_address="" tr_filename="" tr_path="" /><tx_trigger_info /><tx_classification_info /><cti_transaction_info><cti_transaction userdata="2#;QRecorded= #;QFOTSerialID= #;" eventtime="2006-04-26T09:11:27" party="0" serialid="db606227-44a8-4af2-97a9-e0261b8d256c" ani="7703808051" dnis="6241" event="20101" appearance="0" infoonly="0" number="6409" cause="0" /><cti_transaction userdata="2#;QRecorded= #;QFOTSerialID= #;" eventtime="2006-04-26T09:11:44" party="0" serialid="db606227-44a8-4af2-97a9-e0261b8d256c" ani="7703808051" dnis="6241" event="20103" appearance="0" infoonly="0" number="6409" cause="0" /></cti_transaction_info><tx_sys_classification_info><sys_classification plan_id="10" plan_session_id="0" plan_assoc_id="0" class_id="-8" requestor_id="0" trigger_start_time="2006-04-26T09:11:27" user_id="" trigger_stop_time="2006-04-26T09:11:44" /></tx_sys_classification_info><tx_warning_info /></transaction></ROOT>'

select @after_doc

EXEC @retc= dbo.sp_xml_preparedocument @idocOUTPUT, @after_doc

SELECT @record_type= record_type, @sm_session_id= sm_session_id, @user_id=CASEWHENuser_id<= 0THEN-1ELSEuser_idEND, @system_id= system_id,

@fot_serial_id= fot_serial_id, @uudata_delimiter= uudata_delimiter

FROMOPENXML(@idoc,'/ROOT/transaction',1)

WITH(record_typeint,

sm_session_idnvarchar(50),

user_idint,

fot_serial_idvarchar(50),

fot_flagtinyint,

system_idint,

uudata_delimitervarchar(2))

SELECT @record_type, @sm_session_id, @user_id, @system_id,

@fot_serial_id, @uudata_delimiter

node() code:

DECLARE

@idocINT,

@trans_idint,

@after_docxml,

@record_typeint,

@sm_session_idnvarchar(50),

@trigger_start_timedatetime,

@user_idint,

@fot_serial_idvarchar(50),

@system_idint,

@uudata_delimitervarchar(2),

@retcint

SELECT @after_doc='<ROOT><transaction fot_flag="" fot_serial_id="91bec7f9-edd7-4abb-b912-cef35f753e6b" record_type="1" user_id="6" sm_session_id="a31000ef-fa7f-41c8-8de2-2572c471ed6d" uudata_delimiter="#;" system_id="1"><voice_transaction alias_id="4" trans_size="16453" machine_id="7" comp_code="0" tran_start="2006-04-26T09:11:27" tran_stop="2006-04-26T09:11:44" line_id="1" ext_id="6409" tr_filename="extT8.1_04_26_2006_09;11;27.wav" tr_path="\\IRTXQFNTI001\Recordings\0001" /><screen_transaction alias_id="" trans_size="" comp_code="" tran_start="" tran_stop="" screen_delay="" ip_address="" tr_filename="" tr_path="" /><tx_trigger_info /><tx_classification_info /><cti_transaction_info><cti_transaction userdata="2#;QRecorded= #;QFOTSerialID= #;" eventtime="2006-04-26T09:11:27" party="0" serialid="db606227-44a8-4af2-97a9-e0261b8d256c" ani="7703808051" dnis="6241" event="20101" appearance="0" infoonly="0" number="6409" cause="0" /><cti_transaction userdata="2#;QRecorded= #;QFOTSerialID= #;" eventtime="2006-04-26T09:11:44" party="0" serialid="db606227-44a8-4af2-97a9-e0261b8d256c" ani="7703808051" dnis="6241" event="20103" appearance="0" infoonly="0" number="6409" cause="0" /></cti_transaction_info><tx_sys_classification_info><sys_classification plan_id="10" plan_session_id="0" plan_assoc_id="0" class_id="-8" requestor_id="0" trigger_start_time="2006-04-26T09:11:27" user_id="" trigger_stop_time="2006-04-26T09:11:44" /></tx_sys_classification_info><tx_warning_info /></transaction></ROOT>'

select @after_doc

SELECT @record_type= trn.value('@record_type','[int]'),

@sm_session_id= trn.value('@sm_session_id','nvarchar(50)'),

@user_id=CASEWHEN trn.value('@user_id','int')<= 0THEN-1ELSE trn.value('@user_id','int')END,

@system_id= trn.value('@system_id','int'),

@fot_serial_id= trn.value('@fot_serial_id','varchar(50)'),

@uudata_delimiter= trn.value('@uudata_delimiter','varchar(2)')

from @after_doc.nodes('/ROOT/transaction')

AS node(trn)

SELECT @record_type, @sm_session_id, @user_id, @system_id,

@fot_serial_id, @uudata_delimiter

[16346 byte] By [rbennet8] at [2007-12-27]
# 1

Whilst the OPENXML way is based on COM it is now in its 3rd version with SQL 2005. (it had a second version in SQL 2000 SP4) This means its had some big performance tweaks.

I have found nodes doesn't perform well in certain situations.

It's also not fair to measure the cost, nodes has a different means by which it calculates the number of rows returned (xml indexes etc). Thus the costs will be very different.

You need to do imperical testing using the two to find which one uses less CPU/Memory etc

SimonS at 2007-9-4 > top of Msdn Tech,SQL Server,SQL Server XML...
# 2

Could you give some idea as to which situations nodes do not work well? Is sp_aml_preparedocument the preferred method to render a large xml document?

Thanks,

Bob

rbennet8 at 2007-9-4 > top of Msdn Tech,SQL Server,SQL Server XML...
# 3

Large documents. Navigating parent axes, although there is a work around

You need to try it in your situation, no 1 XML document is like another.

nodes in my view is a more elegant solution and easier to understand.

SimonS at 2007-9-4 > top of Msdn Tech,SQL Server,SQL Server XML...

SQL Server

Site Classified