慢速SafeDataReader循环

时间:2017-07-19 19:59:14

标签: vb.net executereader

我有一个vb.net/SQL Server客户端应用程序,我需要在现有查询中添加左连接。 asp.net使用数据层中的相同代码来检索应用程序的Web版本的数据。接下来的两个代码块显示整个原始语句和包含所有连接的新语句。我建议你跳到较低的代码块,这些代码块要小得多,以说明问题。 原始查询:

    select q.qasr_id as Id,
           q.qasr_identification_number as IdentificationNumber,
           q.status as Status,
           b.business_unit_name as BusinessUnitName,
           b.business_unit_id as BusinessUnitId,
           u.login as Originator,
           q.date_created as DateCreated,
           q.po_number as PoNumber,
           q.po_part_number as PoPartNumber,
           q.po_part_rev as PoPartRev,
           q.lot_quantity as LotQuantity,
           q.control_number as ControlNumber,
           q.item_number as PoItem,
           q.buyer as Buyer,
           q.buyercode as BuyerCode,
           q.spe as Spe,
           p.program as ProgramName,
           l.product_line as productline,
           s.ent_supplier_nbr as SupplierEsd,
           s.sup_name1 as SupplierName,
           c.commodity_code as CommodityCode,
           a.agency_name as AgencyName,
           q.verifyid as AgencyDocId,
           q.activity as Activity,
           case q.isdeleted when 1 then 'Deleted' else '' end as Deleted,
           case when m.d_count > 0 then 'Yes' else 'No' end as Dispositioned
      from qasr q
      join auth_user u on q.auth_user_id = u.auth_user_id
      join functional_area f on q.functional_area_id = f.functional_area_id
      join business_unit b on f.business_unit_id = b.business_unit_id
      left join suppliers s on q.supplier_id = s.supplier_id
      left join programs p on q.program_id = p.program_id
      left join product_lines l on p.product_line_id = l.product_line_id
      left join commodities c on q.commodity_id = c.commodity_id
      left join agency a on q.agency_id = a.agency_id
      left join (select qasr_id as qasr_id, count(1) as d_count
                   from qasr_metric
                  where d = 1
                  group by qasr_id) m on m.qasr_id = q.qasr_id
       where b.business_unit_id = @BusinessUnitId
         and q.isdeleted = @IsDeleted

新查询:

    select q.qasr_id as Id from qasr q

新查询将左连接添加到第二个表(qasr_metric),以确定第二个表是否包含列" D"中任何值为1的关联行。 qasr_metric表由(quasr_id,d)索引,这就是性能受到最小影响的原因。删除所有其他列和连接归结为:

之间的区别
    select q.qasr_id as Id,
           case when m.d_count > 0 then 'Yes' else 'No' end as Dispositioned
      from qasr q
      left join (select qasr_id as qasr_id, count(1) as d_count
                   from qasr_metric
                  where d = 1
                  group by qasr_id) m on m.qasr_id = q.qasr_id

    Private Sub Fetch(ByVal dr As SafeDataReader)
       Using dr
          While dr.Read()
             Add(InspectionDocumentSummary.GetInspectionDocumentSummary(dr))
             _id = dr.GetGuid("Id")
             _identificationNumber = dr.GetString("IdentificationNumber")
             _status = dr.GetString("Status")
             _originator = dr.GetString("Originator")
             _dateCreated = dr.GetSmartDate("DateCreated")
             _poPartNumber = dr.GetString("PoPartNumber")
             _poPartRev = dr.GetString("PoPartRev")
             _lotQuantity = Convert.ToString(dr.GetInt32("LotQuantity"))
             _buyer = dr.GetString("Buyer")
             _spe = dr.GetString("Spe")
             _agencyDocId = dr.GetString("AgencyDocId")
             _agencyName = dr.GetString("AgencyName")
             _activity = dr.GetString("Activity")
             _controlNumber = dr.GetString("ControlNumber")
             _dispositioned = dr.GetString("Dispositioned")
             _programName = dr.GetString("ProgramName")
             _supplierEsd = dr.GetString("SupplierEsd")
             _supplierName = dr.GetString("SupplierName")
             _commodityCode = dr.GetString("CommodityCode")
          End While
       End Using
    End Sub

应用程序的asp.net端新查询的性能在原始查询的大约3秒内,如果我在SQL Server Mgmt Studio中单独运行查询,情况也是如此。但是,应用程序的客户端在几秒钟内处理原始查询的结果,但新查询需要一个多小时,这是荒谬的。查看vb代码本身,数据层使用参数化查询调用ExecuteReader,该查询几乎立即完成原始查询和新查询。问题发生在使用dr作为ExecuteReader填充的阅读器调用的业务层中。每行返回的数据用于填充新对象(InspectionDocumentSummary),这些对象的列表用于填充显示给用户的DataGridView。为了简洁起见,将一些创建新对象的子组合起来得到:

...
<Page.Resources>
    <SolidColorBrush x:Key= "myColor" Color="Red"/>
    <Thickness x:Key="myThickness">1,1,1,1</Thickness>
<Page.Resources>
...
<Broder BorderBrush="{StaticResource myColor}" BorderThickness="{StaticResource myThickness}">something</Border>
...

对于原始查询,这几乎立即完成,但新查询需要一个多小时。我的理解是ExecuteReader检索数据而dr.Read循环只是将返回的数据读入正在填充的DataGridView中(在我的情况下没有其他数据处理)。我更换了&#34; _dispositioned = dr.GetString(&#34; Dispositioned&#34;)&#34;在上面的代码中使用&#34; _dispositioned =&#34;是&#34;&#34;没有变化所以数据读取本身不是问题。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

这样的许多问题都是将查询中的类型转换为字符串,例如将varchar的一侧与另一侧的intchar进行比较或连接时。

如果查询正在运行而没有直接来自数据库的性能问题,请尝试运行查询并让.NET在数据读取器读取行之后进行数据类型转换:

Private Sub Fetch(ByVal dr As SafeDataReader)
   Using dr
      While dr.Read()
         Add(InspectionDocumentSummary.GetInspectionDocumentSummary(dr))
         _id = GUID.Parse(dr("Id"))
         _identificationNumber = dr("IdentificationNumber")
         _status = dr("Status")
         _originator = dr("Originator")
         _dateCreated = dr.GetSmartDate("DateCreated")
         _poPartNumber = dr("PoPartNumber")
         _poPartRev = dr("PoPartRev")
         _lotQuantity = Convert.ToString(dr.GetInt32("LotQuantity"))
         _buyer = dr("Buyer")
         _spe = dr("Spe")
         _agencyDocId = dr("AgencyDocId")
         _agencyName = dr("AgencyName")
         _activity = dr("Activity")
         _controlNumber = dr("ControlNumber")
         _dispositioned = dr("Dispositioned")
         _programName = dr("ProgramName")
         _supplierEsd = dr("SupplierEsd")
         _supplierName = dr("SupplierName")
         _commodityCode = dr("CommodityCode")
      End While
   End Using
End Sub

如果在没有致电Add的情况下尝试运行它,以确保该方法中的某些内容不会导致问题,则无法帮助您运行。