最近在忙着优化集团公司的一个报表。优化完成后,报表查询速度有从半小时以上(甚至查不出)到秒查的质变。从修改SQL查询语句逻辑到决定创建存储
过程实现,花了我3天多的时间,在此总结一下,希望对朋友们有帮助。
数据背景
首先项目是西门子中国在我司实施部署的MES项目,由于项目是在产线上运作(3 years+),数据累积很大。在项目的数据库中,大概上亿条数据的表有5个以上,千万级数据的表10个以上,百万级数据的表,很多...
(历史问题,当初实施无人监管,无人监控数据库这块的性能问题。ps:我刚入职不久...)
不多说,直接贴西门子中国的开发人员在我司开发的SSRS报表中的SQL语句:

1 select distinct b.MaterialID as matl_def_id, c.Descript, case when right(b.MESOrderID, 12) < '001000000000' then right(b.MESOrderID, 9)
2 else right(b.MESOrderID, 12) end as pom_order_id, a.LotName, a.SourceLotName as ComLot,
3 e.DefID as ComMaterials, e.Descript as ComMatDes, d.VendorID, d.DateCode,d.SNNote, b.OnPlantID,a.SNCUST
4 from
5 (
6 select m.lotname, m.sourcelotname, m.opetypeid, m.OperationDate,n.SNCUST from View1 m
7 left join co_sn_link_customer as n on n.SNMes=m.LotName
8 where
9 ( m.LotName in (select val from fn_String_To_Table(@sn,',',1)) or (@sn) = '') and
10 ( m.sourcelotname in (select val from fn_String_To_Table(@BatchID,',',1)) or (@BatchID) = '')
11 and (n.SNCust like '%'+ @SN_ext + '%' or (@SN_ext)='')
12 ) a
13 left join
14 (
15 select * from Table1 where SNType = 'IntSN'
16 and SNRuleName = 'ProductSNRule'
17 and OnPlantID=@OnPlant
18 ) b on b.SN = a.LotName
19 inner join MMdefinitions as c on c.DefID = b.MaterialID
20 left join Table1 as d on d.SN = a.SourceLotName
21 inner join MMDefinitions as e on e.DefID = d.MaterialID
22 where not exists (
23 select distinct LotName, SourceLotName from ELCV_ASSEMBLE_OPS
24 where LotName = a.SourceLotName and SourceLotName = a.LotName
25 )
26 and (d.DateCode in (select val from fn_String_To_Table(@DCode,',',1)) or (@DCode) = '')
27 and (d.SNNote like '%'+@SNNote+'%' or (@SNNote) = '')
28 and ((case when right(b.MESOrderID, 12) < '001000000000' then right(b.MESOrderID, 9)
29 else right(b.MESOrderID, 12) end) in (select val from fn_String_To_Table(@order_id,',',1)) or (@order_id) = '')
30 and (e.DefID in (select val from fn_String_To_Table(@comdef,',',1)) or (@comdef) = '')
31 --View1是一个嵌套两层的视图(出于保密性,实际名称可能不同),里面有一张上亿数据的表和几张千万级数据的表做左连接查询
32 --