In xml. value of SQL server, the importance of XPath using the text () function

Source: Internet
Author: User
Tags xpath

I have been asked many times why the DBA specification requires that the text () function be added to the XPath of the value method when parsing xml values, it seems that the result of adding and not adding is the same.

The following test script shows the difference between text () and why it is required.

DECLARE
@ Xml

SET @ xml = N'
<A/>
<B> B
<C> c </c>
</B> ';
SELECT @ xml. value ('(/a) [1]', 'varchar (10 )');
SELECT @ xml. value ('(/a/text () [1]', 'varchar (10 )');

SELECT @ xml. value ('(/B) [1]', 'varchar (10 )');
SELECT @ xml. value ('(/B/text () [1]', 'varchar (10 )');

Execute this script. The obvious difference is displayed in the result.

 

By comparing the execution plans, we can see that there are significant differences between the two implementation plans and significant differences in efficiency. If text () is not used, additional steps are required to retrieve the data of the child node (even if the child node exists, the overhead still exists ).

Therefore, if it is not for a special purpose, you should specify the text () function in XPath for precise positioning to reduce performance overhead.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.