查询转换——子查询解嵌套(2)!

本文深入探讨了SQL查询中嵌套查询的解嵌套过程与视图合并的区别,通过实例展示了如何将子查询转化为表连接以优化查询效率。同时,文章还介绍了如何使用特定提示(如NO_UNNEST)来控制查询行为,从而在不同场景下实现更高效的查询执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

子查询解嵌套与视图合并的相似之处在于子查询也是通过一个单独的查询块来表示的。可合并的视图与可以解嵌套的子查询之间的主要区别在于它们的位置是不同的:子查询位于WHERE子句。

/*+ NO_UNNEST */    禁止子查询解嵌套转换。

  1. SQL> select /*+ gather_plan_statistics */ * from emp where deptno in(select deptno from dept where dname = 'RESEARCH');  
  2.   
  3.      EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM     DEPTNO  
  4. ---------- ---------- --------- ---------- -------------- ---------- ---------- ----------  
  5.       7566 JONES      MANAGER         7839 02-4月 -81           2975                    20  
  6.       7902 FORD       ANALYST         7566 03-12月-81           3000                    20  
  7.       7876 ADAMS      CLERK           7788 23-5月 -87           1100                    20  
  8.       7369 SMITH      CLERK           7902 17-12月-80            800                    20  
  9.       7788 SCOTT      ANALYST         7566 19-4月 -87           3000                    20  
  10.   
  11. SQL> select * from table(dbms_xplan.display_cursor(null,null,'ALLSTATS LAST'));  
  12.   
  13. PLAN_TABLE_OUTPUT  
  14. ------------------------------------------------------------------------------------------------------------------------------------------------------  
  15. SQL_ID  c9r1ywanhnt7q, child number 0  
  16. -------------------------------------  
  17. select /*+ gather_plan_statistics */ * from emp where deptno in(select  
  18. deptno from dept where dname = 'RESEARCH')  
  19.   
  20. Plan hash value: 844388907  
  21.   
  22. -----------------------------------------------------------------------------------------------------------------------------  
  23. | Id  | Operation                    | Name    | Starts | E-Rows | A-Rows |   A-Time   | Buffers |  OMem |  1Mem | Used-Mem |  
  24. -----------------------------------------------------------------------------------------------------------------------------  
  25. |   0 | SELECT STATEMENT             |         |      1 |        |      5 |00:00:00.01 |      11 |       |       |          |  
  26. |   1 |  MERGE JOIN                  |         |      1 |      5 |      5 |00:00:00.01 |      11 |       |       |          |  
  27. |*  2 |   TABLE ACCESS BY INDEX ROWID| DEPT    |      1 |      1 |      1 |00:00:00.01 |       4 |       |       |          |  
  28. |   3 |    INDEX FULL SCAN           | PK_DEPT |      1 |      4 |      4 |00:00:00.01 |       2 |       |       |          |  
  29. |*  4 |   SORT JOIN                  |         |      1 |     14 |      5 |00:00:00.01 |       7 |  2048 |  2048 | 2048  (0)|  
  30. |   5 |    TABLE ACCESS FULL         | EMP     |      1 |     14 |     14 |00:00:00.01 |       7 |       |       |          |  
  31. -----------------------------------------------------------------------------------------------------------------------------  
  32.   
  33. Predicate Information (identified by operation id):  
  34. ---------------------------------------------------  
  35.   
  36.    2 - filter("DNAME"='RESEARCH')  
  37.   
  38. PLAN_TABLE_OUTPUT  
  39. ------------------------------------------------------------------------------------------------------------------------------------------------------  
  40.    4 - access("DEPTNO"="DEPTNO")  
  41.        filter("DEPTNO"="DEPTNO")  
  42.   
  43.   
  44. 已选择25行。  

这里的子查询就简单的通过转化为表联结来合并到主查询中。该查询的计划就好像是按如下语句得出来的:

  1. SQL> select /*+ gather_plan_statistics */ e.* from emp e,dept d where e.deptno = d.deptno and dname = 'RESEARCH';  
  2.   
  3.      EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM     DEPTNO  
  4. ---------- ---------- --------- ---------- -------------- ---------- ---------- ----------  
  5.       7566 JONES      MANAGER         7839 02-4月 -81           2975                    20  
  6.       7902 FORD       ANALYST         7566 03-12月-81           3000                    20  
  7.       7876 ADAMS      CLERK           7788 23-5月 -87           1100                    20  
  8.       7369 SMITH      CLERK           7902 17-12月-80            800                    20  
  9.       7788 SCOTT      ANALYST         7566 19-4月 -87           3000                    20  
  10.   
  11. SQL> select * from table(dbms_xplan.display_cursor(null,null,'ALLSTATS LAST'));  
  12.   
  13. PLAN_TABLE_OUTPUT  
  14. ------------------------------------------------------------------------------------------------------------------------------------------------------  
  15. SQL_ID  26zzdpdm0q17j, child number 0  
  16. -------------------------------------  
  17. select /*+ gather_plan_statistics */ e.* from emp e,dept d where  
  18. e.deptno = d.deptno and dname = 'RESEARCH'  
  19.   
  20. Plan hash value: 844388907  
  21.   
  22. -----------------------------------------------------------------------------------------------------------------------------  
  23. | Id  | Operation                    | Name    | Starts | E-Rows | A-Rows |   A-Time   | Buffers |  OMem |  1Mem | Used-Mem |  
  24. -----------------------------------------------------------------------------------------------------------------------------  
  25. |   0 | SELECT STATEMENT             |         |      1 |        |      5 |00:00:00.01 |      11 |       |       |          |  
  26. |   1 |  MERGE JOIN                  |         |      1 |      5 |      5 |00:00:00.01 |      11 |       |       |          |  
  27. |*  2 |   TABLE ACCESS BY INDEX ROWID| DEPT    |      1 |      1 |      1 |00:00:00.01 |       4 |       |       |          |  
  28. |   3 |    INDEX FULL SCAN           | PK_DEPT |      1 |      4 |      4 |00:00:00.01 |       2 |       |       |          |  
  29. |*  4 |   SORT JOIN                  |         |      1 |     14 |      5 |00:00:00.01 |       7 |  2048 |  2048 | 2048  (0)|  
  30. |   5 |    TABLE ACCESS FULL         | EMP     |      1 |     14 |     14 |00:00:00.01 |       7 |       |       |          |  
  31. -----------------------------------------------------------------------------------------------------------------------------  
  32.   
  33. Predicate Information (identified by operation id):  
  34. ---------------------------------------------------  
  35.   
  36.    2 - filter("DNAME"='RESEARCH')  
  37.   
  38. PLAN_TABLE_OUTPUT  
  39. ------------------------------------------------------------------------------------------------------------------------------------------------------  
  40.    4 - access("E"."DEPTNO"="D"."DEPTNO")  
  41.        filter("E"."DEPTNO"="D"."DEPTNO")  
  42.   
  43.   
  44. 已选择25行。  

使用NO_UNNEST提示

  1. SQL> select /*+ gather_plan_statistics */ * from emp where deptno in(select /*+ no_unnest */ deptno from dept where dname = 'RESEARCH');  
  2.   
  3.      EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM     DEPTNO  
  4. ---------- ---------- --------- ---------- -------------- ---------- ---------- ----------  
  5.       7369 SMITH      CLERK           7902 17-12月-80            800                    20  
  6.       7566 JONES      MANAGER         7839 02-4月 -81           2975                    20  
  7.       7788 SCOTT      ANALYST         7566 19-4月 -87           3000                    20  
  8.       7876 ADAMS      CLERK           7788 23-5月 -87           1100                    20  
  9.       7902 FORD       ANALYST         7566 03-12月-81           3000                    20  
  10.   
  11. SQL> select * from table(dbms_xplan.display_cursor(null,null,'ALLSTATS LAST'));  
  12.   
  13. PLAN_TABLE_OUTPUT  
  14. ------------------------------------------------------------------------------------------------------------------------------------------------------  
  15. SQL_ID  fxark931f2dh8, child number 0  
  16. -------------------------------------  
  17. select /*+ gather_plan_statistics */ * from emp where deptno in(select  
  18. /*+ no_unnest */ deptno from dept where dname = 'RESEARCH')  
  19.   
  20. Plan hash value: 2809975276  
  21.   
  22. --------------------------------------------------------------------------------------------------  
  23. | Id  | Operation                    | Name    | Starts | E-Rows | A-Rows |   A-Time   | Buffers |  
  24. --------------------------------------------------------------------------------------------------  
  25. |   0 | SELECT STATEMENT             |         |      1 |        |      5 |00:00:00.01 |      14 |  
  26. |*  1 |  FILTER                      |         |      1 |        |      5 |00:00:00.01 |      14 |  
  27. |   2 |   TABLE ACCESS FULL          | EMP     |      1 |     14 |     14 |00:00:00.01 |       8 |  
  28. |*  3 |   TABLE ACCESS BY INDEX ROWID| DEPT    |      3 |      1 |      1 |00:00:00.01 |       6 |  
  29. |*  4 |    INDEX UNIQUE SCAN         | PK_DEPT |      3 |      1 |      3 |00:00:00.01 |       3 |  
  30. --------------------------------------------------------------------------------------------------  
  31.   
  32. Predicate Information (identified by operation id):  
  33. ---------------------------------------------------  
  34.   
  35.    1 - filter( IS NOT NULL)  
  36.    3 - filter("DNAME"='RESEARCH')  
  37.   
  38. PLAN_TABLE_OUTPUT  
  39. ------------------------------------------------------------------------------------------------------------------------------------------------------  
  40.    4 - access("DEPTNO"=:B1)  
  41.   
  42.   
  43. 已选择24行。  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谭祖爱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值