sql> set autot on
sql> select rowid, object_id
2 from test
3 where rowid ='aaarvkaaeaaaad8aaa';
rowid object_id
------------------ ----------
aaarvkaaeaaaad8aaa 28
elapsed: 00:00:00.01
execution plan
----------------------------------------------------------
plan hash value: 2153624467
-------------------------------------------
| id | operation | name |
| 0 | select statement | |
| 1 | table access by user rowid| test |
note
-----
- rule based optimizer used (consider using cbo)
statistics
1 recursive calls
0 db block gets
1 consistent gets
0 physical reads
0 sorts (memory)
0 sorts (disk)
1 rows processed
注意到统计信息,只有一个consistent gets,只需要一次读取即可以获得数据。这种方式是访问数据最快的方式。
----当查询条件无法命中任何索引或者扫描索引的代价大于全表扫描代价的某一比率(optimizer_index_cost_adj)的时候,oracle会选择使用全表扫描。此时oracle 会读取一定数量的数据块,直到hwm。
sql> select rowid ,object_id
3 where rownum =1;
elapsed: 00:00:00.00
plan hash value: 2347100821
-----------------------------------
| id | operation | name |
| 0 | select statement | |
|* 1 | count stopkey | |
| 2 | table access full| test |--全表扫描。
predicate information (identified by operation id):
---------------------------------------------------
1 - filter(rownum=1)
4 consistent gets
-------------------------
sql> select rowid ,object_id from test where rowid = 'aaarvkaaeaaaad8aaa';
-----------------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
| 0 | select statement | | 1 | 17 | 1 (0)| 00:00:01 |
| 1 | table access by user rowid| test | 1 | 17 | 1 (0)| 00:00:01 |
----------------------------------------------------------------------------------
147 recursive calls
14 consistent gets
0 redo size
。。。
3 sorts (memory)
-----------------------------------------------
----建立索引
sql> create table un_test tablespace users as select * from dba_objects;
table created.
elapsed: 00:00:02.18
sql> create unique index i_test_uni on un_test (object_id);
index created.
elapsed: 00:00:00.24
---index unique scan 当索引为unique时,每个子叶节点只会指向一条数据.
如果oracle预测到只有0或1条数据时,就会选择index unique scan .
sql> select object_id from un_test where object_id =28;
object_id
----------
28
plan hash value: 333614268
--------------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
| 0 | select statement | | 1 | 5 | 1 (0)| 00:00:01 |
|* 1 | index unique scan| i_test_uni | 1 | 5 | 1 (0)| 00:00:01 |
--------------------------------------------------
1 - access("object_id"=28)
---index range scan
当通过索引查找数据时,oracle 认为会返回数据可能大于1 ,会进行index range scan.
例如进行范围查询,< , > ,like,between val1 and val2 ,in 等操作。
index range scan, index unique scan 会引起db file sequential read 等待
sql> select object_id from un_test where object_id >5 and object_id <9;
6
7
8
plan hash value: 1694266620
-------------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
| 0 | select statement | | 1 | 5 | 2 (0)| 00:00:01 |
|* 1 | index range scan| i_test_uni | 1 | 5 | 2 (0)| 00:00:01 |
1 - access("object_id">5 and "object_id"<9)
--index fast full scan
索引快速扫描和全表扫描类似,一次读取db_file_multiblock_count 个数据块。index fast full scan
与其他索引扫描不同,它不会从根节点开始,也不读取节点,而是直接扫描所有子叶节点;也不会一次读取一个数据块。
sql> select object_id from un_test where object_id >3;
53362 rows selected.
elapsed: 00:00:00.25
plan hash value: 972078537
| id | operation | name | rows | bytes | cost (%cpu)| time |
| 0 | select statement | | 53364 | 260k| 27 (4)| 00:00:01 |
|* 1 | index fast full scan| i_test_uni | 53364 | 260k| 27 (4)| 00:00:01 |
1 - filter("object_id">3)
3670 consistent gets
。。。。。。。
53362 rows processed
sql> select object_id from un_test where object_id>500;
52887 rows selected.
| 0 | select statement | | 52998 | 258k| 27 (4)| 00:00:01 |
|* 1 | index fast full scan| i_test_uni | 52998 | 258k| 27 (4)| 00:00:01 |
1 - filter("object_id">500
3634 consistent gets
。。。。。。。。。
52887 rows processed
有时如果 的值过小,强制使用索引扫描时,会发生index full scan .index full scan 和 index fast full scan 不同。它是一种索引扫描,从根节点开始扫描,遍历整棵索引树,并且一次读取一个索引块,ifs 会引起 db file sequential read 事件。