2013-11-25 12 views
1

我有一個XML列一個SQL表持有像下面的xmlXQuery的地方使用SQL查詢來檢查的性能問題

<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> 

這裏的價值我需要的是,我要查詢所有用戶名值誰是具有價值訪問:允許權限:刪除。爲此,我使用以下XQuery。

Select xmlColumn.query('for $item in (/Security/Dacl/ACEInformation[Access="Allow"][Permission="Delete"]/UserName) return concat($item,";")').value('.','NVARCHAR(MAX)') from myTable 

它返回值:管理員; Admin2的以上XML。查詢工作正常......但性能非常低,1000行需要1分鐘。

你能爲這種情況提供最好的xquery嗎?

回答

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 
+0

感謝的Mikael。它工作得很好,性能問題是我在查詢了Mikael – kombsh

+0

PBM喜不,我還有一個問題,你可以幫我?請檢查此問題條目---> http://stackoverflow.com/questions/20188220/xquery-exists-check-in-select-sql-query – kombsh

1

我不認爲有XQuery中的問題,你可以改變它,但([Access="Allow"][Permission="Delete"] =>[Access="Allow" and Permission="Delete"])一點點。

在1000行的查詢工作非常快,在SQL小提琴 - 見sql fiddle demo

+0

感謝您的答案 – kombsh