Alternatives to XML shredding in SQL

后端 未结 1 887
情书的邮戳
情书的邮戳 2021-01-21 15:24

I tried to shred XML into a temporary table by using XQuery .nodes as follows. But, I got performance problem. It is taking much time to shred. Please give me an id

相关标签:
1条回答
  • 2021-01-21 16:06

    Specify the text() node in your values clause.

    insert into #DW_TEMP_TABLE_SAVE
    select A.B.value('(USER_ID/text())[1]', 'nvarchar(30)' ) [USER_ID], 
           A.B.value('(USER_NAME/text())[1]', 'nvarchar(30)' ) [USER_NAME]
    from @l_n_XMLDoc.nodes('/USER_DETAILS/RECORDSET/ROW') as A(B)
    

    Not using text() will create a query plan that tries concatenate the values from the specified node with all its child nodes and I guess you don't want that in this scenario. The concatenation part of the query if you don't use text() is done by the UDX operator and it is a good thing not to have it in your plan.

    enter image description here

    Another thing to try is OPENXML. In some scenarios (large xml documents) I have found that OPENXML performs faster.

    declare @idoc int
    exec sp_xml_preparedocument @idoc out, @l_n_XMLDoc
    
    insert into #DW_TEMP_TABLE_SAVE
    select USER_ID, USER_NAME
    from openxml(@idoc, '/USER_DETAILS/RECORDSET/ROW', 2) 
      with (USER_ID  nvarchar(30), USER_NAME nvarchar(30))
    
    exec sp_xml_removedocument @idoc
    
    0 讨论(0)
提交回复
热议问题