表T(id,type,pid,……)的自关联,该表数据400Wselect a.* from (select * from t where t.type='a') a,(select * from t where t.type='b') b
where a.pid=b.pid请问该如何优化,谢谢!

解决方案 »

  1.   

    select a.*
    from t a
    where a.type='a' and exists(select 1 from t b where b.type='b' and a.pid=b.pid)
      

  2.   

    这样速度是增加了不少,但是无法查出关联的记录中同一个字段的数据
    也即类似select a.type,a.pid,a.name,b.name from (select * from t where t.type='a') a,(select * from t where t.type='b') b
    where a.pid=b.pid
      

  3.   

    那只能按你这个语句写了:
    select a.type,a.pid,a.name,b.name from (select * from t where t.type='a') a,(select * from t where t.type='b') b
    where a.pid=b.pid
      

  4.   

    这个表400W,不能按type分区放么?
    我试了下2W就很慢,用不用exists有将近10倍的差别
      

  5.   

    有谁能说说HASH JOIN RIGHT SEMI和HASH JOIN都是在干什么?-----------------------------------------------------------------------------
    | Id  | Operation            | Name | Rows  | Bytes | Cost (%CPU)| Time     |
    -----------------------------------------------------------------------------
    |   0 | SELECT STATEMENT     |      | 19010 |   891K|   175   (6)| 00:00:03 |
    |*  1 |  HASH JOIN RIGHT SEMI|      | 19010 |   891K|   175   (6)| 00:00:03 |
    |*  2 |   TABLE ACCESS FULL  | T1   | 19010 |   204K|    87   (5)| 00:00:02 |
    |*  3 |   TABLE ACCESS FULL  | T1   | 19010 |   686K|    87   (5)| 00:00:02 |
    --------------------------------------------------------------------------------------------------------------------------------------------------------
    | Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
    ---------------------------------------------------------------------------
    |   0 | SELECT STATEMENT   |      |    72M|  3584M|  1416  (89)| 00:00:17 |
    |*  1 |  HASH JOIN         |      |    72M|  3584M|  1416  (89)| 00:00:17 |
    |*  2 |   TABLE ACCESS FULL| T1   | 19010 |   278K|    87   (5)| 00:00:02 |
    |*  3 |   TABLE ACCESS FULL| T1   | 19010 |   686K|    87   (5)| 00:00:02 |
    ---------------------------------------------------------------------------
      

  6.   

    现在第三方软件都可以查看SQL的执行计划,ORACLE自身也有这个功能,不过没有第三方软件这么方便,但是ORACLE自身的执行计划比较详细
      

  7.   


    在sqlplus里面,set autotrace on就可以了啊
      

  8.   

    pid和type有索引吗
    pid+type是unique key 吗
    select a.* from t a, t b
    where a.pid=b.pid and a.type='a' and b.type='b'这句sql的执行计划和原先的一样吗
      

  9.   

    pid和type是有索引的,你说的这个和我原来的意思是一样的,稍微调整一下你的SQLSQL codeselect a.*,b.mainID from t a, t b
    where a.pid=b.pid and a.type='a' and b.type='b'