SELECT ID,CODE FROM V_PORT_CS WHERE CODE = '//1'    -- 40.11 SECONDS
SELECT ID,NAME FROM V_PORT_CS WHERE NAME = '1'     --0.078 SECONDS如上:只要加上CODE 这个字段,去查V_PORT_CS这个视图就会超级慢(40.11秒),用其他字段都不会有问题,比如用name等去查。以下是其他相关信息:1.   SELECT COUNT(*) FROM V_PORT_CS;    -- 322212该视图共32万条数据2.视图定义create or replace view v_port_cs as
(select
   ID,
   MBID,
   MRID,
   MSID,
   coderule(CODE,ID,'E_NPORT') AS CODE,
   NAME,   RESOURCEID
from e_nport
where kindcode in (-99,1,2,3,6,200,101,9,108,4,8,7));  3.  函数定义create or replace function coderule(object_code in varchar2,tableid in number,tablename in varchar2) return varchar2 is
  rvalue varchar2(1000);
  eqcode varchar2(1000);
  efcode varchar2(1000);
  efidx   number;
  modulecode varchar2(1000);
  begin
       if tablename='E_NPORT' then 
       begin
       select efid into efidx from E_NPORT where id=tableid;
       select (select name from e_equipment where id=e.object_id) into eqcode from e_eqframe e where e.id=efidx;
       EXCEPTION
              when others then
              dbms_output.put_line(sqlerrm);
       end;
       begin
       select (select code from e_eqframe where id=e.efid) into efcode from E_NPORT e where e.id=tableid;
       EXCEPTION
              when others then
              dbms_output.put_line(sqlerrm);
       end;
       begin
       select (select code from e_module where id=e.moduleid) into modulecode from E_NPORT e where e.id=tableid;
       EXCEPTION
              when others then
              dbms_output.put_line(sqlerrm);
       end;
       if modulecode IS NULL then
          rvalue:=eqcode||'/'||efcode||'/'||object_code;
       end if;
       if modulecode IS NOT NULL then
          rvalue:=eqcode||'/'||efcode||'/'||modulecode||'/'||object_code;
       end if; 
    end if;
    return rvalue;
end coderule;5对于 视图中的name,code 等均做了索引以上。求解!

解决方案 »

  1.   

    如果用code字段去查等于把整表都用coderule计算完了,再找计算结果等于的记录,其他字段去查是先找符合条件的记录再用coderule计算code的值验证这个说法很简单很简单,定义一个序列,每执行一次coderule函数把该序列加1,然后试试每次select之后序列加了多少
      

  2.   

    是你的这种操作将 index 废弃了,name,code 做了索引,name 会被view直接映射到实体表name字段,也就使用索引。你的view的code是合并多个字段而来,这样就不会使用index了,如果把view的合并的字段拆分,你逐个使用
    字段=value,这样字段的索引才能被使用到。所以这个才是sql慢的原因。
    view只是一个逻辑映射关系,用来简化sql的,合理的使用非常重要。