0

我有一个创建和运行 SQL 查询的 SP。最终结果用于某些报告。这个查询虽然需要很长时间才能运行(大于 30 秒是很长的时间,有时会持续超过一分钟)。

我已经阅读了一些关于基于集合的查询的内容,但我不清楚这个特定的查询是否适合,或者是否有办法转换它。我读过的关于基于集合的大部分内容似乎都在谈论将循环更改为集合,例如这两个Programming SQL in a Set-Based WayAn Introduction to set-based vs procedural programming methods 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 也更长,但这可以转换为基于集合的查询吗?

如果是这样,我将如何开始这样做?

更新

这是实际执行查询计划

4

1 回答 1

1

根据您发布的执行计划,一些索引可能会有所帮助。特别是在 SDIPRODUCTIONCHEMICSRY.ProductionId 上。它正在对约 1 百万行进行表扫描。Id 是一IDENTITY列并设置为 PK 吗?

对这些索引建议持保留态度。它们特定于仅针对该计划运行的查询,不会告诉您它们将如何影响命中该表的其他查询。它们是很好的起点,但您仍应分析它们让您更改的内容,以确保索引不会太多。

从 gbn 创建索引的一般规则的一个很好的参考链接:https ://dba.stackexchange.com/questions/12922/hard-and-fast-rule-for-include-columns-in-index

似乎有几个远程查询正在运行。还有一个小提示:如果您使用链接服务器连接到这些服务器,则这些表的全部内容必须通过网络传输,然后才能加入/过滤。您计划中的 2 相当小(4700 和 20700),但您的加入标准可能会成倍增加。

于 2018-12-07T16:38:28.813 回答