I am doing some crude benchmarks with the xml datatype of SQL Server 2008. I’ve seen many places where .exist is used in where clauses. I recently compared two queries though and got odd results.
select count(testxmlrid) from testxml
where Attributes.exist('(form/fields/field)[@id="1"]')=1
This query takes about 1.5 seconds to run, with no indexes on anything but the primary key(testxmlrid)
select count(testxmlrid) from testxml
where Attributes.value('(/form/fields/field/@id)[1]','integer')=1
This query on the otherhand takes about .75 seconds to run.
I’m using untyped XML and my benchmarking is taking place on a SQL Server 2008 Express instance. There are about 15,000 rows in the dataset and each XML string is about 25 lines long.
Are these results I’m getting correct? If so, why does everyone use .exist? Am I doing something wrong and .exist could be faster?
You are not counting the same things. Your
.existquery(form/fields/field)[@id="1"]checks all occurrences of@idin the XML until it finds one with the value1and your.valuequery(/form/fields/field/@id)[1]only fetches the first occurrence of@id.Test this:
The
.existquery count is 1 because it finds the@id=1in the secondfieldnode and the.valuequery count is 0 because it only checks the value for the first occurrence of@id.An
.existquery that only checks the value for the first occurrence of@idlike your.valuequery would look like this.