当前位置:Gxlcms > 数据库问题 > oracle中connect by语句的优化

oracle中connect by语句的优化

时间:2021-07-01 10:21:17 帮助过:13人阅读

A.CI, A.ENBAJ02 AS CELL_NAME FROM TDL_CM_CELL A, T_ORG_CELL_SCOPE S WHERE S.REGION_NAME = A.REGION_NAME AND S.CITY_NAME = A.CITY_NAME AND (S.ORG_ID) IN (SELECT ID FROM T_ORG O START WITH ID = 101021003 --1010210 --START WITH ID=1 CONNECT BY PARENT_ID = PRIOR ID)

实际使用的执行计划:
技术分享

而不会采用自适应计划(adaptive plan):

  1. Plan Hash Value : 2596385940
  2. -------------------------------------------------------------------------------------------------------------------
  3. | Id | Operation | Name | Rows | Bytes | Cost | Time |
  4. -------------------------------------------------------------------------------------------------------------------
  5. | 0 | SELECT STATEMENT | | 2622 | 228114 | 227 | 00:00:01 |
  6. | 1 | NESTED LOOPS | | 2622 | 228114 | 227 | 00:00:01 |
  7. | 2 | NESTED LOOPS | | 2622 | 228114 | 227 | 00:00:01 |
  8. | * 3 | HASH JOIN | | 1 | 31 | 7 | 00:00:01 |
  9. | 4 | VIEW | VW_NSO_1 | 1 | 13 | 4 | 00:00:01 |
  10. | 5 | HASH UNIQUE | | 1 | 20 | 4 | 00:00:01 |
  11. | * 6 | CONNECT BY NO FILTERING WITH SW (UNIQUE) | | | | | |
  12. | 7 | TABLE ACCESS FULL | T_ORG | 75 | 825 | 3 | 00:00:01 |
  13. | 8 | TABLE ACCESS FULL | T_ORG_CELL_SCOPE | 85 | 1530 | 3 | 00:00:01 |
  14. | * 9 | INDEX RANGE SCAN | IDX_TDL_CM_CELL_SCOPE | 257 | | 8 | 00:00:01 |
  15. | 10 | TABLE ACCESS BY INDEX ROWID | TDL_CM_CELL | 2313 | 129528 | 220 | 00:00:01 |
  16. -------------------------------------------------------------------------------------------------------------------
  17. Predicate Information (identified by operation id):
  18. ------------------------------------------
  19. * 3 - access("S"."ORG_ID"="ID")
  20. * 6 - access("PARENT_ID"=PRIOR "ID")
  21. * 6 - filter("ID"=101021003)
  22. * 9 - access("S"."REGION_NAME"="A"."REGION_NAME" AND "S"."CITY_NAME"="A"."CITY_NAME")
  23. Notes
  24. -----
  25. - This is an adaptive plan

原因在于,oracle无法知道connect by之后的数量,所以只能认为是很大的量

--

有一种方式就是,就是使用提示来解决:

  1. <span style="color: #0000ff">SELECT</span> <span style="color: #008080">/*</span><span style="color: #008080">+ no_merge(x) use_nl(a x) </span><span style="color: #008080">*/</span><span style="color: #000000">
  2. A.CI, A.ENBAJ02 </span><span style="color: #0000ff">AS</span><span style="color: #000000"> CELL_NAME
  3. </span><span style="color: #0000ff">FROM</span><span style="color: #000000"> TDL_CM_CELL A,
  4. (</span><span style="color: #0000ff">select</span><span style="color: #000000"> s.city_name, s.region_name
  5. </span><span style="color: #0000ff">from</span><span style="color: #000000"> T_ORG_CELL_SCOPE S
  6. </span><span style="color: #0000ff">WHERE</span> (S.ORG_ID) <span style="color: #808080">IN</span><span style="color: #000000">
  7. (</span><span style="color: #0000ff">SELECT</span><span style="color: #000000"> ID
  8. </span><span style="color: #0000ff">FROM</span><span style="color: #000000"> T_ORG O
  9. START </span><span style="color: #0000ff">WITH</span> ID <span style="color: #808080">=</span> <span style="color: #800000; font-weight: bold">101021003</span> <span style="color: #008080">--</span><span style="color: #008080">1010210 </span>
  10. <span style="color: #008080">--</span><span style="color: #008080">START WITH ID=1</span>
  11. CONNECT <span style="color: #0000ff">BY</span> PARENT_ID <span style="color: #808080">=</span><span style="color: #000000"> PRIOR ID)
  12. ) x
  13. </span><span style="color: #0000ff">where</span> x.REGION_NAME <span style="color: #808080">=</span><span style="color: #000000"> A.REGION_NAME
  14. </span><span style="color: #808080">AND</span> x.CITY_NAME <span style="color: #808080">=</span> A.CITY_NAME

这样计划就是:

  1. Plan Hash Value : 37846894
  2. ---------------------------------------------------------------------------------------------------------------------
  3. | Id | Operation | Name | Rows | Bytes | Cost | Time |
  4. ---------------------------------------------------------------------------------------------------------------------
  5. | 0 | SELECT STATEMENT | | 2313 | 277560 | 227 | 00:00:01 |
  6. | 1 | NESTED LOOPS | | 2313 | 277560 | 227 | 00:00:01 |
  7. | 2 | NESTED LOOPS | | 2313 | 277560 | 227 | 00:00:01 |
  8. | 3 | VIEW | | 1 | 64 | 7 | 00:00:01 |
  9. | * 4 | HASH JOIN | | 1 | 31 | 7 | 00:00:01 |
  10. | 5 | VIEW | VW_NSO_1 | 1 | 13 | 4 | 00:00:01 |
  11. | 6 | HASH UNIQUE | | 1 | 20 | 4 | 00:00:01 |
  12. | * 7 | CONNECT BY NO FILTERING WITH SW (UNIQUE) | | | | | |
  13. | 8 | TABLE ACCESS FULL | T_ORG | 75 | 825 | 3 | 00:00:01 |
  14. | 9 | TABLE ACCESS FULL | T_ORG_CELL_SCOPE | 85 | 1530 | 3 | 00:00:01 |
  15. | * 10 | INDEX RANGE SCAN | IDX_TDL_CM_CELL_SCOPE | 257 | | 8 | 00:00:01 |
  16. | 11 | TABLE ACCESS BY INDEX ROWID | TDL_CM_CELL | 2313 | 129528 | 220 | 00:00:01 |
  17. ---------------------------------------------------------------------------------------------------------------------
  18. Predicate Information (identified by operation id):
  19. ------------------------------------------
  20. * 4 - access("S"."ORG_ID"="ID")
  21. * 7 - access("PARENT_ID"=PRIOR "ID")
  22. * 7 - filter("ID"=101021003)
  23. * 10 - access("X"."REGION_NAME"="A"."REGION_NAME" AND "X"."CITY_NAME"="A"."CITY_NAME")

 

如果一个应用的start id可能是一个很大的范围,如果强制使用提示,也会出现问题,所以如果有这样的应用,可以考虑使用oracle 12c的adaptive特性。

如果不行,就必须把不同范围的查询,定义为不同的功能提交给用户。

 

oracle中connect by语句的优化

标签:format   原因   nba   应用   目标   font   log   com   time   

人气教程排行