项目要分页查询科目的父科目,我用递归后,查询要等15秒以上,谁有好办法优化一下,或递归的替代办法,
这个实在太慢了,客户肯定受不了。
代码如下:SELECT * FROM 
(SELECT aa.*,ROWNUM r  FROM 
   ( SELECT  * 
     FROM  ( select distinct a.id, a.ssegmentcode2 m_strAccount, 
             a.ssegmentname2 m_strName,a.nSubjectType m_lSubjectType,
             a.nparentsubjectid nParentSubjectId,a.nBalanceDirection
             from sett_glsubjectdefinition a 
             start with ssegmentcode2 in  
               ( select c.sSubjectCode from Sett_GlEntry c 
                 where  c.nCurrencyID = 1 
                 and c.nOfficeID = 1 
                 and c.nStatusID > 0  
                 and c.mAmount != 0  
                 and c.dtExecute between to_date('2010-02-22','yyyy-MM-dd') 
                 and to_date('2010-02-22','yyyy-MM-dd')
                 )  
                 connect by prior nparentsubjectId = id 
                 order by a.ssegmentcode2 
               ) 
    ) 
aa )  WHERE r  BETWEEN 1 AND 10

解决方案 »

  1.   

    start with
    connect by
    这些语法我第一次看到,我只会使用非递归的查询,如果你可以简要说明下查询需求,我倒是可以重写一个不带递归的查询。
      

  2.   

    首先确定哪段SQL语句引起的,的内嵌的SQL?还是....分段执行看看,哪里影响到速度...
      

  3.   

    实际就是下面的这段递归慢(为看到清楚我把简化了下),因为页面要取出多项值,还用到其他sql语句和这条递归sql配合,下面的递归在plsql中要近5秒,可想加上其它的查询sql会更慢。
    回复2楼:要求出父科目号,可能必须要用递归,其它方法还没听说过,请高手指教!
    SELECT *  
      FROM ( select distinct a.id, a.ssegmentcode2 m_strAccount,  
      a.ssegmentname2 m_strName,  a.nparentsubjectid nParentSubjectId   from sett_glsubjectdefinition a  
      start with ssegmentcode2 in   
      ( select c.sSubjectCode from Sett_GlEntry c  
      where  c.mAmount != 0   
      and c.dtExecute between to_date('2010-02-22','yyyy-MM-dd')  
      and to_date('2010-02-22','yyyy-MM-dd')
      )   
     connect by priornparentsubjectId = id  
      order by a.ssegmentcode2  
      )  
      

  4.   

    SELECT *   
      FROM ( select distinct a.id, a.ssegmentcode2 m_strAccount,   
      a.ssegmentname2 m_strName, a.nparentsubjectid nParentSubjectId from sett_glsubjectdefinition a   
      start with ssegmentcode2 in 
      ( select c.sSubjectCode from Sett_GlEntry c   
      where c.mAmount != 0   
      and c.dtExecute between to_date('2010-02-22','yyyy-MM-dd')   
      and to_date('2010-02-22','yyyy-MM-dd')

      )   
     connect by priornparentsubjectId = id 
      order by a.ssegmentcode2   
      )   
    红色这段慢?
      

  5.   

    红色代码执行很快,start with ssegmentcode2 in    connect by priornparentsubjectId = id 主要是用到递归慢,它要根据红色代码取出的科目号c.sSubjectCode 用递归的方法求出它的所有父科目。
      

  6.   

    我认为递归不好优化,这段sql也不复杂,看看有没有递归的替代方法。
      

  7.   

    ssegmentcode2 有索引?
    order by a.ssegmentcode2 排序也耗时间.. 
    你是原始数据和现在只想结果贴出来看看那
      

  8.   

    ssegmentcode2有没有索引不清楚,原始数据不方便贴,因为有很多客户账户、金额信息。实在不行,我问问技术经理吧。