如何选择全扫描操作

先创建数据:

C:\>sqlplus "/as sysdba"

SQL*Plus: Release 10.2.0.1.0 - Production on 星期日 3月 30 20:32:22 2014

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


连接到:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production With the Partitioning, OLAP and Data Mining options

SQL> create table t1 as select trunc((rownum-1)/100) id, rpad(rownum, 100) t_pad from dba_source where rownum <= 10000;

表已创建。

SQL> create index t1_idx1 on t1(id);

索引已创建。

SQL> create table t2 as select mod(rownum, 100) id, rpad(rownum, 100) t_pad from dba_source where rownum<=10000;

表已创建。

SQL> create index t2_idx1 on t2(id);

索引已创建。

SQL> exec dbms_stats.gather_table_stats(user,'t2', method_opt=>'FOR ALL COLUMNS SIZE 1', cascade=>TRUE);

PL/SQL 过程已成功完成。

SQL>




查看t1的执行计划:


SQL> set autotrace traceonly
SQL> alter system flush buffer_cache;

系统已更改。

SQL> alter system flush shared_pool;

系统已更改。

SQL> select * from t1 where id=1;

已选择100行。


执行计划
----------------------------------------------------------
Plan hash value: 2623418078

--------------------------------------------------------------------------------

-------

| Id  | Operation                   | Name    | Rows  | Bytes | Cost (%CPU)| Tim

e     |

--------------------------------------------------------------------------------

-------

|   0 | SELECT STATEMENT            |         |   100 | 11500 |     3   (0)| 00:

00:01 |

|   1 |  TABLE ACCESS BY INDEX ROWID| T1      |   100 | 11500 |     3   (0)| 00:

00:01 |

|*  2 |   INDEX RANGE SCAN          | T1_IDX1 |   100 |       |     1   (0)| 00:

00:01 |

--------------------------------------------------------------------------------

-------


Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("ID"=1)

Note
-----
   - dynamic sampling used for this statement


统计信息
----------------------------------------------------------
        307  recursive calls
          0  db block gets
        123  consistent gets
        114  physical reads
          0  redo size
      12167  bytes sent via SQL*Net to client
        451  bytes received via SQL*Net from client
          8  SQL*Net roundtrips to/from client
          3  sorts (memory)
          0  sorts (disk)
        100  rows processed




查看t2的执行计划:


SQL> alter system flush buffer_cache;

系统已更改。

SQL> alter system flush shared_pool;

系统已更改。

SQL> select * from t2 where id=1;

已选择100行。


执行计划
----------------------------------------------------------
Plan hash value: 1513984157

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |   100 | 10300 |    36   (3)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| T2   |   100 | 10300 |    36   (3)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("ID"=1)


统计信息
----------------------------------------------------------
        203  recursive calls
          0  db block gets
        192  consistent gets
        158  physical reads
          0  redo size
      11845  bytes sent via SQL*Net to client
        451  bytes received via SQL*Net from client
          8  SQL*Net roundtrips to/from client
          3  sorts (memory)
          0  sorts (disk)
        100  rows processed

SQL>


oracle 全扫描_执行计划

oracle 全扫描_sed_02

oracle 全扫描_执行计划_03





全扫描与舍弃


oracle 全扫描_SQL_04


SQL> select table_name, num_rows, blocks from user_tables where table_name = 'T2';

TABLE_NAME                       NUM_ROWS     BLOCKS
------------------------------ ---------- ----------
T2                                  10000        152

SQL>


这里是152个数据块。

oracle 全扫描_sed_05