SQL Server在带有for循环的xquery中的性能

SQL Server在带有for循环的xquery中的性能,sql,sql-server,xquery,xquery-sql,Sql,Sql Server,Xquery,Xquery Sql,我有一个包含xml列的sql表,它包含如下xml格式的值 <Security xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Dacl> <ACEInformation> <UserName>Authenticated Users</UserName>

我有一个包含xml列的sql表,它包含如下xml格式的值

<Security xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Dacl>
    <ACEInformation>
      <UserName>Authenticated Users</UserName>
      <Access>Allow</Access>
      <IsInherited>false</IsInherited>
      <ApplyTo>This object only</ApplyTo>
      <Permission>List Contents</Permission>
      <Permission>Read All Properties</Permission>
      <Permission>Read Permissions</Permission>
    </ACEInformation>
    <ACEInformation>
      <UserName>Local System</UserName>
      <Access>Allow</Access>
      <IsInherited>false</IsInherited>
      <ApplyTo>This object only</ApplyTo>
      <Permission>Read All Properties</Permission>
      <Permission>Read Permissions</Permission>
    </ACEInformation>
  </Dacl>
</Security>

查询工作正常,但给出结果需要太多时间,对于1000行,需要2分钟…有人能帮我改进此查询的性能吗

试试这样的方法:

DECLARE @table TABLE (ID INT, XmlCOntent XML)

INSERT INTO @Table VALUES(1, '<Security xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Dacl>
    <ACEInformation>
      <UserName>Authenticated Users</UserName>
      <Access>Allow</Access>
      <IsInherited>false</IsInherited>
      <ApplyTo>This object only</ApplyTo>
      <Permission>List Contents</Permission>
      <Permission>Read All Properties</Permission>
      <Permission>Read Permissions</Permission>
    </ACEInformation>
    <ACEInformation>
      <UserName>Local System</UserName>
      <Access>Allow</Access>
      <IsInherited>false</IsInherited>
      <ApplyTo>This object only</ApplyTo>
      <Permission>Read All Properties</Permission>
      <Permission>Read Permissions</Permission>
    </ACEInformation>
  </Dacl>
</Security>')

SELECT
    ID,
    Access = XACE.value('(Access)[1]', 'varchar(50)'),
    ApplyTo = XACE.value('(ApplyTo)[1]', 'varchar(50)'),
    AuthUser = XACE.value('(UserName)[1]', 'varchar(50)'),
    Perm1 = XACE.value('(Permission)[1]', 'varchar(50)'),
    Perm2 = XACE.value('(Permission)[2]', 'varchar(50)'),
    Perm3 = XACE.value('(Permission)[3]', 'varchar(50)')
FROM
    @table
CROSS APPLY
    XmlContent.nodes('/Security/Dacl/ACEInformation') AS XTbl(XACE)
DECLARE@table表(ID INT,XmlCOntent XML)
插入@表值(1,'
验证用户组
容许
假的
仅此对象
目录
读取所有属性
读取权限
本地系统
容许
假的
仅此对象
读取所有属性
读取权限
')
挑选
身份证件
Access=XACE.value('(Access)[1]','varchar(50)'),
ApplyTo=XACE.value(‘(ApplyTo)[1]’,‘varchar(50)’),
AuthUser=XACE.value('(用户名)[1],'varchar(50)'),
Perm1=XACE.value('(权限)[1]','varchar(50)'),
Perm2=XACE.value('(权限)[2]','varchar(50)',
Perm3=XACE.value('(权限)[3]','varchar(50)'
从…起
@桌子
交叉应用
节点('/Security/Dacl/ACEInformation')作为XTbl(XACE)
给我一个输出:

select (
       select '['+
                 A.X.value('(Access/text())[1]', 'nvarchar(max)')+
                 '->'+
                 A.X.value('(UserName/text())[1]', 'nvarchar(max)')+
                 '->'+
                 (
                 select P.X.value('(./text())[1]', 'nvarchar(max)')+';'
                 from A.X.nodes('Permission') as P(X)
                 for xml path(''), type
                 ).value('text()[1]', 'nvarchar(max)')+
                 '->'+
                 A.X.value('(ApplyTo/text())[1]', 'nvarchar(max)')+
               ']'
       from T.xmlColumn.nodes('/Security/Dacl/ACEInformation') as A(X)
       for xml path(''), type
       ).value('text()[1]', 'nvarchar(max)')
from myTable as T

谢谢你的回答,但我需要的是,我必须将所有的价值观结合起来,得到一个单独的专栏,做得很好。。。非常感谢你……它将我的查询时间从2.5分钟减少到了10分钟secs@kombsh如果您使用的是SQL Server 2012,则可能会通过以下方法进一步加快速度。
select (
       select '['+
                 A.X.value('(Access/text())[1]', 'nvarchar(max)')+
                 '->'+
                 A.X.value('(UserName/text())[1]', 'nvarchar(max)')+
                 '->'+
                 (
                 select P.X.value('(./text())[1]', 'nvarchar(max)')+';'
                 from A.X.nodes('Permission') as P(X)
                 for xml path(''), type
                 ).value('text()[1]', 'nvarchar(max)')+
                 '->'+
                 A.X.value('(ApplyTo/text())[1]', 'nvarchar(max)')+
               ']'
       from T.xmlColumn.nodes('/Security/Dacl/ACEInformation') as A(X)
       for xml path(''), type
       ).value('text()[1]', 'nvarchar(max)')
from myTable as T