日期:2014-05-17  浏览次数:20776 次

求优化一段sql语句

 SELECT * FROM (SELECT page.*, ROWNUM AS rn FROM (    
  select a.deptid,a.phone_brand as phonebrand,a.model_name as modelname ,       
  (select count(1) from STOCKS_PHONES c where substr(c.PHONE_IMEI,1,8)=b.MODEL_IMEI and c.status=0  and c.deptid like '001%' and c.deptid=a.deptid)  as count,        
  (select count(1) from STOCKS_PHONES c where substr(c.PHONE_IMEI,1,8)=b.MODEL_IMEI and c.status=1 and c.PHONE_STATUS = 1 and c.deptid like '001%' and c.deptid=a.deptid)  as count1,       
  (select count(1) from STOCKS_PHONES c where substr(c.PHONE_IMEI,1,8)=b.MODEL_IMEI and c.deptid like '001%' and c.deptid=a.deptid)  as count3,       
  (select d.deptname from eadept d where d.deptid = substr(a.deptid,0,LENGTH(a.deptid)-6)) as countyname,       
  (select d.deptname from eadept d where d.deptid = substr(a.deptid,0,LENGTH(a.deptid)-3)) as hallname,       
  (select d.deptname from eadept d where d.deptid = a.deptid) as deptname      
   from stocks_phones a,stocks_phone_model b  WHERE     a.MODEL_ID=b.MODEL_ID and                         a.DEPTID like '001%'
   group by a.deptid,a.phone_brand,a.model_name,b.MODEL_IMEI 
    ) page) WHERE rn BETWEEN 1 AND 20

------解决方案--------------------
 rn BETWEEN 1 AND 20  

分页算法改下,网上很多,google下


(select d.deptname from eadept d where d.deptid = substr(a.deptid,0,LENGTH(a.deptid)-6)) as countyname,       
  (select d.deptname from eadept d where d.deptid = substr(a.deptid,0,LENGTH(a.deptid)-3)) as hallname,       
  (select d.deptname from eadept d where d.deptid = a.deptid) as deptname 

似乎是部门上下级关系,看能不能用connect by处理掉