Oracle& SQL对我来说很新,我还在学习阶段。
你能帮我调整下面的oracle查询吗? 该表包含~95个lac记录,使用此查询检索数据需要1小时。
您的建议/意见/帮助将不胜感激。
提前致谢。
SELECT A.CNO AS CNO,
A.FNO AS FNO,
A.CID AS CID,
A.IID AS IID
FROM CAC_LKP A,
(SELECT C_DATE, CNO, FNO
FROM (SELECT MAX(CAC_LKP.C_DATE) AS C_DATE,
CAC_LKP.CNO AS CNO,
CAC_LKP.FNO AS FNO
FROM CAC_LKP
WHERE ACTIVE = 'Y'
GROUP BY CNO, FNO)) B
WHERE A.C_DATE = B.C_DATE
AND A.CNO = B.CNO
AND A.FNO = B.FNO
AND A.ACTIVE = 'Y'
主键是通过c_date,iid,active的组合定义的。
答案 0 :(得分:2)
您正在使用聚合来获取变量组的最新日期。然后,您将获得与这些日期匹配的行。
要优化查询,请忘记自联接并改为使用分析函数:
select cno, fno, cid, lid
from (select A.*,
rank() over (partition by cno, fno order by c_date desc) as seqnum
from cac_lkp A
where A.active = 'Y'
) A
where seqnum = 1;
答案 1 :(得分:0)
由于存在大量数据,删除不必要的子查询可以提高性能。确保您在C_DATE,CNO,FNO和A.ACTIVE上有索引。试试这个sql
SELECT A.CNO AS CNO,
A.FNO AS FNO,
A.CID AS CID,
A.IID AS IID
FROM CAC_LKP A,
(SELECT MAX(CAC_LKP.C_DATE) AS C_DATE,
CAC_LKP.CNO AS CNO,
CAC_LKP.FNO AS FNO
FROM CAC_LKP
WHERE ACTIVE = 'Y'
GROUP BY CNO, FNO) B
WHERE A.C_DATE = B.C_DATE
AND A.CNO = B.CNO
AND A.FNO = B.FNO
AND A.ACTIVE = 'Y'