我有一个带有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>Administrator</UserName>
<Access>Allow</Access>
<IsInherited>false</IsInherited>
<ApplyTo>This object only</ApplyTo>
<Permission>Read All Properties</Permission>
<Permission>Delete</Permission>
</ACEInformation>
<ACEInformation>
<UserName>Admin2</UserName>
<Access>Allow</Access>
<IsInherited>false</IsInherited>
<ApplyTo>This object only</ApplyTo>
<Permission>Read All Properties</Permission>
<Permission>Delete</Permission>
</ACEInformation>
</Dacl>
</Security>
我的需要是,我必须查询具有值访问权限:允许和权限:删除的所有UserName值。为此,我正在使用以下XQuery。
Select xmlColumn.query('for $item in (/Security/Dacl/ACEInformation[Access="Allow"][Permission="Delete"]/UserName) return concat($item,";")').value('.','NVARCHAR(MAX)') from myTable
返回上述xml的值:管理员; Admin2 。查询工作正常...但性能非常低,1000行需要1分钟。
你能为这种情况提供最好的xquery吗?
答案 0 :(得分:1)
xQuery没有真正的变化,但我将concat更改为for xml
concat instaed。
select (
select A.X.value('(UserName/text())[1]', 'nvarchar(max)')+';'
from T.xmlColumn.nodes('/Security/Dacl/ACEInformation[Access = "Allow" and Permission = "Delete"]') as A(X)
for xml path(''), type
).value('text()[1]', 'nvarchar(max)')
from myTable as T
答案 1 :(得分:1)
我不认为XQuery存在问题,你可以稍微改一下([Access="Allow"][Permission="Delete"]
=&gt; [Access="Allow" and Permission="Delete"]
)。
在1000行上,您的查询在SQL小提琴上运行得非常快 - 请参阅 sql fiddle demo