大数据集将值连接到查询中

Large Data Set Concatenate Values into Query

我有一个包含数百万行的视图,当对所有内容进行 运行 处理时超时,但可以在其中执行 selects 以拉回数据快照。

VIEW A(超过 5000000 行 - 运行ning 时超时)

我正在尝试根据列 0 和 idno 连接条件名称和 KeyDescription2 作为不同的条件,然后导出到 csv 或电子表格中。

努力实现的结果

我创建了一个 .asp 页面,我在其中 select 与原始 table 不同的第 0 列和 ID 编号,然后循环获取条件名称。这很好用,但是完成 1000 行需要 2 分钟,这意味着需要半年的时间才能 运行 完整报告!所以需要一种更好更快的方式来实现这一点。有谁知道我如何直接在 SQL-Server 本身中获得结果。

使用 stuff() with select ... for xml path ('') method of string concatenation

select 
    Column0
  , idno
  , grouppid
  , Criterianame = stuff((
      select ', '+ i.CriteriaName +': ' + i.KeyDescription2
      from view_a as i
      where i.Column0 = a.Column0
        and i.grouppid = a.grouppid
        and i.idno = '' -- `i.idno is null` if your blank value is a null
      for xml path (''), type).value('(./text())[1]','nvarchar(max)')
    ,1,2,'')
  , criteriatype2
  , keydescription2
from view_a as a
where a.idno <> '' -- `a.idno is not null` if your blank value is a null

备注:

  • 这不会重新格式化您在所需结果中指定的日期值。
  • 尽管这会比循环更快,但仍然不会非常快
  • 您可能想要调查该视图,看看使用基表是否会带来更好的性能。
  • 在 SQL Server 2017 中,您可以使用 string_agg().