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

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

0

积分

0

好友

5

主题
1#
发表于 2013-7-19 15:25:20 | 查看: 6006| 回复: 4
本帖最后由 wendy 于 2013-7-19 15:27 编辑

版本:oracle 10.2.0.4(64bit).

SQL> set autotrace on;
SQL> select min(income_time) from AGG_QUERY_SESSION_DETAIL;

MIN(INCOME_TIME)
-------------------
2011-01-01 00:00:00


Execution Plan
----------------------------------------------------------

-----------------------------------------------------------------------------------------
| Id  | Operation                  | Name                  | Rows  | Bytes | Cost (%CPU)|
-----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT           |                       |     1 |     8 |   743K  (2)|
|   1 |  SORT AGGREGATE            |                       |     1 |     8 |            |
|   2 |   INDEX FULL SCAN (MIN/MAX)| IDX_QSD_INCOME_TIME11 |   272M|  2081M|            |
-----------------------------------------------------------------------------------------

Note
-----
   - 'PLAN_TABLE' is old version


Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
          4  consistent gets
          4  physical reads
          0  redo size
        528  bytes sent via SQL*Net to client
        492  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

SQL> select max(income_time) from AGG_QUERY_SESSION_DETAIL;

MAX(INCOME_TIME)
-------------------
2013-07-18 23:59:40


Execution Plan
----------------------------------------------------------

-----------------------------------------------------------------------------------------
| Id  | Operation                  | Name                  | Rows  | Bytes | Cost (%CPU)|
-----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT           |                       |     1 |     8 |   743K  (2)|
|   1 |  SORT AGGREGATE            |                       |     1 |     8 |            |
|   2 |   INDEX FULL SCAN (MIN/MAX)| IDX_QSD_INCOME_TIME11 |   272M|  2081M|            |
-----------------------------------------------------------------------------------------

Note
-----
   - 'PLAN_TABLE' is old version


Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
          4  consistent gets
          3  physical reads
          0  redo size
        528  bytes sent via SQL*Net to client
        492  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

SQL> select max(income_time),min(income_time) from AGG_QUERY_SESSION_DETAIL;

MAX(INCOME_TIME)    MIN(INCOME_TIME)
------------------- -------------------
2013-07-18 23:59:40 2011-01-01 00:00:00


Execution Plan
----------------------------------------------------------

------------------------------------------------------------------------------------
| Id  | Operation          | Name                     | Rows  | Bytes | Cost (%CPU)|
------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |                          |     1 |     8 |   743K  (2)|
|   1 |  SORT AGGREGATE    |                          |     1 |     8 |            |
|   2 |   TABLE ACCESS FULL| AGG_QUERY_SESSION_DETAIL |   272M|  2081M|   743K  (2)|
------------------------------------------------------------------------------------

Note
-----
   - 'PLAN_TABLE' is old version


Statistics
----------------------------------------------------------
          2  recursive calls
          3  db block gets
    4273288  consistent gets
    4273185  physical reads
        132  redo size
        610  bytes sent via SQL*Net to client
        492  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

select min(income_time) from table; 走索引;
select max(income_time) from table; 走索引。
select max(income_time),min(income_time) from table;全表扫描。为什么会这样呢?请赐教!
2#
发表于 2013-7-19 15:27:14
环境:oracle 10.2.0.4(64bit).

回复 只看该作者 道具 举报

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:


正如下面:
  1. SQL> alter table t1 modify object_id not null;

  2. Table altered.

  3. SQL> select max(object_id),min(object_id) from t1;

  4. MAX(OBJECT_ID) MIN(OBJECT_ID)
  5. -------------- --------------
  6.          76200                    2


  7. Execution Plan
  8. ----------------------------------------------------------
  9. Plan hash value: 1447436376

  10. --------------------------------------------------------------------------------
  11. | Id  | Operation              | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
  12. --------------------------------------------------------------------------------
  13. |   0 | SELECT STATEMENT      |        |     1 |    13 |    50         (0)| 00:00:01 |
  14. |   1 |  SORT AGGREGATE       |        |     1 |    13 |            |               |
  15. |   2 |   INDEX FAST FULL SCAN| T1_IDX | 97916 |  1243K|    50         (0)| 00:00:01 |
  16. --------------------------------------------------------------------------------

  17. Note
  18. -----
  19.    - dynamic sampling used for this statement (level=2)


  20. Statistics
  21. ----------------------------------------------------------
  22.          36  recursive calls
  23.           0  db block gets
  24.         290  consistent gets
  25.         165  physical reads
  26.           0  redo size
  27.         500  bytes sent via SQL*Net to client
  28.         419  bytes received via SQL*Net from client
  29.           2  SQL*Net roundtrips to/from client
  30.           6  sorts (memory)
  31.           0  sorts (disk)
  32.           1  rows processed
复制代码
Good luck.

回复 只看该作者 道具 举报

4#
发表于 2013-7-19 22:08:06
谢谢,非常感谢,补了一课。

回复 只看该作者 道具 举报

5#
发表于 2013-7-23 02:41:07
比较有意思。

回复 只看该作者 道具 举报

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

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

GMT+8, 2024-6-1 19:56 , Processed in 0.053095 second(s), 20 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

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