2017-07-31 6 views
0

ここではサブクエリを良好なパフォーマンスで削除できますか?このサブクエリを追加すると、正しい出力が得られますが、利用可能なレコード数のため検索が遅くなります。しかし、これを取り除いた後は、複数の折り目で増加します。誰でもこのサブクエリを省略したり、以下のクエリを改善するのに役立つことができます。多分良いパフォーマンスでサブクエリを削除するにはどうすればいいですか

SELECT insurance_folder_cnt, MAX(lapsed_date) lapseddate 
FROM insurance_file ifl 
WHERE insurance_file_status_id=1 AND insurance_file_type_id in (2, 5, 6, 8, 9) AND (ISNULL(Base_Insurance_File_Cnt,0) = 0 OR ISNULL(Base_Insurance_File_Cnt, 0) = insurance_file_cnt) AND ISNULL(out_of_sequence_replaced, 0) = 0 
AND policy_version = (SELECT MAX(policy_version) FROM Insurance_File ifld WHERE ifl.insurance_folder_cnt=ifld.insurance_folder_cnt) GROUP BY insurance_folder_cnt having policy_version = max(policy_version) 
+2

良い答えを得るためには、テキストとしてあなたの探求を貼り付けてください。そして、より良いサンプルデータ、実行計画を提供する。また、データベースは 'sql'は言語ですが、RDMSによって異なるアプローチがあるかもしれません – Prisoner

+0

SELECT insurance_folder_cnt、MAX(lapsed_date)lapseddate FROM insurance_file ifl \t \t where insurance_file_status_id = 1 AND insurance_file_type_id in(2、5、6、 8、9) \t \t AND(ISNULL(Base_Insurance_File_Cnt、0)= 0 OR ISNULL(Base_Insurance_File_Cnt、0)= insurance_file_cnt) \t \t AND ISNULL(out_of_sequence_replaced、0)= 0 \t \t --and policy_version =(SELECT MAX (policy_version)FROM Insurance_File ifld WHERE ifl.insurance_folder_cnt = ifld.insurance_folder_cnt \t \t GROUP BYは、[編集]あなたのポストの代わりに、このために申し訳ありませんコメント –

+0

を持つ \t \tをしてくださいinsurance_folder_cnt、私はここに新しいです。 –

答えて

0
Please use this for better performance: 

SELECT insurance_folder_cnt , 
     MAX(lapsed_date) lapseddate 
FROM insurance_file ifl 
     CROSS APPLY (SELECT MAX(policy_version) AS maxVersion 
         FROM  Insurance_File ifld 
        ) TmpMaxVersion 
WHERE insurance_file_status_id = 1 
     AND insurance_file_type_id IN (2, 5, 6, 8, 9) 
     AND (ISNULL(Base_Insurance_File_Cnt, 0) = 0 
       OR ISNULL(Base_Insurance_File_Cnt, 0) = insurance_file_cnt 
      ) 
     AND ISNULL(out_of_sequence_replaced, 0) = 0 
     AND policy_version = TmpMaxVersion.maxVersion 
GROUP BY insurance_folder_cnt 
HAVING policy_version = MAX(policy_version) 
+0

私はフルグループ別を使用しているため、これは機能しません。 –

0

:あなたはまた、ISNULL関数を置き換えることができます

SELECT insurance_folder_cnt, MAX(lapsed_date) lapseddate 
FROM insurance_file ifl 
join (select insurance_folder_cnt inffld, max(policy_version) pol_ver from insurace_file group by insurance_folder_cnt) ifld 
on (ifld.inffld = ifl.insurance_folder_cnt and pol_ver = ifl.policy_version) 
WHERE insurance_file_status_id=1 AND insurance_file_type_id in (2, 5, 6, 8, 9) 
AND (ISNULL(Base_Insurance_File_Cnt,0) = 0 OR ISNULL(Base_Insurance_File_Cnt, 0) = insurance_file_cnt) 
AND ISNULL(out_of_sequence_replaced, 0) = 0 
GROUP BY insurance_folder_cnt 

(Base_Insurance_File_Cnt is null OR Base_Insurance_File_Cnt = insurance_file_cnt) 
関連する問題