日期:2014-05-16  浏览次数:20807 次

求下面SQL语句优化。
select companyid bmdm1,count(distinct claimno) pretidiao from v_claimInfo2010
where isPreResearch='1' 
and claimno not in (select claimno from claimCoordinateRes where type= '2' )  
and (status='5') and 1=1  
and (trunc(endorOverDate,'DD') >= to_date('2010-01-01','YYYY-MM-DD') and trunc(endorOverDate,'DD') <=to_date('2010-12-31','YYYY-MM-DD'))  
group by companyid
order by companyid

初步分析是not in影响效率,据说写成EXISTS会提高效率,如何写呢?

------解决方案--------------------
SQL code

select companyid bmdm1,count(distinct claimno) pretidiao 
from v_claimInfo2010
where isPreResearch='1'   
and (status='5')   
and (trunc(endorOverDate,'DD') between to_date('2010-01-01','YYYY-MM-DD') and to_date('2010-12-31','YYYY-MM-DD'))   
and not exists (select 1 from claimCoordinateRes where type='2' and claimCoordinateRes.claimno=v_claimInfo2010.claimno)
group by companyid
order by companyid;

------解决方案--------------------
--飞一般的感觉
SELECT a.Companyid Bmdm1, COUNT(DISTINCT a.Claimno) Pretidiao
FROM v_Claiminfo2010 a, Claimcoordinateres b
 WHERE a.Claimno = b.Claimno
AND a.Endoroverdate >= DATE
 '2010-01-01'
AND a.Endoroverdate <
To_Date('2010-12-31 00:00:01', 'yyyy-mm-dd hh:mi:ss')
AND a.Ispreresearch = '1'
AND b.TYPE = 2
AND a.Status = '5'

------解决方案--------------------
还有在你的claimCoordinateRes 表中的claimno列上建立一个索引