update AA set gldescription = 
     (select max(RateDescription) from BB 
      where trim(msifilesiteid) =  AA.msifilesiteid and 
      Trim(ratecode) = AA.GLCode and 
      length(trim(ratecode)) > 3)
      where (glcode is not null or glcode <> '') and glDescription is null;这是一个存储过程的一部分,每天执行,其它的部分执行都很快,但到了这里就好半天才过去,AA数据量大概每天有3000条记录,BB表中大概有300000条记录, 每天增加。不知道我的SQL到底哪里有问题,执行实在是太慢了,大家帮忙看看,谢谢!!

解决方案 »

  1.   

    先创建个临时表来存储BB表的数据吧,
    然后在更新AA表,这样会快些!:)
      

  2.   

    又是 is not null 又是or 还有<>
    这个SQL能快起来嘛
      

  3.   

    SQL从表面看有两几个致命问题:问题一:
    -----------------------------------
    where (glcode is not null or glcode  <> '') and glDescription is null
    这样写好像没有意义吧,在Oracle里
    【glcode  <> ''】等价于【glcode is not null】
    看这SQL好想是在SQL Server里的吧。问题二:
    is null 不能使用索引,因为AA表每天增加3000左右,所以记录也不会少了,
    全表扫描成本不小,所以需要使用函数索引解决 is null函数问题,
    例: create index decode(glDescription,null,1,0);然后在Where中使用
    ....and decode(glDescription,null,1,0) = 1
    这样的转化。问题三:
    set字段使用查询,你这样做存在两个问题,
    在其中一个问题和问题二一样,追加函数索引,
    解决Trim、length等函数不使用索引问题,
    然后使用Merge函数改进,Merge那本Oracle书
    都有,自己一查就知道了。
    select max(RateDescription) from BB  
          where trim(msifilesiteid) =  AA.msifilesiteid and  
          Trim(ratecode) = AA.GLCode and  
          length(trim(ratecode)) > 3以上都照做,保证你能够写出好SQL。
      

  4.   

    你跑一下执行线索,看看你的时间都花在那里了
    我看你的语句效率不太高呀  比如说  glcode is not null or glcode  <> ''
    你无意中加了太多的索引   
      

  5.   

    将这句:where (glcode is not null or glcode  <> '') and glDescription is null
    换成:where length(trim(ratecode))>0 and glDescription is null
      

  6.   

    需要看你的两个表之间的关联了,如果简单的条件就可以唯一确定一条记录,没有必要加那么多了
    update (select AA.gldescription as AA_gldescription , max(BB.RateDescription) BB_RateDescription from BB,AA 
          where BB.msifilesiteid =  AA.msifilesiteid 
             and  BB.ratecode = AA.GLCode )
    set AA_gldescription  = BB_RateDescription 
      

  7.   

    or glcode  <> ''不需要
      

  8.   

    首先谢谢各位的答复,ehuman看的很准,这个SQL原本是在SQL Server里的,我现在需要将它转到Oralce,我先试试各位的提议,稍后给大家答复, 很感谢大家的帮助!!
      

  9.   

    我逐一的把各种条件去掉然后执行SQL, 影响最大的是当我把trim函数去掉后,速度提高了N倍,下面是我优化后的SQL:
    update AA set gldescription = 
         (select max(RateDescription) from BB 
          where msifilesiteid =  AA.msifilesiteid and 
          ratecode = AA.GLCode and length(ratecode) > 3)
          where glcode is not null and decode(glDescription,null,1,0) = 1;
    速度比以前快多了,谢谢各位的帮助!
      

  10.   

    不是很明白,,
    在数据库里做了一下测试
    glcode  <> ''和glcode  = ''什么都查不出
    菜鸟求教了,,