SQL Server中Index Allocation Map介绍

本文涉及的产品
云数据库 RDS SQL Server,独享型 2核4GB
简介:

引用:

http://msdn.microsoft.com/en-us/library/ms190969(SQL.105).aspx

正文

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 xmlvarbinary(max), and varchar(max).用于存储大数据对象。

  • ROW_OVERFLOW_DATA

    Holds variable length data stored in varcharnvarcharvarbinary, or sql_variant columns that exceed the 8,060 byte row size limit.用于存储超过 8,060 字节行大小限制的 varcharnvarcharvarbinary 或 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
-------------------------------------------------------------------------------------------
复制代码

结论:

  1. 在表中没有数据的时候,没有IAM page,也没有data page。
  2. 当插入一行数据以后,通过dbcc ind可以查看到一个IAM page和一个data page。虽然一行记录远没有一页大小,但是还是为其分配一个data page。
  3. 当插入1000行记录以后,会新增很多data page,但是IAM page没有新增。这是因为一个IAM page最多可以定位4GB的数据量。
  4. 通过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进行分配。如果有索引的话,那么插入点的位置就由索引来决定。

仅当数据库引擎不能在现有的区中快速找到足以容纳插入行的页时,才将新区分配给分配单元。数据库引擎使用比例分配算法从文件组的可用区中分配区。如果文件组内有两个文件,而一个文件的可用空间是另一个文件的两倍,那么每从后一个文件分配一页,就从前一个文件分配两页。这意味着文件组内的每个文件应该有近似的空间使用百分比。


本文转自xwdreamer博客园博客,原文链接:http://www.cnblogs.com/xwdreamer/archive/2012/07/19/2599114.html,如需转载请自行联系原作者

相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS&nbsp;SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/sqlserver
目录
相关文章
|
4天前
|
SQL 存储 数据库连接
LabVIEW与SQL Server 2919 Express通讯
LabVIEW与SQL Server 2919 Express通讯
|
5天前
|
SQL Windows
安装SQL Server 2005时出现对性能监视器计数器注册表值执行系统配置检查失败的解决办法...
安装SQL Server 2005时出现对性能监视器计数器注册表值执行系统配置检查失败的解决办法...
13 4
|
5天前
|
SQL 数据可视化 Oracle
这篇文章教会你:从 SQL Server 移植到 DM(上)
这篇文章教会你:从 SQL Server 移植到 DM(上)
|
5天前
|
SQL 关系型数据库 数据库
SQL Server语法基础:入门到精通
SQL Server语法基础:入门到精通
SQL Server语法基础:入门到精通
|
6天前
|
SQL 存储 网络协议
SQL Server详细使用教程
SQL Server详细使用教程
30 2
|
6天前
|
SQL 存储 数据库连接
C#SQL Server数据库基本操作(增、删、改、查)
C#SQL Server数据库基本操作(增、删、改、查)
7 0
|
6天前
|
SQL 存储 小程序
数据库数据恢复—Sql Server数据库文件丢失的数据恢复案例
数据库数据恢复环境: 5块硬盘组建一组RAID5阵列,划分LUN供windows系统服务器使用。windows系统服务器内运行了Sql Server数据库,存储空间在操作系统层面划分了三个逻辑分区。 数据库故障: 数据库文件丢失,主要涉及3个数据库,数千张表。数据库文件丢失原因未知,不能确定丢失的数据库文件的存放位置。数据库文件丢失后,服务器仍处于开机状态,所幸未写入大量数据。
数据库数据恢复—Sql Server数据库文件丢失的数据恢复案例
|
7天前
|
SQL 存储 关系型数据库
SQL Server详细使用教程及常见问题解决
SQL Server详细使用教程及常见问题解决
|
8天前
|
SQL 安全 数据库
SQL Server 备份和还原
SQL Server 备份和还原
|
8天前
|
SQL 存储 安全
SQL Server 权限管理
SQL Server 权限管理