测试目的
1,未创建索引前的表的扫描情况
1,扫描哪些数据块
2,数据块之间的关系
3,物理读
4,逻辑读
5,以上测试区分:全表扫描与部分表记录扫描
6,扫描数据块是采用单块读取还是多块读取还是先单块读后多块读取?
2,小结:
1,表扫描速度与数据块大小的关系
2,表扫描与并行度设置的关系
3,表扫描与db cache的关系
前文测试了全表扫描的数据块读取情况;如果对表建立了索引,先读取索引,然后根据ROWID再读取对应表记录的数据块
SQL> create table t_detail(a int);
Table created.
--插入10000条记录
SQL> insert into t_detail select level from dual connect by level commit;
Commit complete.
SQL> create index idx_t_detail on t_detail(a);
Index created.
--跟踪已建索引的查询
SQL> alter system set events '10046 trace name context level 8';
System altered.
--因表数据量10000条,10046 trace对查询速度有一定影响
SQL> select count(a) from t_detail where a=2000;
COUNT(A)
----------
1
--关闭10046 trace
SQL> alter system set events '10046 trace name context off';
System altered.
--仅摘录10046 trace重要内容
WAIT #2: nam='Disk file operations I/O' ela= 886 FileOperation=2 fileno=10 filetype=2 obj#=69559 tim=31824399508 --先是一个等待事件
WAIT #2: nam='db file sequential read' ela= 20687 file#=10 block#=276483 blocks=1 obj#=69559 tim=31824420353 -单块读 file#=10 block#=276483 blocks=1 obj#=69559
WAIT #2: nam='db file sequential read' ela= 823 file#=10 block#=276488 blocks=1 obj#=69559 tim=31824421542 --继续单块读 file#=10 block#=276488 blocks=1 obj#=69559
FETCH #2:c=0,e=23170,p=2,cr=2,cu=0,mis=0,r=1,dep=0,og=1,plh=1976055679,tim=31824421699 --然后提取数据了
STAT #2 id=1 cnt=1 pid=0 pos=1 bj=0 p='SORT AGGREGATE (cr=2 pr=2 pw=0 time=0 us)'
STAT #2 id=2 cnt=1 pid=1 pos=1 bj=69559 p='INDEX RANGE SCAN IDX_T_DETAIL (cr=2 pr=2 pw=0 time=0 us cost=1 size=13 card=1)'
--上述2个单块读的数据块是什么呢?表还是表所属索引的数据块
--可知上述TRACE中的对象不是表
SQL> select owner,object_name,object_id from dba_objects where object_name='T_DETAIL' and wner='SCOTT';
OWNER OBJECT_NAME OBJECT_ID
------------------------------ -------------------------------------------------------------------------------- ----------
SCOTT T_DETAIL 69558
--是不是索引呢,就是索引,所以单块读先是读取索引的数据块
SQL> select owner,object_name,object_id from dba_objects where object_name='IDX_T_DETAIL' and wner='SCOTT';
OWNER OBJECT_NAME OBJECT_ID
------------------------------ -------------------------------------------------------------------------------- ----------
SCOTT IDX_T_DETAIL 69559
--既然读取索引的数据块,哪这是索引的哪个位置的数据块呢
--index的段头块为276482
SQL> select segment_name,HEADER_FILE,header_block from dba_segments ds where ds.segment_name='IDX_T_DETAIL';
SEGMENT_NAME HEADER_FILE HEADER_BLOCK
-------------------------------------------------------------------------------- ----------- ------------
IDX_T_DETAIL 10 276482
--表占用的块
SQL> select segment_name,extent_id,block_id,blocks from dba_extents where wner='SCOTT' and segment_name='T_DETAIL';
SEGMENT_NAME EXTENT_ID BLOCK_ID BLOCKS
-------------------------------------------------------------------------------- ---------- ---------- ----------
T_DETAIL 0 220392 8
T_DETAIL 1 276336 8
T_DETAIL 2 276344 8
--index占用的块
SQL> select segment_name,extent_id,block_id,blocks from dba_extents where wner='SCOTT' and segment_name='IDX_T_DETAIL';
SEGMENT_NAME EXTENT_ID BLOCK_ID BLOCKS
-------------------------------------------------------------------------------- ---------- ---------- ----------
IDX_T_DETAIL 0 276480 8
IDX_T_DETAIL 1 276488 8
IDX_T_DETAIL 2 276496 8
IDX_T_DETAIL 3 276504 8
而上述的10046的trace中单块读 file#=10 block#=276483 blocks=1 obj#=69559,正好位于第一个extent之内
另一个单块读file#=10 block#=276488 blocks=1 obj#=69559,刚好是第二个extent的头一个块