什么叫碎片,碎片对io的影响以及什么叫物理顺序,什么叫逻辑

今天有人问我关于物理顺序和逻辑顺序的事情,就像到了这个话题

页碎片:就是IAM页中叶子页的下一页和页头中该页的下一页不相同,那么就是碎片

逻辑顺序:就是索引的叶子页中通过上一页下一页形成的顺序

物理顺序:就是IAM中分配的顺序

DROP TABLE dbo.t_index
go
CREATE TABLE t_index (id INT PRIMARY KEY ,VALUE CHAR(1000))
GO
INSERT INTO t_index VALUES(1,'1')
INSERT INTO t_index VALUES(2,'2')
INSERT INTO t_index VALUES(3,'3')
INSERT INTO t_index VALUES(4,'4')
INSERT INTO t_index VALUES(6,'6')
INSERT INTO t_index VALUES(7,'7')
INSERT INTO t_index VALUES(8,'8')


INSERT INTO t_index VALUES(5,'5')


DBCC IND (db_test,t_index,1)

PageFID PagePID IAMFID IAMPID ObjectID IndexID PartitionNumber PartitionID iam_chain_type PageType IndexLevel NextPageFID NextPagePID PrevPageFID PrevPagePID
------- ----------- ------ ----------- ----------- ----------- --------------- -------------------- -------------------- -------- ---------- ----------- ----------- ----------- -----------
1 80 NULL NULL 1298103665 1 1 72057594040549376 In-row data 10 NULL 0 0 0 0
1 78 1 80 1298103665 1 1 72057594040549376 In-row data 1 0 1 90 0 0
1 89 1 80 1298103665 1 1 72057594040549376 In-row data 2 1 0 0 0 0
1 90 1 80 1298103665 1 1 72057594040549376 In-row data 1 0 0 0 1 78

那么他们在IAM的分配是:

IAM: Single Page Allocations @0x00000000118EA08E

Slot 0 = (1:78) Slot 1 = (1:89) Slot 2 = (1:90)
Slot 3 = (0:0) Slot 4 = (0:0) Slot 5 = (0:0)
Slot 6 = (0:0) Slot 7 = (0:0)


IAM: Extent Alloc Status Slot 1 @0x00000000118EA0C2

(1:0) - (1:896) = NOT ALLOCATED

肉眼看感觉没有碎片,那么回头想想碎片的定义是显然在2个页子页中间(78,90)中间有一个89索引页那么会被认为是有碎片的。

那么就碎片到底对io性能有什么影响,其实碎片对精确查找的性能影响不是很大(如用唯一的聚集索引查找),但是对区域扫描有影响:

1.如果你的碎片跨区了,那么就要涉及到你设置的分配单元大小了,如果你的分配单元和区一样大,那么可能需要重新寻道等问题导致io时间变成。

2.如果你的碎片没有跨区,那么唯一造成问题的就是你的页密度不够高导致性能问题

原文地址:https://www.cnblogs.com/Amaranthus/p/3077470.html