【问题标题】:XQuery where check Performance issue with SQL QueryXQuery where 检查 SQL Query 的性能问题
【发布时间】:2013-11-25 05:47:47
【问题描述】:

我有一个带有 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>

我的需要是,我必须查询所有具有 Access: AllowPermission: Delete 值的 UserName 值。为此,我正在使用以下 XQuery。

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

返回值:Administrator;Admin2 用于上述 xml。查询工作正常...但性能非常低,1000 行需要 1 分钟。

你能为这种情况提供最好的 xquery 吗?

【问题讨论】:

    标签: sql sql-server xquery sqlxml xquery-sql


    【解决方案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
    

    【讨论】:

    • 感谢 Mikael。它运行良好,性能问题是我的 pbm 不在查询中
    • 嗨 Mikael,我还有一个问题可以帮我吗?请检查这个问题条目---> stackoverflow.com/questions/20188220/…
    【解决方案2】:

    我认为 XQuery 没有问题,不过你可以稍微改变一下 ([Access="Allow"][Permission="Delete"] => [Access="Allow" and Permission="Delete"])。

    在 1000 行上,您的查询在 SQL fiddle 上运行得非常快 - 请参阅 sql fiddle demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多