以前运行速度还挺好的,这次把试图改了一下,性能就出现问题了,硬是不知道该怎么处理了,待我细细道来。oracle 9i数据库有试图 v 和表 t情况如下:直接查表:select id from t where id<5;
这样查起来很快。返回结果为:1,2,3,4接下来查试图:select * from v where tid in(1,2,3,4);
这样查起来很快。但是换成下面查询就很慢很慢了。
select * from v where tid in(select id from t where id<5);一直不解为什么会这样,哪位帮忙解决下,小弟不胜感激。

解决方案 »

  1.   


    --v,t表都建立索引,應不會很慢的
    select * from v where exists(select 1 from t where t.id=v.tid and t.id<5);
      

  2.   

    我再补充下我的问题,原始的SQL脚本是子查询里面依然有一个in查询:如下select * from v where tid in(select id from t where b in(1));
    select id from t where b in(1);返回结果为:1,2,3,4我试过以下几种查询方式:
    select * from v where tid in(select id from t where b in(1));select * from v where tid in(select id from t where b=1);select * from v where exists (select 'X' from t where b in(1) and b.id=a.tid);select * from v where exists (select 'X' from t where b=1 and b.id=a.tid);这四种查询结果是一样的,很慢很慢。
      

  3.   

    你试试这个:
    select * from v where tid in(select  /*+no_unnest*/  id from t where id<5);
      

  4.   

    SELECT * FROM v WHERE TID in (SELECT ID FROM T WHERE ROWNUM<5 ORDER BY ID ASC)
      

  5.   


    创建视图的时候,视图里面已经写了一些SQL调优策略。
      

  6.   

    我这边问题是解决了,导致速度慢的原因是视图V嵌套了另外一张视图,但是为什么嵌套视图就会变的这么慢,我还真不清楚。两张视图的关系类似下面:
    create view v_a
    as
    select t.a,t.b,t.c from t_a t;create view v_b
    as
    select a.*,(select tc from t_c where ta=a.a) tc from v_a a;结果查
    select * from v_b where a in(select a from t_a where a in(1,2,3,4));这样就很慢select * from v_b where a in(1,2,3,4);这样很快select * from v_a where a in(select a from t_a where a in(1,2,3,4));这样也很快。还不知道为什么会这样。
      

  7.   

    t表数据量比较大吧,注定很慢, select id from t where id<5 你得到的ID记录数有多少? 和in (1,2,3,4)比较看看就知道了