SELECT FOR XML查询速度慢吗?

时间:2011-06-07 19:54:22

标签: sql-server xml

我有一个存储过程,它使用SELECT FOR XML PATH语句将XML返回给调用者。随着在查询中的主表中添加了更多行,我注意到此查询的性能已降低。

经过调查,我发现在没有FOR XML语句的SQL管理工作室中运行查询需要花费1/3的FOR XML查询时间。 FOR XML调用的XML生成是否需要大量开销,或者在使用FOR XML时是否存在。

下面是我的表定义和使用的返回>的查询3000行。列名已被更改以保护无辜者。

欢迎任何建议。

CREATE TABLE dbo.results
( 
colA  int NOT NULL, 
colB  varchar(20) NULL, 
colC varchar(30) NULL, 
colD varchar(100) NULL, 
colE char(3) NULL, 
colF int NULL, 
colG int NULL, 
colH datetime NULL, 
colJ int NULL, 
colK int NULL, 
colL int NULL, 
colM int NULL, 
colN int NULL, 
colO int NULL, 
colP int NULL, 
colQ int NULL, 
colR int NULL, 
colS int NULL, 
colT int NULL, 
colU int NULL, 
colV int NULL, 
colW int NULL, 
colX int NULL, 
colY datetime NULL, 
colZ int NULL, 
colA1 datetime NULL, 
colB1 int NULL, 
colC1 int NULL, 
colD1 int NULL, 
colE1 int NULL, 
colF1 int NULL, 
colG1 int NULL, 
colH1 int NULL, 
colI1 int NULL, 
colK1 int NULL, 
colL1 int NULL, 
colM1 int NULL, 
colN1 int NULL, 
colO1 int NULL, 
colP1 int NOT NULL, 
colQ1 int NOT NULL, 
colS1 int NULL, 
colT1 int NULL, 
colU1 int NULL, 
colV1 int NULL, 
colW1 int NULL, 
colX1 int NULL, 
colY1 int NULL, 
colZ1 datetime NULL 

CONSTRAINT results_pk PRIMARY KEY CLUSTERED 
( 
   colA ASC 
)
WITH (PAD_INDEX  = OFF, 
      STATISTICS_NORECOMPUTE  = OFF, 
      IGNORE_DUP_KEY = OFF, 
      ALLOW_ROW_LOCKS  = ON, 
      ALLOW_PAGE_LOCKS  = ON) 
 ON PRIMARY) 

查询:

select    colA  "@A", 
          colB "@B", 
          colC "@C", 
          colD "@D", 
          colE "@E", 
          colF "@F", 
          colG "@G",                      
          colH "@H",         
          colJ "@J", 
          colK "@K",            
          colL "@L", 
          colM "@M", 
          colO "@O", 
          colN "@N", 
          colP "@P", 
          colQ "@Q", 
          colR "@R", 
          colZ1 "@Z1", 
          colS "@S", 
          colT "@T", 
          colU "@U", 
          colV "@V", 
          colW "@W", 
          colX "@X", 
          colY "@Y", 
          colP1 "@P1", 
          colQ1 "@Q1", 
          colO1 "@O1" 
from result
order by colO desc , colC 
for xml PATH('item'), TYPE 

2 个答案:

答案 0 :(得分:2)

只是为了确保您没有将客户端渲染时间纳入等式,将结果分配给变量并查看执行时间是否相同。这是我刚在服务器上运行的一个例子:

SET STATISTICS TIME ON
go

DECLARE @x XML
PRINT '------------'
SELECT @x =
(SELECT * FROM sys.[dm_exec_connections] AS dec
FOR XML PATH('connections'), TYPE)
PRINT '------------'

SELECT * FROM sys.[dm_exec_connections] AS dec
FOR XML PATH('connections'), TYPE

以下是结果(查看执行时间):

SQL Server parse and compile time: 
   CPU time = 0 ms, elapsed time = 0 ms.

 SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 0 ms.
SQL Server parse and compile time: 
   CPU time = 0 ms, elapsed time = 87 ms.
------------

 SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 34 ms.

 SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 2 ms.
------------

 SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 0 ms.

(1 row(s) affected)

 SQL Server Execution Times:
   CPU time = 15 ms,  elapsed time = 884 ms.

将它放入变量需要34 + 2 = 36毫秒,而将其转储到我的屏幕需要884.这是相当不同的!

答案 1 :(得分:1)

根据我的经验,这就是它的方式 - 慢。我们习惯于使用基于集合的SQL,并且会受到效率的影响。 XML还没有。它在SQL Server中实现为CLR类型,这为该等式添加了另一层开销。

你的例子就像它得到的一样简单;一旦你处理更大的数据集,就没有一颗银弹会更快。那么,你必须将整个数据集(3k记录)作为XML返回吗?如果需要,你能只检索一部分并抓住剩下的部分吗?