[英]XQuery where check Performance issue with SQL Query
我有一個帶有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值: Administrator; Admin2 。 查詢工作正常...但是性能非常低,它需要1分鍾才能處理1000行。
您能為這種情況提供最佳的xquery嗎?
xQuery中沒有真正的變化,但是我將concat更改為for xml
concat insta。
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
我認為XQuery中沒有問題,不過您可以對其進行一些更改( [Access="Allow"][Permission="Delete"]
=> [Access="Allow" and Permission="Delete"]
)。
在1000行上,您的查詢在SQL Fiddle上運行非常快-請參見SQL Fiddle演示
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.