h2 { margin-top: 0.46cm; margin-bottom: 0.46cm; direction: ltr; line-height: 173%; text-align: justify; page-break-inside: avoid }
h2.western { font-family: "Cambria", serif; font-size: 16pt }
h2.cjk { font-family: "宋体"; font-size: 16pt }
h2.ctl { font-size: 16pt }
h1 { margin-top: 0.6cm; margin-bottom: 0.58cm; direction: ltr; line-height: 241%; text-align: justify; page-break-inside: avoid }
h1.western { font-family: "Calibri", serif; font-size: 22pt }
p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify }
a:link { color: rgb(0, 0, 255) }

1.创建测试表

SQL>
CREATE TABLE TEST AS SELECT * FROM dba_objects WHERE 0=1;

2.创建测试索引

SQL>
CREATE INDEX ind_test_id ON TEST(object_id);

3.插入测试数据

SQL>
INSERT INTO TEST SELECT * FROM dba_objects WHERE
object_id IS NOT NULL AND object_id > 10000 ORDER BY object_id
DESC;

17837
rows created.

4.分析表
附带索引等等

SQL>
analyze table test compute statistics for table for all columns for
all indexes;

Table
analyzed.

5.打开执行计划

SQL>
set autotrace trace;

6.FFS示例

SQL>
select object_id from test;

17837
rows selected.

Execution
Plan

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

0
SELECT STATEMENT Optimizer=CHOOSE (Cost=68 Card=17837
Bytes=71348)

1
0 TABLE ACCESS (FULL) OF 'TEST' (Cost=68 Card=17837 Bytes=71348)

这时候
Oracle会选择全表扫描,因为
object_id
列默认是可以为null的,来修改成
not null

6.1修改字段属性
not null

SQL>alter
table test modify(object_id not null);

6.2再次验证
FFS

SQL>
select object_id from test;

17837
rows selected.

Execution
Plan

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

0
SELECT STATEMENT Optimizer=CHOOSE (Cost=11 Card=17837
Bytes=71348)

1
0 INDEX (FAST FULL SCAN) OF 'IND_TEST_ID' (NON-UNIQUE) (Cost=11
Card=17837 Bytes=71348)

没有问题

7. IFS
示例

SQL>
select/*+ index(test ind_TEST_ID)*/ object_id from test;

17837
rows selected.

Execution
Plan

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

0
SELECT STATEMENT Optimizer=CHOOSE (Cost=41 Card=17837
Bytes=71348)

1
0 INDEX (FULL SCAN) OF 'IND_TEST_ID' (NON-UNIQUE) (Cost=101
Card=17837 Bytes=71348)

没有问题

我们看到了两者都可以在这种情况下使用,那么他们有什么区别呢?有个地方可以看出两者的区别,
来看一下两者的输出结果,为了让大家看清楚一点,我们只取10行。

8结果验证

SQL>
set arraysize 1000;

SQL>
alter system flush buffer_cache; ----一定要刷新,不然观察不到
db file sequential
read

SQL>
alter system flush shared_pool;

SQL>
alter session set events '10046 trace name context forever, level 8';

8.1FFS(INDEX
FAST FULL SCAN)

SQL>
select object_id from test where rownum<11;

OBJECT_ID

----------

66266

66267

66268

66269

66270

66271

66272

66273

66274

66275

10
rows selected.

SQL>
alter session set events '10046 trace name context off';

检查该索引所属文件号、段头快

SQL>
select owner,header_file,header_block from dba_segments where
segment_name='IND_TEST_ID';

OWNER
HEADER_FILE HEADER_BLOCK

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

OWNER
4 3562

段头块为
3562,后退一个即
索引的 root block
3563

SQL>
set arraysize 1000;

SQL>
alter system flush buffer_cache; ----一定要刷新,不然观察不到
db file sequential
read

SQL>
alter system flush shared_pool;

SQL>
alter session set events '10046 trace name context forever, level 8';

以下内容取自
10046 event trace文件

=====================

PARSING
IN CURSOR #2 len=42 dep=0 uid=88 oct=3 lid=88 tim=1478672879417440
hv=3715463873 ad='cf77db60' sqlid='9rkncnzfrayq1'

select
object_id from test where rownum<11

END
OF STMT

PARSE
#2:c=12998,e=13339,p=15,cr=61,cu=0,mis=1,r=0,dep=0,og=1,plh=1931801113,tim=1478672879417411

EXEC
#2:c=0,e=103,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,plh=1931801113,tim=1478672879417635

WAIT
#2: nam='SQL*Net message to client' ela= 12 driver id=1650815232
#bytes=1 p3=0 obj#=0 tim=1478672879417740

WAIT
#2: nam='db file sequential read' ela= 24 file#=4 block#=3562
blocks=1 obj#=81680 tim=1478672879417839 --第四个数据文件的
3562数据块
也就是从段头块开始 ,依次读取 3563数据块
。3563数据块一次读入5个数据块

WAIT
#2: nam='db file sequential read' ela= 11 file#=4 block#=21761
blocks=1 obj#=81680 tim=1478672879417916

WAIT
#2: nam='db file sequential read' ela= 7 file#=4 block#=3561
blocks=1 obj#=81680 tim=1478672879417940

WAIT
#2: nam='db file scattered read' ela= 5 file#=4 block#=11008
blocks=2 obj#=81680 tim=1478672879417964

WAIT
#2: nam='db file scattered read' ela= 9 file#=4 block#=3563
blocks=5 obj#=81680 tim=1478672879418008

FETCH
#2:c=1000,e=270,p=10,cr=12,cu=0,mis=0,r=1,dep=0,og=1,plh=1931801113,tim=1478672879418046

WAIT
#2: nam='SQL*Net message from client' ela= 118 driver
id=1650815232 #bytes=1 p3=0 obj#=81680 tim=1478672879418186

WAIT
#2: nam='SQL*Net message to client' ela= 0 driver id=1650815232
#bytes=1 p3=0 obj#=81680 tim=1478672879418213

FETCH
#2:c=0,e=18,p=0,cr=1,cu=0,mis=0,r=9,dep=0,og=1,plh=1931801113,tim=1478672879418225

STAT
#2 id=1 cnt=10 pid=0 pos=1 obj=0 op='COUNT STOPKEY (cr=13 pr=10
pw=0 time=0 us)'

STAT
#2 id=2 cnt=10 pid=1 pos=1 obj=81680 op='INDEX FAST FULL SCAN
IND_TEST_ID (cr=13 pr=10 pw=0 time=0 us cost=2 size=40 card=10)'

WAIT
#2: nam='SQL*Net message from client' ela= 239 driver
id=1650815232 #bytes=1 p3=0 obj#=81680 tim=1478672879418502

***
SESSION ID:(1.13) 2016-11-09 14:27:59.419

结论:FFS会读取
段头块,并且会多块读

最开始扫描的是3562,它是索引的段头,并且是单块读(注意:段头都是单块读),然后才是从3563
开始扫描,一共扫描了5个block
3563就是索引的root
block

8.2FS(INDEX
FULL SCAN)

SQL>
set arraysize 1000;

SQL>
alter system flush buffer_cache; ----一定要刷新,不然观察不到
db file sequential
read

SQL>
alter system flush shared_pool;

SQL>
alter session set events '10046 trace name context forever, level 8';

SQL>
select/*+ index(test ind_TEST_ID)*/ object_id from test where
rownum<11;

OBJECT_ID

----------

10616

12177

12178

12179

12301

13495

13536

13539

13923

16503

10
rows selected.

SQL>
alter session set events '10046 trace name context off';

以下内容取自
10046 event trace文件

=====================

PARSING
IN CURSOR #4 len=72 dep=0 uid=88 oct=3 lid=88 tim=1478673548236909
hv=2159188642 ad='cf9c1348' sqlid='344baf60b56p2'

select/*+
index(test ind_TEST_ID)*/ object_id from test where rownum<11

END
OF STMT

PARSE
#4:c=27996,e=28261,p=17,cr=61,cu=0,mis=1,r=0,dep=0,og=1,plh=2443641574,tim=1478673548236908

EXEC
#4:c=0,e=15,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,plh=2443641574,tim=1478673548236966

WAIT
#4: nam='SQL*Net message to client' ela= 2 driver id=1650815232
#bytes=1 p3=0 obj#=0 tim=1478673548237005

WAIT
#4: nam='db file sequential read' ela= 10 file#=4 block#=3563
blocks=1 obj#=81680 tim=1478673548237648 --直接跳过
3562数据块(也就是跳过段头块)

WAIT
#4: nam='db file scattered read' ela= 31 file#=4 block#=3564
blocks=4 obj#=81680 tim=1478673548237730 读取3564数据块
一次读入4个数据块

FETCH
#4:c=1000,e=735,p=5,cr=2,cu=0,mis=0,r=1,dep=0,og=1,plh=2443641574,tim=1478673548237758

WAIT
#4: nam='SQL*Net message from client' ela= 124 driver
id=1650815232 #bytes=1 p3=0 obj#=81680 tim=1478673548237914

WAIT
#4: nam='SQL*Net message to client' ela= 1 driver id=1650815232
#bytes=1 p3=0 obj#=81680 tim=1478673548237949

FETCH
#4:c=0,e=22,p=0,cr=1,cu=0,mis=0,r=9,dep=0,og=1,plh=2443641574,tim=1478673548237962

STAT
#4 id=1 cnt=10 pid=0 pos=1 obj=0 op='COUNT STOPKEY (cr=3 pr=5 pw=0
time=0 us)'

STAT
#4 id=2 cnt=10 pid=1 pos=1 obj=81680 op='INDEX FULL SCAN
IND_TEST_ID (cr=3 pr=5 pw=0 time=0 us cost=2 size=40 card=10)'

WAIT
#4: nam='SQL*Net message from client' ela= 193 driver
id=1650815232 #bytes=1 p3=0 obj#=81680 tim=1478673548238201

***
SESSION ID:(1.13) 2016-11-09 14:39:08.239

结论:这个索引的段头块是3562,root
block就是段头+1
,这里 root
block 就是3563
,根据实验可知,index
full scan 没有扫描 segment
header ,而是直接扫描 root
block3563、leaf
block 3564

结论:两者的结果完全不一样,这是为什么呢?

这是因为当进行index
full scan 的时候 oracle跳过段头
定位到索引的root
block,然后到branch
block(如果有的话),再定位到第一个leaf
block, 然后根据leaf
block的双向链表顺序读取。它所读取的块都是有顺序的,也是经过排序的。

而进行index
fast full scan则不同,它是从段头开始,读取包含位图块,root
block,所有的branch
block, leaf
block,读取的顺序完全由物理存储位置决定,并采取多块读,每次读取db_file_multiblock_read_count个块

9.原因考证

归纳:

索引类别

访问方式

是否排序

FFS

先扫描 segment
header,读取索引的段头,然后开始读取
root block、brunch
block、leaf
block

多一步 sort
(order by)

FS

不扫描 segment
header, 跳过索引的段头,而是直接扫描
root block、brunch
block、leaf
block

自动的执行 sort
(order by)

详情

为什么
index
fast full scan 要扫描
segment
header呢?因为
index
fast full scan 需要扫描所有的索引块(leaf
block),并且扫描不是有序的,是多块读,而且它不会回表,也就是说它不会解析出rowid,正是由于它要扫描所有的leaf
block,并且是离散读,所以它必须读取segment
header,不然Oracle怎么知道它读取了所有的
leaf
block

为什么
index
full scan 不扫描segment
header? 因为
index
full scan 是连续读的,由于leaf
block之间有双向指针,Oracle不需要扫描segment
header就能判断
leaf
block 扫描完了没,它只需要从左往右,或者从右往左一直扫描到尽头即可。

index ffs、index fs原理考究-1109的更多相关文章

  1. 【从翻译mos文章】采用高速全扫描索引(index ffs) 为了避免全表扫描

    采用高速全扫描索引(index ffs) 为了避免全表扫描 参考原始: Index Fast Full Scan Usage To Avoid Full Table Scans (Doc ID 701 ...

  2. index index.html index.htm index.php

    server { listen 80; server_name localhost; index index.html index.htm index.php;#前后顺序有关系,越在前优先级别越高 r ...

  3. nginx -t "nginx: [warn] only the last index in "index" directive should be absolute in 6 "的问题解决

    修改完nginx的配置文件之后,执行nginx -t命令提示"nginx: [warn] only the last index in "index" directive ...

  4. 14.8.11 Physical Structure of an InnoDB Index InnoDB Index 的物理结构

    14.8.11 Physical Structure of an InnoDB Index InnoDB Index 的物理结构 所有的InnoDB indexes 是 B-trees Index r ...

  5. 14.2.5.4 Physical Structure of an InnoDB Index InnoDB Index 的物理结构

    14.2.5.4 Physical Structure of an InnoDB Index InnoDB Index 的物理结构 所有的InnoDB indexes 是B-trees ,index ...

  6. MySQL 执行计划中Extra(Using where,Using index,Using index condition,Using index,Using where)的浅析

      关于如何理解MySQL执行计划中Extra列的Using where.Using Index.Using index condition,Using index,Using where这四者的区别 ...

  7. [Oacle][Partition]Partition操作与 Index, Global Index 的关系

    [Oacle][Partition]Partition操作与 Index, Global Index 的关系: ■ Regarding the local index and the global i ...

  8. C 缓冲区过读 if (index >= 0 && index < len)

    C 缓冲区过读 if (index >= 0 && index < len) CWE - CWE-126: Buffer Over-read (3.2) http://cw ...

  9. Sql Server中的表访问方式Table Scan, Index Scan, Index Seek

    1.oracle中的表访问方式 在oracle中有表访问方式的说法,访问表中的数据主要通过三种方式进行访问: 全表扫描(full table scan),直接访问数据页,查找满足条件的数据 通过row ...

随机推荐

  1. python手记(30)

    #!/usr/bin/env python #-*- coding: utf-8 -*- import cv2 import numpy as np fn="test3.png" ...

  2. 【HDOJ】1325 Is It A Tree?

    并查集.需要考虑入度. #include <stdio.h> #include <string.h> #define MAXNUM 10005 int bin[MAXNUM]; ...

  3. lambda -- Filter Java Stream to 1 and only 1 element

    up vote10down votefavorite I am trying to use Java 8 Streams to find elements in a LinkedList. I wan ...

  4. Linux学习笔记28——消息队列

    一 关于消息队列 消息队列提供了一种从一个进程向另一个进程发送一个数据块的方法,而且,每个数据块都被认为含有一个类型,接收进程可以独立地接受含有不同类型值的数据块.可以通过发送消息来几乎完全避免命名管 ...

  5. ubuntu安装jdk1.8

    sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracle-java8-i ...

  6. HTML---网页编程(2)

    前言 接着前面的HTML-网络编程1)来学习吧~~~ 色彩的表示 在计算机显示器中,使用红(red).绿(green).蓝(blue)3种颜色来构成各种各样的颜色.颜色的种类有16,256及65536 ...

  7. HDOJ/HDU 2539 点球大战(String.endsWith()方法的一个应用~)

    Problem Description 在足球比赛中,有不少赛事,例如世界杯淘汰赛和欧洲冠军联赛淘汰赛中,当比赛双方经过正规比赛和加时赛之后仍然不分胜负时,需要进行点球大战来决定谁能够获得最终的胜利. ...

  8. MVC项目初次发布到IIS可能会遇到的问题

    MVC4 + .NET Framework 4.5 +Windows Server 2008+ IIS7.5 + 4.0集成模式池 ,初次发布后可能会遇到404.0 或者403.14错误,加入以下代码 ...

  9. IOS学习之路十五(UIView 添加背景图片以及加边框)

    怎样给UIview添加背景图片呢很简单,就是先给view添加一个subview,然后设为背景图片: 效果图如下: 很简单直接上代码: //设置内容 self.myTopView.backgroundC ...

  10. 银联手机支付(.Net Csharp),3DES加密解密,RSA加密解密,RSA私钥加密公钥解密,.Net RSA 3DES C#

    前段时间做的银联支付,折腾了好久,拼凑的一些代码,有需要的朋友可以参考,本人.Net新手,不保证准确性! 这个银联手机支付没有SDK提供,技术支持也没有.Net的,真心不好搞! RSA加解密,这里有个 ...