- 最后登录
- 2013-11-14
- 在线时间
- 104 小时
- 威望
- 999
- 金钱
- 271
- 注册时间
- 2013-1-9
- 阅读权限
- 70
- 帖子
- 63
- 精华
- 0
- 积分
- 999
- UID
- 855
|
3#
发表于 2013-7-19 19:53:35
问题很有意思啊 :)
可以参考这篇文章:"http://richardfoote.wordpress.com/category/index-full-scan-minmax/"
主要原因摘录如下:
If you’re after either the minimum or the maximum of a column value and the column is indexed, the CBO can potentially use the Index Full Scan (Min/Max), which simply navigates to the first OR last leaf block in the index structure looking for the Min or Max value in question. Oracle can of course navigate to the first (left-most) or last (right-most) leaf blocks very easily by simply following the associated first/last pointers in the Root/Branch structure of the index. All things being equal and providing there haven’t been any subsequent deletes to empty out the index entries from these leaf blocks, Oracle can very quickly determine the minimum or maximum value of the column.
However, the Index Full Scan (Min/Max) can only visit one side of the index, not both. Therefore, if you want both the minimum and the maximum column value, an Index Full Scan (Min/Max) is not viable and the CBO is forced to look for other alternatives. It sounds like such a trivial thing to implement but that’s how it goes. I do remember way back when Oracle9i was released and the introduction of the Index Skip Scan I thought perhaps Oracle might also soon introduce an index skip scan version of Min/Max (as it basically just needs to “skip” all the index leaf blocks in the “middle” of the index via another lookup of the index), but it was not to be.
特别注意如下描述:
Then an (expensive) Full Table Scan is likely the way to go. However, if the column has a NOT NULL constraint and the index is indeed smaller than the parent table, then:
正如下面:- SQL> alter table t1 modify object_id not null;
- Table altered.
- SQL> select max(object_id),min(object_id) from t1;
- MAX(OBJECT_ID) MIN(OBJECT_ID)
- -------------- --------------
- 76200 2
- Execution Plan
- ----------------------------------------------------------
- Plan hash value: 1447436376
- --------------------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- --------------------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 1 | 13 | 50 (0)| 00:00:01 |
- | 1 | SORT AGGREGATE | | 1 | 13 | | |
- | 2 | INDEX FAST FULL SCAN| T1_IDX | 97916 | 1243K| 50 (0)| 00:00:01 |
- --------------------------------------------------------------------------------
- Note
- -----
- - dynamic sampling used for this statement (level=2)
- Statistics
- ----------------------------------------------------------
- 36 recursive calls
- 0 db block gets
- 290 consistent gets
- 165 physical reads
- 0 redo size
- 500 bytes sent via SQL*Net to client
- 419 bytes received via SQL*Net from client
- 2 SQL*Net roundtrips to/from client
- 6 sorts (memory)
- 0 sorts (disk)
- 1 rows processed
复制代码 Good luck. |
|