2

クライアントが必要とするクエリ用に書いたこのコードがあります。結果はまさに必要なものを提供してくれますが、応答時間が非常に長くなります。クエリ自体はあまり変わらないのに、クエリの最初の部分は 2 番目の部分よりも時間がかかります。基本的に、これらは UNION ALL を使用して 1 つに結合した 2 つの異なるクエリです。

これを簡素化するために、フィードバックは素晴らしいでしょう。ありがとう

Select * from
(

Select b.Storage_Loc_Nbr as locNbr, b.CurrentLoadCount,b.MMM_Id_Nbr,
(SELECT count(Load_ID) as LoadCount 
    FROM t_load c
    WHERE b.MMM_Id_Nbr=c.MMM_Id_Nbr
    and
    QC_Status_Code like 'R%' and mmm_Facility_Code ='MC' and     Active_Status_Ind='A'
    and
    b.Storage_Loc_Nbr <> c.Storage_Loc_Nbr)as OtherLocCount

    FROM
(

SELECT * FROM

(

    SELECT t_load.Storage_Loc_Nbr, 
    Count(t_Load.Load_Id) AS CurrentLoadCount,
    t_load.MMM_Id_Nbr,
    t_Storage_Location.Storage_Loc_Type_Code

    FROM t_Load LEFT JOIN t_Storage_Location ON t_Load.Storage_Loc_Nbr = t_Storage_Location.Storage_Loc_Nbr
    WHERE (

    ((t_load.MMM_Id_Nbr) between '702004%' and '702011%')
    AND ((t_Load.Active_Status_Ind)='A')
    AND ((t_Load.QC_Status_Code) Like 'R%') 
    AND ((t_Load.MMM_Facility_Code)='MC')
    AND  ((t_Storage_Location.Storage_Loc_Type_Code)='CD')
)

    GROUP BY t_load.MMM_Id_Nbr,
    t_load.Storage_Loc_Nbr,
    t_Storage_Location.Storage_Loc_Type_Code

 HAVING
    Count(t_Load.Load_Id)<=1




    )
    as a

)  
as b


)
as c
Group By
locNbr,CurrentLoadCount, MMM_Id_Nbr, OtherLocCount
having OtherLocCount>0

UNION ALL

Select * from
(

Select b.Storage_Loc_Nbr as locNbr, b.CurrentLoadCount,b.MMM_Id_Nbr,
(SELECT count(Load_ID) as LoadCount 
    FROM t_load c
    WHERE b.MMM_Id_Nbr=c.MMM_Id_Nbr
    and
    QC_Status_Code like 'R%' and mmm_Facility_Code ='MC' and Active_Status_Ind='A'
    and
    b.Storage_Loc_Nbr <> c.Storage_Loc_Nbr)as OtherLocCount

 FROM
(

SELECT * FROM

(

    SELECT t_load.Storage_Loc_Nbr, 
    Count(t_Load.Load_Id) AS CurrentLoadCount,
    t_load.MMM_Id_Nbr,
    t_Storage_Location.Storage_Loc_Type_Code

    FROM t_Load LEFT JOIN t_Storage_Location ON t_Load.Storage_Loc_Nbr = t_Storage_Location.Storage_Loc_Nbr
    WHERE (

    ((t_load.MMM_Id_Nbr) between '702004%' and '702011%')
    AND ((t_Load.Active_Status_Ind)='A')
    AND ((t_Load.QC_Status_Code) Like 'R%') 
    AND ((t_Load.MMM_Facility_Code)='MC')
    AND  ((t_Storage_Location.Storage_Loc_Type_Code)<>'CD') AND
    ((t_load.Storage_Loc_Nbr)<>'clc' AND (t_load.Storage_Loc_Nbr)<>'WHLEAD')    AND
(
((t_load.Storage_Loc_Nbr) Like '%A') 
OR((t_load.Storage_Loc_Nbr) Like '%B')
OR ((t_load.Storage_Loc_Nbr) Like '%C') 
OR ((t_load.Storage_Loc_Nbr) Like '%D') 
OR ((t_load.Storage_Loc_Nbr) Like '%E'))
)

    GROUP BY t_load.MMM_Id_Nbr,
    t_load.Storage_Loc_Nbr,
    t_Storage_Location.Storage_Loc_Type_Code



    HAVING
    Count(t_Load.Load_Id)<=1



    )
    as a

)  
as b


)
as c
Group By
locNbr,CurrentLoadCount, MMM_Id_Nbr, OtherLocCount
having OtherLocCount>0
Order by
LocNbr
4

2 に答える 2

1

組合の最初の部分しか見ていない

http://sqlfiddle.com/#!3/72b6d/9/0は元のクエリに非常に似ていますが、オプティマイザがおそらくそれ自体で行ういくつかの小さな単純化があります。

http://sqlfiddle.com/#!3/72b6d/11/0は、私が書いたテスト データと同じ結果を生成します。より代表的なものでこれを自由に拡張してください。CTEを使用します(以下にコピー)

-- CTE Version
With T As (
Select
  l.Storage_Loc_Nbr,
  l.MMM_Id_Nbr,
  Count(Load_ID) As LoadCount
From 
  t_load l
Where
  l.QC_Status_Code like 'R%' And
  l.mmm_Facility_Code ='MC' And
  l.Active_Status_Ind='A' And
  l.MMM_Id_Nbr between '702004%' and '702011%'
Group By
  l.Storage_Loc_Nbr,
  l.MMM_Id_Nbr
)

Select
  t1.Storage_Loc_Nbr LocNbr,
  t1.LoadCount CurrentLoadCount,
  t1.MMM_Id_Nbr,
  t2.TotalCount - t1.LoadCount OtherLocCount
From
  T t1
    Inner Join (
      Select
        MMM_Id_Nbr,
        Sum(LoadCount) TotalCount
      From
        T  
      Group By
        MMM_Id_Nbr
    ) t2
    On t1.MMM_Id_Nbr = t2.MMM_Id_Nbr
    Left Outer Join
  t_Storage_Location sl
    On t1.Storage_Loc_Nbr = sl.Storage_Loc_Nbr
Where
  sl.Storage_Loc_Type_Code = 'CD' and
  t1.LoadCount <= 1 And
  t2.TotalCount - t1.LoadCount > 0

http://sqlfiddle.com/#!3/72b6d/6/0は、ウィンドウ関数と無償の CTE を使用する別のバージョンです。

-- Window Function Version
With T As (
Select
  l.MMM_Id_Nbr,
  l.Storage_Loc_Nbr,
  Count(Load_ID) Over (Partition By MMM_ID_Nbr, Storage_Loc_Nbr) As LoadCount,
  Count(Load_ID) Over (Partition By MMM_ID_Nbr) As TotalCount
From 
  t_load l
Where
  QC_Status_Code like 'R%' And
  mmm_Facility_Code ='MC' And
  Active_Status_Ind='A' And
  l.MMM_Id_Nbr between '702004%' and '702011%'
)

Select
  t1.Storage_Loc_Nbr LocNbr,
  t1.LoadCount CurrentLoadCount,
  t1.MMM_Id_Nbr,
  t1.TotalCount - t1.LoadCount OtherLocCount
From
  T t1
    Left Outer Join
  t_Storage_Location sl
    On t1.Storage_Loc_Nbr = sl.Storage_Loc_Nbr
Where
  sl.Storage_Loc_Type_Code = 'CD' and
  t1.LoadCount <= 1 And
  t1.TotalCount - t1.LoadCount > 0
于 2012-10-23T23:50:42.923 に答える
0
(SELECT count(Load_ID) as LoadCount  
    FROM t_load c 
    WHERE b.MMM_Id_Nbr=c.MMM_Id_Nbr 
    and 
    QC_Status_Code like 'R%' and mmm_Facility_Code ='MC' and     Active_Status_Ind='A' 
    and 
    b.Storage_Loc_Nbr <> c.Storage_Loc_Nbr)as OtherLocCount

これは相関サブクエリです。つまり、行ごとに実行されます。これは、使用を避けるべきプログラミング手法です。代わりに派生テーブルにしてください。

于 2012-10-23T21:45:13.643 に答える