简体   繁体   中英

XQuery where check Performance issue with SQL Query

I have one sql table with xml column which holds the value like below 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>

Here my need is, I have to query all UserName values who are having values Access: Allow and Permission: Delete . For that, I am using following XQuery.

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

It returns the value : Administrator;Admin2 for above xml. the query is working fine...but performance is very low it is taking 1 mins for 1000 rows.

Can you any one provide best xquery for this case?

No real change in the xQuery but I changed the concat into a 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

I don't think there's problem in XQuery, you can change it a little bit though ( [Access="Allow"][Permission="Delete"] => [Access="Allow" and Permission="Delete"] ).

On 1000 rows your query works pretty fast at SQL fiddle - see sql fiddle demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM