Oracle是10G的
我在PL/SQL执行以下语句3个半钟都没有执行完
update t_p_ds a set 
dwbh = (select dwbh from t_p_dw where  ROWNUM = 1 and nsrbm = a.col002 and zzjgdm = replace(a.col004,'-','')) where exists(select 1 from t_p_dw where nsrbm = a.col002 and zzjgdm = replace(a.col004,'-','')) 
and a.dwbh is null and a.col002 is not null and a.col004 is not null
-------------
执行上面语句3个钟都没完
我是用SQL的,SQL表面上看是效率不好,但是用Update dwbh from T_P_ds a inner join T_P_DW on .... 这种方法Oracle又提示语法错误怎么优化这条SQL 
谢谢

解决方案 »

  1.   

    我是用MS-SQL的,上面SQL表面上看是效率不好,但是用MS-SQL的写法Update dwbh from T_P_ds a inner join T_P_DW on .... 这种方法Oracle又提示语法错误
      

  2.   

    确认表t_p_dw列nsrbm,zzjgdm和表t_p_ds列col002上有索引,
    最好把rownum=1修改为rownum<2,
    另:你子查询有重复数据吗?
      

  3.   

    另:你子查询有重复数据吗?
    ------------
    可能会有,所以加上rownum=1谢谢
      

  4.   

    update t_p_ds a
    set    dwbh = (select dwbh
                   from   t_p_dw
                   where  ROWNUM < 2
                          and nsrbm = a.col002
                          and zzjgdm = replace(a.col004,'-',''))
    where  exists (select 1
                   from   t_p_dw
                   where  nsrbm = a.col002
                          and zzjgdm = replace(a.col004,'-',''))
           and a.dwbh is null
           and a.col002 is not null
           and a.col004 is not null
    --------------------------------------
    还是不行。。没有反应
    我两个表分别是T_P_DW 40W条 T_P_DS 30W条
    有没有更好的写法?
      

  5.   

    把exists换成in试试
    检查一下关键字段的索引 nsrbm 和 a.col002上有索引吗?
      

  6.   

    建议你写个存储过程把还是.说下我的大概感觉。做个游标把 
             test
             select b.主键,a.dwbh,a.1 
             from   t_p_dw a,t_p_ds b
             where  nsrbm = a.col002 
             and    zzjgdm = replace(a.col004,'-','')
             and    ROWNUM = 1update t_p_ds a 
    set   dwbh = 
          (
             select dwbh 
             from   test 
             where  a.主键 = test.主键       
          ) 
    where a.dwbh is null 
    and a.col002 is not null 
    and a.col004 is not null
    and exists
          (
             select 1 
             from   test 
             where  a.主键 = test.主键   
          )  大概就是这个意思,好久没有写过ORACLE的代码了.
    有些关键词也忘了,凑合的看个意思把.
      

  7.   

    楼主,首先你那sql中select dwbh from t_p_dw where ROWNUM = 1 and nsrbm = a.col002 and zzjgdm = replace(a.col004,'-',''用到了两次,且都用到了replace,自然效率不高
    提高你那sql效率
    1,在操作的列上建立索引
    2,a is null或a is not null 改用a>0或a>''或a<0 a<''
    3,避免在索引上使用not
    由此,更改你的sql如下:DECLARE
      v_dwbh varhar2(128) := '';
    BEGIN
      SELECT dwbh
      INTO v_dwbh
      FROM t_p_dw
      WHERE ROWNUM = 1
            AND nsrbm = a.col002
            AND zzjgdm = REPLACE(a.col004, '-', '');
      IF dwbh > ''--若dwbh不为空,则更新,<>号效率底下
      THEN
        UPDATE t_p_ds a SET dwbh = v_dwbh;
      END IF;
    END