我们如何在此处删除具有良好性能的子查询。如果我附加此子查询,它会提供正确的输出,但由于可用记录的数量而减慢了搜索速度。但在删除它之后,它会增加多倍。任何人都可以帮助省略这个子查询或任何改进以下查询的内容。
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)
答案 0 :(得分: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)
答案 1 :(得分:0)
也许:
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
你也可以替换ISNULL函数:
(Base_Insurance_File_Cnt is null OR Base_Insurance_File_Cnt = insurance_file_cnt)