2017-06-29 10 views
-4

私は速くするために改善が必要な選択クエリがあります。SQLクエリが非常に遅く改善する必要があります

SELECT 
    st.SigHistUID, st.RelationType, st2.RelationType, 
    case 
     when st.RelationType <> st2.RelationType 
      then 'N' 
      else st.RelationType 
    end as [NewRelationType], 
    st.ToSigUID , sh.FullDescription, sh.SigHistUID AS ToSigHistUID 
FROM 
    [Stackability] (nolock) st 
INNER JOIN 
    [SignatureHistory] (nolock) sh ON sh.SigUID = st.ToSigUID 
            AND sh.SigHistUID = (SELECT TOP 1 SigHistUID FROM [SignatureHistory] (nolock) tmp where tmp.SigUID = sh.SigUID ORDER BY SigHistUID DESC) 
INNER JOIN 
    [SignatureHistory] (nolock) sh2 ON st.SigHistUID = sh2.SigHistUID 
INNER JOIN 
    Stackability (nolock) st2 on st2.ToSigUID = sh2.SigUID and st2.SigHistUID = sh.SigHistUID 
WHERE 
    st.SigHistUID < 1000 and 
    st.RelationType <> st2.RelationType 
ORDER BY 
    st.SigHistUID 
+8

を!テーブル構造は何ですか?これらのテーブルにはどのようなインデックスが定義されていますか?各表にはどのような種類のデータ(**ボリューム**の観点から)がありますか? –

+4

最低限、[実際の実行計画を含める](https://stackoverflow.com/a/7359705/1260204)、[計画を貼り付ける](https://www.brentozar.com/pastetheplan)を使用することができます/)あなたの質問にリンクを共有してください。また、[あなた自身で読む](https://stackoverflow.com/a/759097/1260204)、おそらくあなたのクエリでパフォーマンスの問題を把握することができます。最後に、実行中のクエリとともに[schema ddl](https://en.wikipedia.org/wiki/Data_definition_language)を含めます。 – Igor

+0

SignatureHistoryのサイズによっては、副問合せの結果が索引付けされていないため、副問合せによってsh結合での処理が遅くなっている可能性があります。おそらくそのサブクエリを避ける方法を見つけるでしょう。 – justiceorjustus

答えて

0

トップ1の条件を取り出した試してみてください。これは**あまりにも少し**情報です

SELECT 
    st.SigHistUID, st.RelationType, st2.RelationType, 
    case 
     when st.RelationType <> st2.RelationType 
      then 'N' 
      else st.RelationType 
    end as [NewRelationType], 
    st.ToSigUID , sh.FullDescription, sh.SigHistUID AS ToSigHistUID 
FROM 
    [Stackability] (nolock) st 
INNER JOIN 
    (select distinct siguid, max(SigHistUID) OVER(PARTITION BY siguid) as SigHistUID from [SignatureHistory] (nolock)) sh ON sh.SigUID = st.ToSigUID 

INNER JOIN 
    [SignatureHistory] (nolock) sh2 ON st.SigHistUID = sh2.SigHistUID 
INNER JOIN 
    Stackability (nolock) st2 on st2.ToSigUID = sh2.SigUID and st2.SigHistUID = sh.SigHistUID 
WHERE 
    st.SigHistUID < 1000 and 
    st.RelationType <> st2.RelationType 
ORDER BY 
    st.SigHistUID 
+0

大変ありがとうございます。 –

関連する問題