SELECT MIN(ID),MAX(ID) FROM TABLE 优化问题

一个问题 SELECT MIN(ID),MAX(ID) FROM TABLE 如何优化

好的现在来做个实验:

[html]  view plain copy print ?
  1. SQL>  select * from v$version where rownum<2;  
  2.   
  3. BANNER  
  4. --------------------------------------------------------------------------------  
  5. Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production  
  6.   
  7. SQL>  create table test as select * from dba_objects;  
  8.   
  9. Table created.  
  10.   
  11. SQL>  alter table test modify object_id not null;  
  12.   
  13. Table altered.  
  14. <pre class="html" name="code"> </pre><pre class="html" name="code">SQL> create index i_object_id on test(object_id);  
  15.   
  16. Index created.</pre><pre class="html" name="code"> </pre><pre class="html" name="code">SQL> select max(object_id),min(object_id) from test;  
  17.   
  18. MAX(OBJECT_ID) MIN(OBJECT_ID)  
  19. -------------- --------------  
  20.          74644              2  
  21.   
  22. Elapsed: 00:00:00.34  
  23.   
  24. Execution Plan  
  25. ----------------------------------------------------------  
  26. Plan hash value: 1751978921  
  27.   
  28. -------------------------------------------------------------------------------------  
  29. | Id  | Operation             | Name        | Rows  | Bytes | Cost (%CPU)| Time     |  
  30. -------------------------------------------------------------------------------------  
  31. |   0 | SELECT STATEMENT      |             |     1 |     5 |    37   (0)| 00:00:01 |  
  32. |   1 |  SORT AGGREGATE       |             |     1 |     5 |            |          |  
  33. |   2 |   INDEX FAST FULL SCAN| I_OBJECT_ID | 72860 |   355K|    37   (0)| 00:00:01 |  
  34. -------------------------------------------------------------------------------------  
  35.   
  36.   
  37. Statistics  
  38. ----------------------------------------------------------  
  39.           1  recursive calls  
  40.           0  db block gets  
  41.         167  consistent gets  
  42.         145  physical reads  
  43.           0  redo size  
  44.         501  bytes sent via SQL*Net to client  
  45.         420  bytes received via SQL*Net from client  
  46.           2  SQL*Net roundtrips to/from client  
  47.           0  sorts (memory)  
  48.           0  sorts (disk)  
  49.           1  rows processed  
  50. </pre><pre class="html" name="code">可以看到CBO选择了 INDEX FAST FULL SCAN,有145个物理读,167个逻辑读,现在改写SQL如下</pre><pre class="html" name="code"> </pre><pre class="html" name="code"><pre class="html" name="code">SQL> select (select min(object_id) min_id from test a),(select max(object_id) max_id from test b) from dual;  
  51.   
  52. (SELECTMIN(OBJECT_ID)MIN_IDFROMTESTA) (SELECTMAX(OBJECT_ID)MAX_IDFROMTESTB)  
  53. ------------------------------------- -------------------------------------  
  54.                                     2                                 74644  
  55.                                       
  56. Execution Plan  
  57. ----------------------------------------------------------  
  58. Plan hash value: 4224666897  
  59.   
  60. ------------------------------------------------------------------------------------------  
  61. | Id  | Operation                  | Name        | Rows  | Bytes | Cost (%CPU)| Time     |  
  62. ------------------------------------------------------------------------------------------  
  63. |   0 | SELECT STATEMENT           |             |     1 |       |     2   (0)| 00:00:01 |  
  64. |   1 |  SORT AGGREGATE            |             |     1 |     5 |            |          |  
  65. |   2 |   INDEX FULL SCAN (MIN/MAX)| I_OBJECT_ID |     1 |     5 |     2   (0)| 00:00:01 |  
  66. |   3 |  SORT AGGREGATE            |             |     1 |     5 |            |          |  
  67. |   4 |   INDEX FULL SCAN (MIN/MAX)| I_OBJECT_ID |     1 |     5 |     2   (0)| 00:00:01 |  
  68. |   5 |  FAST DUAL                 |             |     1 |       |     2   (0)| 00:00:01 |  
  69. ------------------------------------------------------------------------------------------  
  70.   
  71.   
  72. Statistics  
  73. ----------------------------------------------------------  
  74.           1  recursive calls  
  75.           0  db block gets  
  76.           4  consistent gets  
  77.           0  physical reads  
  78.           0  redo size  
  79.         547  bytes sent via SQL*Net to client  
  80.         420  bytes received via SQL*Net from client  
  81.           2  SQL*Net roundtrips to/from client  
  82.           0  sorts (memory)  
  83.           0  sorts (disk)  
  84.           1  rows processed</pre><pre class="html" name="code"> </pre><pre class="html" name="code">可以看到逻辑读降低为4,大家看看这个是不是最优化的方法?还有更进一步的方法优化吗?支付宝说这个案例很经典,可能是我水平太低了,只能想出这个方法  
  85. <pre class="html" name="code"> </pre><pre class="html" name="code">----update,我能想到的另外一种方法,不过逻辑读也是4---------------------</pre><pre class="html" name="code"> </pre><pre class="html" name="code">SQL> select (select /*+ index_asc(test i_object_id) */ object_id from test where rownum=1) min  
  86. ,(select /*+ index_desc(test i_object_id) */ object_id from test where rownum=1) max from dual;  2  
  87.   
  88.        MIN        MAX  
  89. ---------- ----------  
  90.          2      74644  
  91.   
  92.   
  93. Execution Plan  
  94. ----------------------------------------------------------  
  95. Plan hash value: 91314419  
  96.   
  97. -------------------------------------------------------------------------------------------  
  98. | Id  | Operation                   | Name        | Rows  | Bytes | Cost (%CPU)| Time     |  
  99. -------------------------------------------------------------------------------------------  
  100. |   0 | SELECT STATEMENT            |             |     1 |       |     2   (0)| 00:00:01 |  
  101. |*  1 |  COUNT STOPKEY              |             |       |       |            |          |  
  102. |   2 |   INDEX FULL SCAN           | I_OBJECT_ID |     1 |     5 |     2   (0)| 00:00:01 |  
  103. |*  3 |  COUNT STOPKEY              |             |       |       |            |          |  
  104. |   4 |   INDEX FULL SCAN DESCENDING| I_OBJECT_ID |     1 |     5 |     2   (0)| 00:00:01 |  
  105. |   5 |  FAST DUAL                  |             |     1 |       |     2   (0)| 00:00:01 |  
  106. -------------------------------------------------------------------------------------------  
  107.   
  108. Predicate Information (identified by operation id):  
  109. ---------------------------------------------------  
  110.   
  111.    1 - filter(ROWNUM=1)  
  112.    3 - filter(ROWNUM=1)  
  113.   
  114.   
  115. Statistics  
  116. ----------------------------------------------------------  
  117.           1  recursive calls  
  118.           0  db block gets  
  119.           4  consistent gets  
  120.           0  physical reads  
  121.           0  redo size  
  122.         479  bytes sent via SQL*Net to client  
  123.         420  bytes received via SQL*Net from client  
  124.           2  SQL*Net roundtrips to/from client  
  125.           0  sorts (memory)  
  126.           0  sorts (disk)  
  127.           1  rows processed  
  128. </pre><br></pre></pre>  
转载:http://blog.csdn.net/robinson1988/article/details/6623773
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值