Storage Structure and Relationships

Objectives

After completing this lesson, you should be able to do the following :

  • Describe the logical structure of the database
  • List the segment types and their uses
  • List the keywords that control block space usage
  • Obtain information about storage structures from the data dictionary
  • List the criteria for separating segments

 tablespace 分三种:permernent , undo , temp

segment : 对应我们数据库的对象,比如一个表,索引等等。

extent : 连续的 数据库的 data block (extent是逻辑上连续, 物理上不用连续), 因为逻辑上 segment 是分为多个extent的, 而且segment是可以横跨datafile的, 既然segment可以横跨datafile, 那么就有可能存在一个segment里的两个多个extent属于不同的datafile.

当分配物理空间时, 指定到segment级别时, 就可以指定分配哪个extent了, 已经指定extent的大小(uniform除外).

1. Types of segments

一个segment可以横跨多个 datafile. 而一个 extent 只是属于一个datafile.

Segments are space-occupying objects in a database. They use space in the data files of a database. This section describes the different types of segments.

  • Table : ( 不包括 cluster(将2个或多个表捆绑在一起) 和 partition ) .
  • Table Partition : ( 当多并发的情况出现时, 可以考虑使用 table partitions. 每一个 table partition 是一个 segment )
  • Cluster : A cluster, like a table, is a type of data segment, rows in a cluster are stored based on ky column values. A cluster may contain one or more tables. Tables in a cluster belong to the same segment and share the same storage characteristics.
  • Index : If a table has three indexes, three index segments are used.
  • Index-Organized Table : In an index-organized table, data is stored within the index based on the key value.
  • Index partition : An index can be partitioned and spread across several tablespaces.
  • undo segment : is used by a transaction that is making changes to a database. Before chaging the data or index blocks, the old value is stored in the undo segment.
  • Temporary Segment : 排序, When a sort needs more space than the space available in memory, intermediate results are written to the disk. Temporary segments are used to store these intermediate results.
  • LOB Segment : If the column is large, the Oracle server stores these values in separate segments known as LOB segments. The table contains only a locator or a pointer to the location of the corresponding LOB data.
  • Nested Table : 表中的某个列还是表.
  • Bootstrap Segment : also known as a cache segment . is created by the sql.bsq script when a database is created , This segment helps to initialize the data dictionary cache when the database is opened by an instance. The bootstrap segment cannot be queried or updated and does not require any maintenance by the database administrator. ( 不用DBA操心)


partition table : 逻辑上是一个 table , 但是 物理上其实是独立的 table .

cluster : 集群,很多表的内容有相关性,那么在物理上就存储在一起,查询起来非常方便。

IOT 表:index-orginized table : ( 普通的表,只存储信息,还在在该表之上增加索引方便查询,IOT则是这个表本身存储也是有顺序的,即将表和索引合二唯一了。)

上边的 table 有 partition , 同样,index 也有 partition , 对应的,因为 partition的表的索引就是 index partition.

undo segment

temporary segment

LOB segment

nested table : 嵌套表内也自己占有一个 segment.


2. Storage parameter

存储内容的这些参数优先级别,首先是如果你指定了segment级别的参数,其次是你指定了tablespace级别的参数,最后才是默认级别的参数。

优先级别



A storage clause can be specified at the segment level to control how extents are allocated to a segment.

  • Any storage parameter specified at the segment level overrides the corresponding option set at the tablespace level, except for the MINIMUM EXTENT or UNIFORM SIZE tablespace parameter.
  • When storage parameters are not set explicitly at the segment level, the default to those at the tablespace level.
  • When storage parameters are not set explicitly at the tablspace level, the oracle server system defaults are used.

另外需要考虑的内容

  • if sotrage parameters are altered , the new options apply only to the extents not yet allocated.
  • Some parameters cannot be specified at the tablespace level. These parameters need to be specified at the segments level only.
  • if minimum extent size has been specified for the tablespace, this size applies to all extents that are allocated for segments in the tablespace in the future.

3. Extent Allocation and Dallocation

补充 :

逻辑 : 1个tablespace 包含若干个 segment , 1个 segment 包含若干个 extent , 1个 extent 包含若干个 oracle block .

物理 : 1个tablespace 包含若干个 datafile , 1个 segment 包含若干个 datafile, 一个 extent 只能属于一个datafile , 一个oracle block 包含若干个 OS block .

分配时候 : segment created, segment extended, segment altered ( 扩大 )

删除时候 : segment droped , segment altered ( 缩小 ) , segment truncated


When a tablespace is created, the data files in the tablespace contain a header , which is the first block or blocks in the file.

As segments are created , they are allocated space from the free extents in a tablespace.

Contiguous space used by a segment is referred to as a used extent. when segments release space, the extents that are released are added to the pool of free extents available in the tablespace.

 


4. Data blocks 包含以下内容:

DB_BLOCK_SIZE : 2k ~ 32k ( 创建数据库之前就设定好,不能改 ) SYSTEM , TEMPORARY 一定是 standard size. ( 好象是 8 ke )

DB_BUFFER_CACHE :  专门缓存 standard block size 的

db_2k_cache_size , db_4k_cache_size, ...

以上的作用主要是方便转移数据,比如 你的数据本来是普通的,standard block size 是 8k , 但是如果是转移到分析数据库里,由于分析数据库里的 standard block size 是比较大,比如 16k, 所以,如果统一的话,就方便转移数据。

一般使用不到

一个 table 的不同 partition 必须使用同样的 block size

tempory tablespace , permernant tablespace 必须使用 standard block size .


block header contains the data block address, table directory, row directory, and transaction slots that are used when transactions make changes to rows in the block.(从上到下)

 Data space , row data is inserted into the block from bottom up. ( 从下到上 )

 Free space : the free space in a block is in the middle of the block thus both the header and the row data space can grow when necessary. the free space in a block is

                      coninguous initally. However, deletions and updates may fragment the free space in the block, the free space in the block is coalesced by the Oracle when

                      when necessary. ( 中间,初始化时连续,后来可能由于删除啊,修改等操作,但是不连续,)



block space 参数

INITRANS , MAXTRANS specify the initial and the maximum number of transaction slots(事物槽) that are created in an index or a data block. the transaction slots are used to store information about transactions that are making changes to the block at a point in time .  用于控制并发性。

这两个参数是事务槽数量, 一个transaction事务 只用一个事务槽, 即使它改变了多行或索引.

(个人感觉, datablock 是用来存储数据的, 为什么还是存储事务信息呢?事务信息是要占用block空间的.)

INITRANS, which defaults to 1 for a data segment and 2 for an index segment, guarantees a minimum level of concurrency, For example, if set to 3, INITRANS ensures that at least three transactions can concurrently make changes to the block. if necessary, additional transaction slots can be allocated from the free space in the block to permit more concurrent transactions to modify rows in the block.

MAXTRANS, which has a default value of 255, sets the limit for the number of concurrent transactions that can make changes to a data or an index block. when set, this value restricts use of space for transaction slots and therefore guarantees that there is sufficient space in the block for use by row or index data.

事务要修改块中的数据, 必须获得该块中的一个 itl (事务槽), 通过 itl 和 undo segment header 中的 transaction table, 可以知道事务处于活动阶段, 还是已经完成. 事务在修改块时(起始就是在修改行) 会检查行中 row header 中的标志位, 如果该标志位为 0 (该行没有被活跃的事务锁住, 这是可能要进行 deferred block clenaout 等工作), 就把该标志修改为事务在该块获得的 itl 序号, 这样当前事务就获得了对记录的锁定, 然后就可以修改数据了, 这也就是 oracle 行锁实现的原理.

ITL 也叫事务槽, 它位于 block header.

一个事务可能涉及多个BLOCK的更改, 所以一个事务可能在多个block 中产生 ITL 信息.

PCTFREE for a data segment specifies the percentage of space in each data block reserved for growth resulting from updates to rows in the block .最少有多少空闲空间,为了已存在数据的扩张,比如 update 当前已存在的数据。比如 10%

为什么要留出空间来呢? 这个留出空间的意思是,当这个 datablock 存储到一定程度的时候,剩下的空间是不能再继续往里存储数据的,这是为什么呢?

因为需要留下这些空间为了 已经存储在这个 data block 里的数据被修改使用的,比如这个 data block里的数据被修改了,尤其是增大了,那么需要把相关内容

存储在这个data block中,所以要预留一些空间.

 

PCTUSED for a data segment represents the minimum percentage of used space that the Oracle server tries to maintain for each data block of the table. 默认 40% 

A block is put back on the free list when its used space falls below PCTUSED. The free list of a segment is a list of blocks that are candidates for accommodating future inserts. A segment, by default, is created with one free list, Segments can be created with a higher number of free lists by setting the FREELISTS parameter of the storage clause. The default for PCTUSED is 40%.

Both PCTFREE and PCTUSED are calclated as percentages of available data space, that is, the block space that remains after deducting the header space from the total block size.

下边的链表叫空闲链表,首先pctused这个概念必须是删除数据时相关,即当一个新块,你往里边插数据,没关系,你可以插入到该块的90%都没有问题,但是当你删除该块的数据,就必须要删除的40%已下之后,这个块才会被放置到空闲链表上,你才可以继续向该块插入数据(从空闲链表上找到该数据)

作用是 : 比如要插入数据到 datablock 中, extent 有个链表 free list,链表上的节点就是个个 data block ( 那些存储的数据小于  PCTUSED 的 datablock) 即当新插入数据时, oracle 会自动找那些存储空间多的地方插入,判断的依据就是 PCTUSED )

PCTFREE , PCTUSED 单位都是  %

PCTUSED 参数用于决定一个数据快(data block) 是否可被用于插入新数据(放置在空闲链表中),她的依据是数据区(rowdata)与数据块头(overhead))的容量之和占数据块全部容量的最大百分比

当一个数据块中的可用空间比例小于 PCTUSED 参数的规定时,ORACLE就认为此数据块无法被用于插入新数据,直到数据块中的占用容量比例小于PCTUSED参数的限定

一、建立表时候,注意PCTFREE参数的作用

      PCTFREE:为一个块保留的空间百分比,表明是否可以被 insert 操作, 默认是10,表示当数据块的可用空间低于10%后,就不可以被insert了,只能被用于update;即:当使用一个block时,在达到pctfree之前,该block是一直可以被插入的,这个时候处在上升期。

      PCTUSED:是指当块里的数据低于多少百分比时,又可以重新被insert,一般默认是40,即40%,即:当数据低于40%时,又可以写入新的数据,这个时候处在下降期。表明是否可以被放到 freelist 空闲表上.

二、举例说明

      假设你一个块可以存放100个数据,而且PCTFREE 是10,PCTUSED是40,则:不断的向块中插入数据,如果当存放到90个时,就不能存放新的数据,这是受pctfree来控制,预留的空间是给UPDATE用的。注意, 这时, 这个块会被从 freelist 空闲表中拿出来, 所以不能再插入数据了.

      当你删除一个数据后,再想插入个新数据行不行?不行,因为这个块还没有别拿到freelist上, 必须是删除41个,即低于40个以后才能插入新的数据的,这是受pctused来控制的。

      注意:如果表空间上启用了ASSM,在建立表的时候,只能指定PCTFREE,否则可用指定PCTFREE和PCTUSED。

三、调整pctfree与pctused
1、通过user_tables的pct_free,pct_used来查看
select a.table_name, a.pct_free, a.pct_used, a.* from user_tables a; 
说明:
pctfree表示用于保留更新操作的百分比,如果超过该值不能插入数据。
pctused表示数据所占最低百分比,如果达到pctfree时不能插入,delete后如果达到pctused才可以用来insert

2、如果你使用的是自动管理表空间pctused不需要设置
altertable tablename pctfree values;
说明:
pctfree默认是10,主要看更新的数据有多大,可以查看表的max_row_len如果很大又频繁更新可以考虑增加该值。
pctused主要看删除数据的大小,如果很大可以调大该值,如果不是很频繁可以设置小一些30-40
pctused+pctfree<90

 

3、查看自动管理表空间
select tablespace_name,segment_space_management from user_tablespaces; --segment_space_management为auto表示自动管理表空间
主要起到节省表空间的作用。


5. Data Block management

2种方法 : automatic segment-space management ,      manual configuration

先说自动管理 :

Tracking in -segment free and used space is done using bitmaps as opposed to usingfree lists

bitmap 在这个segment 里是独立存储的。专门的空间。

 automatic 管理 :

CREATE TABLESPACE data02
DATAFILE 'u01/oradata/data02.dbf' SIZE 5M
EXTENT MANAGEMENT LOCAL UNIFORM SIZE 64K
SEGMENT SPACE MANAGEMENT AUTO;  -- auto management


手工管理

必须要指定参数 :

- PCTFREE

-PCTUSED

-FREELIST


High PCTFREE , Low PCTUSD ( 大量的空余空间,如果数据经常更改,并且是增加的 update ) 则比较适合这种

Low PCTFREE, High PCTUSD ( 相反,没有频繁的增加 , 或者只是删除)

High Water Mark ( 高水位 ) HWM

类似 竖一个杆子在河里,来看最高水位。

左边的已经保存的输入,如果删除了,这个 hwm 不会下降,所以在 water mark 左右都有可能有空间。

算法  : 先搜索灰色的已经有数据的部分是否够存储,如果不够继续搜索 water mark 左边,并且没有使用的块( 比如之前有存储,后来删除的 ), 如果还不够,那么使用 water mark 右边的大空间存储,并同时移动 water mark.

select count(*) from table ; 操作是 oracle 从第一个块一直搜索到 water mark , 不管之前是否有数据,它都要搜索。

所以当你创建一个空的 table , select count(*) from table ; 速度很快,但是,如果你先插入了100W数据,后又删除了全部100W数据,虽然目前也是空表,但是由于 water mark位置变化了。所以,此时再执行 select count(*) from table 就会很需要时间。

truncate 操作,会将 water mark 归位。

以下图中的每个长条应该是一个extent,  每一个小方块是一个data block,


 

6. Data Dictionary Views

Used extents DBA_EXTENTS

reee extents DBA_FREE_SPACE

segments DBA_SEGMENTS

Data files DBA_DATA_FILES

tablespace DBA_TABLESPACES

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值