IAM页面是在统一区分配的还是在混合区分配的?
IAM页面的作用这里就不说了,网上的资料很多
文章中用到的工具:查看SQLSERVER内部数据页面的小插件Internals Viewer
先建立四张表,堆表、聚集索引表、非聚集索引表、聚集索引和非聚集索引表
USE master
GO
--新建数据库IAMDB
CREATE DATABASE IAMDB
GO USE IAMDB
GO --DROP TABLE heaptable
--DROP TABLE clusteredtable
--DROP TABLE nonclusteredtable
--DROP TABLE clusteredandnonclusteredtable --建立测试表
CREATE TABLE heaptable(c1 INT IDENTITY(1,1), c2 VARCHAR (5000))
GO
CREATE TABLE clusteredtable(c1 INT IDENTITY(1,1), c2 VARCHAR (5000))
GO
CREATE TABLE nonclusteredtable(c1 INT IDENTITY(1,1), c2 VARCHAR (5000))
GO
CREATE TABLE clusteredandnonclusteredtable(c1 INT IDENTITY(1,1), c2 VARCHAR (5000))
GO --建立索引
CREATE CLUSTERED INDEX cix_clusteredtable ON clusteredtable([C1])
GO
CREATE INDEX ix_nonclusteredtable ON nonclusteredtable([C1])
GO
CREATE CLUSTERED INDEX cix_clusteredandnonclusteredtable ON clusteredandnonclusteredtable([C1])
GO
CREATE INDEX ix_clusteredandnonclusteredtable ON clusteredandnonclusteredtable([C1])
GO --插入测试数据
DECLARE @a INT;
SELECT @a = 1;
WHILE (@a <= 3)
BEGIN
INSERT INTO heaptable VALUES ( replicate('a', 5000))
SELECT @a = @a + 1
END DECLARE @a INT;
SELECT @a = 1;
WHILE (@a <= 3)
BEGIN
INSERT INTO clusteredtable VALUES ( replicate('a', 5000))
SELECT @a = @a + 1
END DECLARE @a INT;
SELECT @a = 1;
WHILE (@a <= 3)
BEGIN
INSERT INTO nonclusteredtable VALUES ( replicate('a', 5000))
SELECT @a = @a + 1
END DECLARE @a INT;
SELECT @a = 1;
WHILE (@a <= 3)
BEGIN
INSERT INTO clusteredandnonclusteredtable VALUES ( replicate('a', 5000))
SELECT @a = @a + 1
END --查询数据
SELECT * FROM heaptable ORDER BY [c1] ASC
SELECT * FROM clusteredtable ORDER BY [c1] ASC
SELECT * FROM nonclusteredtable ORDER BY [c1] ASC
SELECT * FROM clusteredandnonclusteredtable ORDER BY [c1] ASC
这些表的特点是一行记录刚好占用一页
创建DBCCResult表
USE [IAMDB]
GO
CREATE TABLE DBCCResult (
PageFID NVARCHAR(200),
PagePID NVARCHAR(200),
IAMFID NVARCHAR(200),
IAMPID NVARCHAR(200),
ObjectID NVARCHAR(200),
IndexID NVARCHAR(200),
PartitionNumber NVARCHAR(200),
PartitionID NVARCHAR(200),
iam_chain_type NVARCHAR(200),
PageType NVARCHAR(200),
IndexLevel NVARCHAR(200),
NextPageFID NVARCHAR(200),
NextPagePID NVARCHAR(200),
PrevPageFID NVARCHAR(200),
PrevPagePID NVARCHAR(200)
)
我们看一下heaptable表的IAM页面是哪一个
--TRUNCATE TABLE [dbo].[DBCCResult] INSERT INTO DBCCResult EXEC ('DBCC IND(IAMDB,heaptable,-1) ') SELECT * FROM [dbo].[DBCCResult] ORDER BY [PageType] DESC
IAM页面号为80
看一下clusteredtable表的IAM页面是哪一个
--TRUNCATE TABLE [dbo].[DBCCResult] INSERT INTO DBCCResult EXEC ('DBCC IND(IAMDB,clusteredtable,-1) ') SELECT * FROM [dbo].[DBCCResult] ORDER BY [PageType] DESC
IAM页面号为120
看一下nonclusteredtable表的IAM页面是哪一个
--TRUNCATE TABLE [dbo].[DBCCResult] INSERT INTO DBCCResult EXEC ('DBCC IND(IAMDB,nonclusteredtable,-1) ') SELECT * FROM [dbo].[DBCCResult] ORDER BY [PageType] DESC
IAM页面号为121和110
看一下clusteredandnonclusteredtable表的IAM页面是哪一个
--TRUNCATE TABLE [dbo].[DBCCResult] INSERT INTO DBCCResult EXEC ('DBCC IND(IAMDB,clusteredandnonclusteredtable,-1) ') SELECT * FROM [dbo].[DBCCResult] ORDER BY [PageType] DESC
IAM页面号为126和93
IAM页面号分别为:80、120、121、110、126、93
我们打开SSMS的插件Internals Viewer
点击PFS按钮
可以看到所有IAM页面都在混合区
实际上,如果你用Internals Viewer查看其他数据库的任何一个IAM页都是分配在混合区的
我们在网上也看到
地址:http://msdn.microsoft.com/zh-cn/library/ms174412(v=sql.100).aspx
地址:http://www.cnblogs.com/wcyao/archive/2011/06/28/2092270.html
为什麽始终从混合区进行分配呢??
统一区:由单个对象所有。区中的所有8页只能由一个对象使用
混合区:最多可由8个对象共享。区中8页的每页可由不同对象所有。但是一页总是只能属于一个对象
因为在大部分情况下一个对象(一张表一般只有很少的IAM页),这样的情况下,如果用统一区就显得浪费了
为什麽说大部分情况下呢?如果表中有分区、LOB数据页、行溢出数据页,这时候IAM页就会有很多,尽管这样,
IAM页面还是从混合区分配
详细看这篇文章,IAM页的数量与什么有关
http://www.cnblogs.com/wcyao/archive/2011/06/28/2092270.html
还有使用DMV:dm_db_index_physical_stats来查看表中的页面数量的时候,IAM页是不统计入结果中的
heaptable表
SELECT
[database_id],
[index_id],
[index_type_desc],
[index_level],
[fragment_count],
[page_count],
[avg_fragmentation_in_percent],
[record_count]
from sys.dm_db_index_physical_stats(DB_ID('IAMDB'),object_id('heaptable'),null,null,'detailed')
clusteredtable表
SELECT
[database_id],
[index_id],
[index_type_desc],
[index_level],
[fragment_count],
[page_count],
[avg_fragmentation_in_percent],
[record_count]
from sys.dm_db_index_physical_stats(DB_ID('IAMDB'),object_id('clusteredtable'),null,null,'detailed')
nonclusteredtable表
SELECT
[database_id],
[index_id],
[index_type_desc],
[index_level],
[fragment_count],
[page_count],
[avg_fragmentation_in_percent],
[record_count]
from sys.dm_db_index_physical_stats(DB_ID('IAMDB'),object_id('nonclusteredtable'),null,null,'detailed')
clusteredandnonclusteredtable表
SELECT
[database_id],
[index_id],
[index_type_desc],
[index_level],
[fragment_count],
[page_count],
[avg_fragmentation_in_percent],
[record_count]
from sys.dm_db_index_physical_stats(DB_ID('IAMDB'),object_id('clusteredandnonclusteredtable'),null,null,'detailed')
如有不对的地方,欢迎大家拍砖o(∩_∩)o