请问如何提高如下sql语句的性能select C1,C2,R1, R2, R3 from table1 where C1 
in
(
select DISTINCT  R1 from table1 
where C2 in(...)
)
or C1 in
(
select DISTINCT  R2 from table1 
where C2 in(...)
)
or C1 in
(
select DISTINCT R3 from table1 
where C2 in(...)
)
select DISTINCT R3(或R1, R2) from table1 
where C2 in(...同样条件)
同样的结果集重复查询了三次,而且作为同一个where的条件  有什么办法改进一下,比如重用结果集合,只查一次使用三次,

解决方案 »

  1.   

    select C1,C2,R1, R2, R3 from table1 where C1 
    in
    (select DISTINCT  R1 from table1 
    )
    or C1 in
    (
    select DISTINCT  R2 from table1 
    )
    or C1 in
    (
    select DISTINCT R3 from table1 
    ) and c1 in(.........)既然是同样的条件,当然可以拿出来
      

  2.   

    等等
    既然都是同一张表,还需要用嵌套的select吗?
    select C1,C2,R1, R2, R3 from table1 
    where C1 in r1 
    or c1 in r2
    or c1 in r3 
    and c2 in (...........)在更正刚才最后一个 c1应该是c2
      

  3.   

    armyyd(不会游泳的猫)谢谢你的帮助 不过你的方法是不可行的!你的语句和我的语句是相差太远,甚至根本就不能执行
      

  4.   

    我想要的是把select DISTINCT R3(或R1, R2) from table1 
    where C2 in(...同样条件)加一个别名,作为一个结果集在where语句中使用多次,以提高效率
      

  5.   

    select C1,C2,R1, R2, R3 from table1 A where exists
    (
    select 1 from table1 B where ( A.C1 = B.R1 OR A.C1 = B.R2 OR A.C1 = B.R3 ) AND C2 in(...)
    )
    在R1 , R2 , R3上有索引
      

  6.   

    不用exists了吧,直接where ( C1 = R1 OR C1 = R2 OR C1 = R3 ) AND C2 in(...)就可以了吧
      

  7.   

    SELECT a.c1,a.c2,a.r1,a.r2,a.r3 
      FROM table1 a
     WHERE EXISTS
           (SELECT 1 
              FROM table1 b 
             WHERE a.c1 IN (b.r1,b.r2,b.r3)
               AND b.c2 in (....) )
      

  8.   

    刚开始学数据库
    同意楼上的
    是不是也可以如下做:
    SELECT a.c1,a.c2,a.r1,a.r2,a.r3 
      FROM table1 a, table1 b
     WHERE b.c2 in (....) and (a.c1=b.r1 or a.c1=b.r2 or a.c1=b.r3)
    请多多指正!
      

  9.   

    SELECT a.c1,a.c2,a.r1,a.r2,a.r3 
      FROM table1 a, table1 b
     WHERE (a.c1=b.r1 or a.c1=b.r2 or a.c1=b.r3) and 
           b.c2 in (....) 最好将b.c2 in (....) 改成exists的形式.
      

  10.   

    DISTINCT去掉
    用inner join代替in,exist
    用union all代替or
    看explain plan,建索引,避免full table access
      

  11.   

    SELECT C1,C2,R1, R2, R3 
    FROM table1 
    WHERE (C1 = R1 OR C1 =R2 OR C1 =R3)
    AND C2 IN (...)
      

  12.   

    使用exists代替in速度快很多很多.
    select C1,C2,R1, R2, R3 from table1 C where 
    exists select 1 from 
    (
    (
    select DISTINCT  R1 from table1 
    where C2 in(...)
    )
    union
    (
    select DISTINCT  R2 from table1 
    where C2 in(...)
    )
    union
    (
    select DISTINCT R3 from table1 
    where C2 in(...)
    )
    ) K
    where C.c1=K.R1
      

  13.   

    采用联合和联接实现:
    select C1,C2,R1, R2, R3 from table1 
    (
    select DISTINCT r1 from a
    (
    select DISTINCT R1,c2 from table1 
    union 
    select DISTINCT R2,c2 from table1 
    union
    select DISTINCT R3,c2 from table1 
    ) a
    where a.c2 in(...)
    ) table2
    where table1.C1 = table2.r1(+)
      

  14.   

    用内嵌视图:
    select C1,C2,R1, R2, R3 from table1,(select DISTINCT  R1,R2,R3 from table1 
    where C2 in(...)) t where C1=t.R1 or C2=t.R2 or C1=t.R3
      

  15.   

    给你一点提示
    所谓表就是记录集
    当然拉,你可以把记录集当做表使用
    比方说:
    select * from  (select * from tab_1 where condition) x ,(select * from tab_2) y, tab_3 z
    where condition;