sql-server - SQL Server 中的物理页数

标签 sql-server

我正在尝试了解如何在 SQL Server 中创建页面。 我做了一个非常简单的例子:

-- 0 pages
CREATE TABLE TMP_1(
    id int)

-- 2 pages  
INSERT INTO TMP_1
VALUES(1)   

-- 3 pages
SELECT  *
INTO TMP_2
FROM TMP_1 

起初表TMP_1没有页,比较直观,因为没有数据。插入 1 行后有两页。其中一个当然存储数据,但第二个是什么?

表 TMP_2 应该与 TMP_1 相同,但它有 3 页,为什么?

我正在使用以下代码检查页数:

SELECT 
    t.NAME AS TableName,
    p.rows AS RowCounts,
    SUM(a.total_pages) AS TotalPages, 
    SUM(a.used_pages) AS UsedPages, 
    (SUM(a.total_pages) - SUM(a.used_pages)) AS UnusedPages
FROM 
    sys.tables t
INNER JOIN      
    sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN 
    sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN 
    sys.allocation_units a ON p.partition_id = a.container_id
WHERE 
    t.NAME NOT LIKE 'dt%' 
    AND t.is_ms_shipped = 0
    AND i.OBJECT_ID > 255 
    AND t.name LIKE 'TMP%'
GROUP BY 
    t.Name, p.Rows
ORDER BY 
    t.Name  

最佳答案

At first table TMP_1 has no pages, which is rather intuitive because there is no data

Sql server 只会在第一次插入时分配页面

After 1 row is inserted there are two pages. One of course stores data but what is the second one?

这称为 IAM 页面..

SQL Server 使用索引分配映射来跟踪哪些页面已分配给哪个分配单元。它们存储在特殊的 IAM 页面中。

每个 IAM 页面覆盖文件中 64000 个范围的区域,即所谓的 GAM 间隔。如果单个分配单元需要多个IAM页,则形成双链表,即IAM链。

请看下面的演示..

create table t1
(
id int
)

drop table t1
insert into t1
values(1)

DBCC TRACEON (3604);

DBCC IND('PerformanceV3','t1',-1)

输出剥离:

iam_chain_type  PartitionID   PageType
In-row data 72057594048217088   10
In-row data 72057594048217088   1

对于第三个问题:

Table TMP_2 should be identical to TMP_1 but it has 3 pages, why ?

这是一个数据页,我最好的猜测是假设,这是方式 select Into 在内部工作,当您插入时,SQL 总是可以在需要时动态创建页面,但是对于 select into,它会另外创建一个数据页面..

至于为什么,select into 显示所有三个页面都已使用:
我相信分配 DMV 将捕获所有页面分配,但我们在这里谈论的是使用..可以通过下面的 DBCC 命令更准确地看到..

dbcc traceon(3604)
dbcc ind('performancev3','t2',-1)

dbcc page(0,1,36639,3)

引用资料:
http://sqlity.net/en/2315/index-allocation-map/
http://www.sqlskills.com/blogs/paul/inside-the-storage-engine-using-dbcc-page-and-dbcc-ind-to-find-out-if-page-splits-ever-roll-back/

关于sql-server - SQL Server 中的物理页数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40672752/

相关文章:

SQL Server删除含有 "duplicate"数据的行

sql-server - 标签的无连接表结构

c# - 如何在 NHibernate 中映射图像类型?

SQL 更新查询

SQL-在左外连接查询中添加缺失数据

sql-server - 执行transact-sql语句或批处理时发生异常

sql-server - 为什么 select nullif(0 ,'' ) 的输出为 NULL(预期为 0)?

sql-server - SQL Server "AFTER INSERT"触发器看不到刚刚插入的行

mysql - 如何从两个表中获取未映射的行

sql - 动态表格展示