Oracle数据库数据恢复、性能优化

找回密码
注册
搜索
热搜: 活动 交友 discuz
发新帖

8

积分

1

好友

20

主题
1#
发表于 2013-1-14 15:59:06 | 查看: 3667| 回复: 2
1.首先以scott用户下的emp,dept表为例执行如下sql:
SQL> explain plan for
  2  select  ename from emp
  3  where not exists (select  1  from dept where dept.deptno=emp.deptno);

Explained.
SQL> select * from table(dbms_xplan.display);

------------------------------------------------------------------------------
| Id  | Operation          | Name    | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |         |     1 |    12 |     3   (0)| 00:00:01 |
|   1 |  NESTED LOOPS ANTI |         |     1 |    12 |     3   (0)| 00:00:01 |
|   2 |   TABLE ACCESS FULL| EMP     |    14 |   126 |     3   (0)| 00:00:01 |
|*  3 |   INDEX UNIQUE SCAN| PK_DEPT |     4 |    12 |     0   (0)| 00:00:01 |
------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   3 - access("DEPT"."DEPTNO"="EMP"."DEPTNO")

2.现在我的需求是希望让dept表为驱动表。于是执行下列sql:
SQL> explain plan for
  2      select  /*+ leading(dept) */ ename from emp
  3      where not exists (select  1  from dept where dept.deptno=emp.deptno);

Explained.

SQL>
SQL> select * from table(dbms_xplan.display);

------------------------------------------------------------------------------
| Id  | Operation          | Name    | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |         |     1 |    12 |     3   (0)| 00:00:01 |
|   1 |  NESTED LOOPS ANTI |         |     1 |    12 |     3   (0)| 00:00:01 |
|   2 |   TABLE ACCESS FULL| EMP     |    14 |   126 |     3   (0)| 00:00:01 |
|*  3 |   INDEX UNIQUE SCAN| PK_DEPT |     4 |    12 |     0   (0)| 00:00:01 |
------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   3 - access("DEPT"."DEPTNO"="EMP"."DEPTNO")




提问:执行计划中的连接顺序依然没变。是否是我的hint添加有误,我知道leading对于normal join是生效的,不过对于特殊的anti-join不知道如何能够改变驱动表的顺序,虚心请教!
3#
发表于 2013-1-14 22:13:24
  1. SQL>
  2. SQL> create table emp as select * from scott.emp;

  3. Table created.

  4. SQL> create table dept as select * from scott.dept;

  5. Table created.



  6. SQL> create index pk_dept on dept(deptno);  

  7. Index created.



  8. select  ename
  9.   from emp A
  10. where not exists (select  1 from dept B where A.deptno = B.deptno);



  11. Execution Plan
  12. ----------------------------------------------------------
  13. Plan hash value: 576271144

  14. ------------------------------------------------------------------------------
  15. | Id  | Operation          | Name    | Rows  | Bytes | Cost (%CPU)| Time     |
  16. ------------------------------------------------------------------------------
  17. |   0 | SELECT STATEMENT   |         |     1 |    33 |     2   (0)| 00:00:01 |
  18. |   1 |  NESTED LOOPS ANTI |         |     1 |    33 |     2   (0)| 00:00:01 |
  19. |   2 |   TABLE ACCESS FULL| EMP     |    14 |   280 |     2   (0)| 00:00:01 |
  20. |*  3 |   INDEX RANGE SCAN | PK_DEPT |     4 |    52 |     0   (0)| 00:00:01 |
  21. ------------------------------------------------------------------------------

  22. Predicate Information (identified by operation id):
  23. ---------------------------------------------------

  24.    3 - access("A"."DEPTNO"="B"."DEPTNO")

  25. Note
  26. -----
  27.    - dynamic sampling used for this statement


  28. Statistics
  29. ----------------------------------------------------------
  30.           0  recursive calls
  31.           0  db block gets
  32.           5  consistent gets
  33.           0  physical reads
  34.           0  redo size
  35.         322  bytes sent via SQL*Net to client
  36.         481  bytes received via SQL*Net from client
  37.           1  SQL*Net roundtrips to/from client
  38.           0  sorts (memory)
  39.           0  sorts (disk)
  40.           0  rows processed

  41.                   
  42.                   
  43.                   
  44. SQL> oradebug setmypid
  45. Statement processed.
  46. SQL> oradebug event 10053 trace name context forever,level 1;
  47. Statement processed.
  48. SQL> explain plan for select  ename
  49.   2    from emp A
  50.   3   where not exists (select  1 from dept B where A.deptno = B.deptno);

  51. Explained.


  52. ******* UNPARSED QUERY IS *******
  53. SELECT "A"."ENAME" "ENAME" FROM "SYS"."DEPT" "B","SYS"."EMP" "A" WHERE "A"."DEPTNO"="B"."DEPTNO"
  54. kkoqbc-subheap (delete addr=0x7f99d52f16f8, in-use=23080, alloc=23736)
  55. kkoqbc-end
  56.           : call(in-use=22344, alloc=65552), compile(in-use=57328, alloc=106032)
  57. apadrv-end: call(in-use=22344, alloc=65552), compile(in-use=58312, alloc=106032)

  58. sql_id=frvs2rfn4z4g3.
  59. Current SQL statement for this session:
  60. explain plan for select  ename
  61.   from emp A
  62. where not exists (select  1 from dept B where A.deptno = B.deptno)

  63. ============
  64. Plan Table
  65. ============
  66. --------------------------------------+-----------------------------------+
  67. | Id  | Operation           | Name    | Rows  | Bytes | Cost  | Time      |
  68. --------------------------------------+-----------------------------------+
  69. | 0   | SELECT STATEMENT    |         |       |       |     2 |           |
  70. | 1   |  NESTED LOOPS ANTI  |         |     1 |    33 |     2 |  00:00:01 |
  71. | 2   |   TABLE ACCESS FULL | EMP     |    14 |   280 |     2 |  00:00:01 |
  72. | 3   |   INDEX RANGE SCAN  | PK_DEPT |     4 |    52 |     0 |           |
  73. --------------------------------------+-----------------------------------+

  74. Predicate Information:
  75. ----------------------
  76. 3 - access("A"."DEPTNO"="B"."DEPTNO")


  77.                   
复制代码
对于ANTI JOIN来说 其原理要求driving table为特定表

回复 只看该作者 道具 举报

2#
发表于 2013-1-14 16:57:01
需要更多演示说明该问题, 问题延迟到 晚上回复

回复 只看该作者 道具 举报

您需要登录后才可以回帖 登录 | 注册

QQ|手机版|Archiver|Oracle数据库数据恢复、性能优化

GMT+8, 2024-11-16 07:01 , Processed in 0.045614 second(s), 21 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部
TEL/電話+86 13764045638
Email service@parnassusdata.com
QQ 47079569