The current index structure for SQL Server is as follows:
This is the storage form of the clustered index:
Nonclustered indexes are in the following ways:
They are stored in the data structure of a B + tree.
I believe we have seen similar graphs, but there is no intuitive understanding, the following is a practical example to illustrate the structure of the graph.
1234567891011 |
USE Test
--1.创建表,指定主键(会自动创建聚集索引)
CREATE
TABLE
Person
(
Id
int
NOT
NULL
IDENTITY,
Name
varchar
(10)
NOT
NULL
,
Sex
varchar
(2)
NOT
NULL
,
CONSTRAINT PK_Person
PRIMARY
KEY
(Id)
);
--2.创建非聚集索引
CREATE
INDEX
idx_Person_Sex
ON
Person(Sex);
|
1234 |
--3.插入1笔数据 Insert Person values ( ‘P0‘ , ‘M‘ ); --4.查看表有哪些页 DBCC ind ( Test, [dbo.Person], -1) |
1234 |
--5. 打开3604监控 DBCC TraceON(3604,-1) --6.查看聚集索引叶子节点页的数据 DBCC PAGE (Test,1,174, 1); |
12345678910111213141516171819202122 |
--7.插入1000条M和500条W记录
SET
NOCOUNT
ON
;
GO
DECLARE
@i
int
;
SET
@i = 1000;
WHILE @i < 2000
BEGIN
Insert Person
values
(
‘P‘
+
convert
(
varchar
(10),@i),
‘M‘
);
SET
@i = @i + 1;
END
;
DECLARE
@i
int
;
SET
@i = 2000;
WHILE @i < 2500
BEGIN
Insert
Person
values
(
‘P‘
+
convert
(
varchar
(10),@i),
‘W‘
);
SET
@i = @i + 1;
END
;
GO
--8.查看表有哪些页
DBCC ind ( Test, [dbo.Person], -1)
|
12 |
--9.查看聚集索引页的数据 DBCC PAGE (Test,1,209, 3); |
12 |
--10.查看非聚集索引页的数据 DBCC PAGE (Test,1,189, 3); |
Summary of Index:
An index can have multiple page
Index is stored in a B + tree structure, where the information of the branch node is present in a page, and the leaf node is stored in the other page.
An instance of SQL Server's index structure