问题描述
我有一个存储过程,它使用 SELECT FOR XML PATH 语句将 XML 返回给调用者.随着更多行被添加到查询的主表中,我注意到此查询的性能有所下降.
I have a stored procedure which returns XML to the caller using a SELECT FOR XML PATH statement. As more rows have been added to the main table in the query I have noticed that the performance of this query has degraded.
经过调查,我发现在没有 FOR XML 语句的情况下在 SQL management studio 中运行查询所需的时间是 FOR XML 查询所需时间的 1/3.由 FOR XML 调用的 XML 生成是否有很大的开销,或者在使用 FOR XML 时有哪些应该做和不应该做的事情.
On investigation I found that running the query in SQL management studio without the FOR XML statement takes the 1/3 of the time the FOR XML query takes. Is the XML generation that is invoked by FOR XML that much of an overhead or are there some do's and don't s when using FOR XML.
下面是我的表定义和使用的返回 > 3000 行的查询.已更改列名称以保护无辜者.
Below is my table definition and the query used which returns > 3000 rows. The column names have been changed to protect the innocent.
欢迎任何建议.
查询:
推荐答案
只是为了确保您没有将客户端渲染时间计入等式,将结果分配给一个变量并查看执行时间是否相同.这是我刚刚在服务器上运行的示例:
Just to make sure that you're not taking client rendering time into the equation, assign the result to a variable and see if the execution time is the same. Here's an example I just ran on my server:
这是结果(查看执行时间):
And here are the results (looking at the execution times):
将其放入变量需要 34+2=36 毫秒,而将其转储到我的屏幕需要 884 毫秒.这是一个很大的不同!
Putting it in a variable took 34+2=36 ms whereas dumping it to my screen took 884. That's quite a difference!
这篇关于SELECT FOR XML 查询速度慢吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!