我有一个 vb.net/SQL 服务器客户端应用程序,我需要在其中向现有查询添加左联接。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
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
where b.business_unit_id = @BusinessUnitId
and q.isdeleted = @IsDeleted
新查询:
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
新查询将左联接添加到第二个表 (qasr_metric(,以确定第二个表是否包含列"D"中值为 1 的任何关联行。qasr_metric表按 (quasr_id, d( 编制索引,这就是性能影响最小的原因。删除所有其他列和联接归结为以下各项之间的差异:
select q.qasr_id as Id from qasr q
和
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
应用程序 asp.net 端的新查询的性能在原始查询的大约 3 秒内,如果我在 SQL Server Mgmt Studio 中单独运行查询,情况也是如此。但是,应用程序的客户端在几秒钟内处理原始查询的结果,但新查询需要一个多小时,这是荒谬的。查看 vb 代码本身,数据层使用参数化查询调用 ExecuteReader,对于原始查询和新查询,该查询几乎立即完成。该问题再次出现在业务层中,该业务层使用 dr 作为 ExecuteReader 填充的读取器调用。每行中返回的数据用于填充新对象 (InspectionDocumentSummary(,这些对象的列表用于填充向用户显示的 DataGridView。为了简洁起见,组合一些创建新对象的子文件可以得到:
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
对于原始查询,此操作几乎立即完成,但新查询需要一个多小时。据我了解,ExecuteReader 检索数据和 dr。读取循环只是将返回的数据读取到正在填充的 DataGridView 中(在我的情况下,没有其他数据处理(。我替换了"_dispositioned = 博士。GetString("Dispositioned"("在上面的代码中,"_dispositioned = "Yes"没有变化,所以数据读取本身不是问题。有什么建议吗?
许多这样的问题都与在查询中转换为字符串的类型有关,例如,将 varchar 的一侧与另一侧的 int 进行比较或连接时。
如果查询直接从数据库运行而没有性能问题,请尝试运行查询,并在数据读取器读取行后让 .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
的情况下运行它,以确保该方法中的某些内容不会导致问题。