您的位置:首页 > 数据库

SQL Server 中 Index Allocation Map(IAM)介绍 02

2015-11-23 08:10 453 查看
An Index Allocation Map (IAM) page maps the extents in a 4-gigabyte (GB) part of a database file used by an allocation unit. An allocation unit is one of three types(allocation unit 有三种类型,分别是IN_ROW_DATA,LOB_DATA,ROW_OVERFLOW_DATA):

IN_ROW_DATA

Holds a partition of a heap or index.用于存储堆或者索引分区。IN_ROW_DATA的数据行不跨页,也就是最大为8060字节。

LOB_DATA

Holds large object (LOB) data types, such as xml, varbinary(max), and varchar(max).用于存储大数据对象。

ROW_OVERFLOW_DATA

Holds variable length data stored in varchar, nvarchar, varbinary, or sql_variant columns that exceed the 8,060 byte row size limit.用于存储超过 8,060
字节行大小限制的 varchar、nvarchar、varbinary 或 sql_variant 列中存储的可变长度数据。

Each partition of a heap or index contains at least an IN_ROW_DATA allocation unit. It may also contain a LOB_DATA or ROW_OVERFLOW_DATA allocation unit, depending on the heap or index schema. For more information about allocation units,
seeTable and Index Organization.下面通过一个demo展示IAM的信息。

---实验4:描述堆结构------------------------
--1.创建堆heap
CREATE TABLE Student
(
stuid    INT          NOT NULL,
stuname  NVARCHAR(40) NOT NULL,
);

--2.插入一条记录
insert into Student values(1,'zhangsan');

--3.查看索引页信息,发现IndexID=0,表示这是堆结构heap。其中PartitionID=72057594041466880
dbcc ind ( TESTDB3, [Student], -1)

--4.查看所有IAM信息,
select * from sys.system_internals_allocation_units
--5.查看PartitionID=72057594041466880的IAM信息,发现total_pages=2,data_pages=1
select * from sys.system_internals_allocation_units where container_id=72057594041466880

--6.插入1000条记录,超过一个索引页跟数据页的容量。
SET NOCOUNT ON--不统计影响行数
declare @i int
set @i=1
while @i<=10000
begin
insert into Student values(1,'zhangsan');
set @i=@i+1
end

--7.查看表的页信息,有一个IAM PAGE,43个data page
dbcc ind ( TESTDB3, [Student], -1)

--8.再次查看IAM PAGE的信息,total_pages=49,used_pages=44,data_pages=43。
select * from sys.system_internals_allocation_units where container_id=72057594041466880
-------------------------------------------------------------------------------------------


结论:

在表中没有数据的时候,没有IAM page,也没有data page。
当插入一行数据以后,通过dbcc ind可以查看到一个IAM page和一个data page。虽然一行记录远没有一页大小,但是还是为其分配一个data page。
当插入1000行记录以后,会新增很多data page,但是IAM page没有新增。这是因为一个IAM page最多可以定位4GB的数据量。
通过dbcc ind我们可以找到IAM page的PartitionID,这个PartitionID对应的是sys.system_internals_allocation_units这个系统视图中的container_id字段。我们可以根据这个来查看IAM page一共maps多个data page。

sys.parititions(PS:2012-7-24)

Heap Structures中提到“A heap is a table without a clustered index. Heaps have one row in sys.partitions,
with index_id = 0 for each partition used by the heap.”因此我们也可以在sys.partitions这个视图中找到关于partition的信息,代码如下:

--在sys.partitions视图中也可以找到这个partition
select * from sys.partitions where partition_id=72057594041860096


IAM结构

An IAM page covers a 4-GB range in a file and is the same coverage as a GAM or SGAM page.If the allocation unit contains extents from more than one file, or more than one 4-GB range of a file, there will be multiple IAM pages linked in an IAM chain.
Therefore,each allocation unit has at least one IAM page for each file on which it has extents. There may also be more than one IAM page on a file, if the range of the extents on the file allocated to the allocation unit exceeds the range
that a single IAM page can record.



IAM 页有一个标头,指明 IAM 页所映射的区范围的起始区。IAM 页中还有一个大位图,其中每个位代表一个区(extent)。位图中的第一个位代表范围内的第一个区,第二个位代表第二个区,依此类推。如果某个位是 0,这个位所对应的区不会被分配给拥有该 IAM 页的allocation unit。如果这个位是 1,则可以分配。

当 SQL Server 数据库引擎必须在当前页中插入新行,而当前页中没有可用空间时,它将使用 IAM 和PFS 页来查找应该将数据插入到哪一个区中的页,或者是说(对于堆或 Text/Image 页)查找具有足够空间容纳该行的页。数据库引擎使用
IAM 页查找那些可以分配给allocation unit的区(也就是bitmap 上位1的区)。对于每个区,数据库引擎将搜索 PFS 页,以查看是否有可用的页。具体可以参考:http://msdn.microsoft.com/en-us/library/ms175195(SQL.105).aspx。每个
IAM 和 PFS 页覆盖大量数据页,因此一个数据库内只有很少的 IAM 和 PFS 页。这意味着 IAM 和 PFS 页通常位于内存中的 SQL Server 缓冲池中,所以能够很快找到它们。如果数据库有索引,那么新行的插入点将由索引键设置,而不再需要IAM页来设置。在这种情况下,不会出现上述搜索过程。假如我们创建的是堆,也就是说既没有聚集索引,有没有非聚集索引的表,我们使用IAM进行分配。如果有索引的话,那么插入点的位置就由索引来决定。

仅当数据库引擎不能在现有的区中快速找到足以容纳插入行的页时,才将新区分配给分配单元。数据库引擎使用比例分配算法从文件组的可用区中分配区。如果文件组内有两个文件,而一个文件的可用空间是另一个文件的两倍,那么每从后一个文件分配一页,就从前一个文件分配两页。这意味着文件组内的每个文件应该有近似的空间使用百分比。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: