Select all child nodes

  • mister.magoo (10/8/2016)


    Eirikur Eiriksson (10/8/2016)


    For completeness, 5 different versions which all produce the same execution plan although performance may vary depending on the XML structure.

    😎

    One more variant for these which shows a very marginal estimated cost gain over using the text() node in the .value method.

    SELECT Id ,

    XmlCol.value('.', 'nvarchar(100)') AS 'fullname'

    FROM @t t

    CROSS APPLY t.XmlData.nodes('News/Photos/Photo/fullname/text()') x ( XmlCol );

    Brilliant MM, thanks, I had totally missed that one.

    😎

    The difference between using the text function within the value method and the nodes method on the XML data type is quite substantial. In simple terms, without the text function in the nodes method, the nodes return value is an XML snipped containing the value and attributes if any. This requires the serializer and the hierarchical structure element (OrdPath) for reassembling the output value and in addition both the singleton notation within the value function and an enumeration of the output to match the singleton's value. In this case, the XML relational operator will always map to a nested loop join in the execution plan.

    Using the text() function within the nodes method avoids the serialization and the OrdPath sorting as the nodes method's output is a scalar value. This enables the XML relational operator to be mapped to a left outer merge join and eliminates few other operators from the execution plan.

    Another way of achieving the same kind of short-cut is to use the data() function within the value method, the query below will produce the exactly same plan as the one with the text() function in the nodes method.

    SELECT

    XmlCol.value('data(.)','nvarchar(max)')

    FROM @TXML.nodes('News/Photos/Photo/fullname/text()') x ( XmlCol );

    Edit: Typo, crossed over as not correct.

  • Viewing 11 posts - 1 through 10 (of 10 total)

    You must be logged in to reply to this topic. Login to reply