我有一个创建并运行SQL查询的SP。最终结果用于某些报告。尽管此查询需要很长时间才能运行(大于30秒是很长的时间,有时会持续一分钟以上)。
我已经阅读了一些有关基于集合的查询的知识,但是我不清楚这个特定查询是否合适,或者是否有转换方法。我所读过的有关基于集合的大多数内容似乎都在谈论将循环更改为类似这两个Programming SQL in a Set-Based Way和An introduction to set-based vs procedural programming approaches in T-SQL
的集合查询最终如下所示:
SELECT ProductionId, Max(ID) AS Id INTO #TEMP
FROM sdiProductionChemistry GROUP BY ProductionId
SELECT DISTINCT P.Id,
ID.InventBatchId AS CoilId, IT.DatePhysical AS DlvDate,IT.TransRefId AS SalesOrderId, 'RPS115898' As PackingSlipId,
CASE
WHEN NOT SI.Diameter IS NULL THEN SI.Diameter
ELSE xSI.Diameter END AS Diameter,
SI.Leco, SI.Tensilestrength,
CASE WHEN NOT SI.E200 IS NULL AND SI.E200 > 0 THEN convert(varchar,convert(numeric(10,1),SI.E200))
WHEN NOT xSI.Elongation IS NULL AND xSI.Elongation > 0 THEN convert(varchar,convert(numeric(10,1),xSI.Elongation))
ELSE '> 35' END AS E200,
CASE WHEN NOT P.HeatNumber IS NULL THEN P.HeatNumber
ELSE xSI.BreakDownId END AS HeatNumber,
--xSA.Heatnumber as SpectroHeatNumber,
CASE WHEN NOT SI.NetWeight IS NULL THEN SI.NetWeight
ELSE xSI.GrossWeight - xSI.TareWeight END AS NetWeight,
CASE
WHEN SI.CertConductivity = 0 THEN
SI.IACS_REAL
WHEN SI.CertConductivity > 0 THEN
SI.CertConductivity
END AS IACS, (SPC.CU + (SPC.AG / 10000)) AS CUAG,
ST.SalesName, ST.PurchOrderFormNum AS CustomerPO,
xSI.Grm,
-- Customer Spec Min/Max Fields
SCS.CUAGMin, SCS.CUAGMax, SCS.DiameterMin, SCS.DiameterMax, SCS.ElongMin, SCS.ElongMax,
SCS.StrengthMin, SCS.StrengthMax, SCS.OxygenMin, SCS.OxygenMax, SCS.ConductivityMin, SCS.ConductivityMax,
SCS.GrmMin, SCS.GrmMax, SCS.PopMin AS OxideMin, SCS.PopMax AS OxideMax,
SCS.ZnMax, SCS.ZnMin, SCS.PbMax, SCS.PbMin, SCS.SnMax, SCS.SnMin, SCS.PMax, SCS.PMin, SCS.MnMax, SCS.MnMin,
SCS.FeMax, SCS.FeMin, SCS.NiMax, SCS.NiMin, SCS.SiMax, SCS.SiMin, SCS.MgMax, SCS.MgMin, SCS.CrMax, SCS.CrMin,
SCS.TeMax, SCS.TeMin, SCS.AsMax, SCS.AsMin, SCS.SeMax, SCS.SeMin, SCS.SbMax, SCS.SbMin, SCS.CdMax, SCS.CdMin,
SCS.BiMax, SCS.BiMin, SCS.AgMax, SCS.AgMin, SCS.CoMax, SCS.CoMin, SCS.AlMax, SCS.AlMin, SCS.SMax, SCS.SMin,
SCS.BeMax, SCS.BeMin, SCS.HRFMax, SCS.HRFMin,
I.ItemName
-- Element values to show
, 0 Zn, xSA.Pb, 0 Sn, 0 P, 0 Mn, 0 Fe, 0 Ni, 0 Si, 0 Mg, 0 Cr, 0 Te, 0 [As], 0 Se, 0 Sb, 0 Cd, 0 Bi, 0 Ag, 0 Co, 0 Al, 0 S, 0 Be, 0 HRF, 0 SurfaceOxide
-- What to show
, case SCSS.ZnShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as ZnShow
, case SCSS.PbShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as PbShow
, case SCSS.SnShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as SnShow
, case SCSS.PShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as PShow
, case SCSS.MnShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as MnShow
, case SCSS.FeShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as FeShow
, case SCSS.NiShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as NiShow
, case SCSS.SiShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as SiShow
, case SCSS.MgShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as MgShow
, case SCSS.CrShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as CrShow
, case SCSS.TeShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as TeShow
, case SCSS.AsShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as AsShow
, case SCSS.SeShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as SeShow
, case SCSS.SbShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as SbShow
, case SCSS.CdShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as CdShow
, case SCSS.BiShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as BiShow
, case SCSS.AgShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as AgShow
, case SCSS.CoShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as CoShow
, case SCSS.AlShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as AlShow
, case SCSS.SShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as SShow
, case SCSS.BeShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as BeShow
, case SCSS.HRFShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as HRFShow
, case SCSS.OxideShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as OxideShow
, case SCSS.CuAgShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as CuAgShow
, case SCSS.DiameterShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as DiameterShow
, case SCSS.ElongationShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as ElongationShow
, case SCSS.StrengthShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as StrengthShow
, case SCSS.OxygenShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as OxygenShow
, case SCSS.ConductivityShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as ConductivityShow
, case SCSS.GRMShow when -1561783295 then 1 when -1561783296 then 0 else 0 end as GRMShow
FROM InventTrans AS IT
LEFT OUTER JOIN InventTable AS I ON IT.ItemId = I.ItemId
LEFT OUTER JOIN InventDim AS ID ON IT.INVENTDIMID = ID.InventDimId
LEFT OUTER JOIN SalesTable AS ST ON IT.TransRefId = ST.SalesId
LEFT OUTER JOIN SDICustomerSpecs AS SCS ON ST.CustAccount = SCS.CustomerId AND IT.ItemId = SCS.ItemId
LEFT OUTER JOIN SDIInventory AS SI ON ID.InventBatchId = SI.BatchId
LEFT OUTER JOIN SDICustomerSpecSheets AS SCSS ON SCSS.CustomerName = ST.SalesName
LEFT OUTER JOIN LAFARGA.LaFargaProd.dbo.BreakdownItem AS xSI ON ID.InventBatchId = xSI.BatchId
LEFT OUTER JOIN SDIProduction AS P ON SI.ProductionId = P.Id
LEFT OUTER JOIN #Temp AS T ON P.Id = T.PRODUCTIONID
LEFT OUTER JOIN SDIPRODUCTIONCHEMISTRY AS SPC ON T.PRODUCTIONID = SPC.ProductionId AND SPC.Id = T.Id
LEFT OUTER JOIN LAFARGA.LaFargaProd.dbo.vSpectroAssays AS xSA ON xSA.BatchID = ID.InventBatchId
WHERE IT.PackingSlipId = 'RPS115898'
ORDER BY ID.InventBatchId
有点长,创建它的SP更长,但是可以将其转换为基于集合的查询吗?
如果是这样,我将如何开始呢?
更新
答案 0 :(得分:1)
根据您发布的执行计划,一些索引可能会有所帮助。特别是在SDIPRODUCTIONCHEMISTRY.ProductionId上。它正在对约100万行进行表扫描。是否将IDENTITY
列设为ID并设置为PK?
将这些索引建议与一粒盐一起使用。它们仅针对计划所针对的查询,不会告诉您它们将如何影响击中该表的其他查询。它们是很好的起点,但是您仍然应该分析它们所做的更改,以确保索引不会太大。
有关gbn创建索引的一般规则的良好参考链接:https://dba.stackexchange.com/questions/12922/hard-and-fast-rule-for-include-columns-in-index
似乎有几个远程查询正在运行。还要注意一点:如果使用链接服务器连接到这些服务器,则这些表的全部内容必须通过有线方式传输,然后才能联接/过滤。您的计划中的2相当小(4700和20700),但是您的加入条件可能会成倍增加。