Sql 群集也对数据进行物理排序和非群集索引排序?

Sql 群集也对数据进行物理排序和非群集索引排序?,sql,sql-server,indexing,clustered-index,non-clustered-index,Sql,Sql Server,Indexing,Clustered Index,Non Clustered Index,那么,聚集索引和非聚集索引都会在数据创建后立即对其进行物理排序 按照我的说法,只读聚集索引进行物理排序?非聚集索引不会像聚集索引那样对磁盘上的数据进行排序 但是查询优化程序生成一个查询计划来扫描这个非聚集索引,它按照索引的顺序提供数据。这是因为索引与表完全匹配:表和索引中都有一列。因此,它使用索引,并对数据进行明显的排序 如果您添加了更多的列,使得索引对于完整表扫描没有用处,那么将生成一个扫描实际堆数据的查询计划。未使用索引 CREATE table comp_tb ( a tinyint )

那么,聚集索引和非聚集索引都会在数据创建后立即对其进行物理排序


按照我的说法,只读聚集索引进行物理排序?

非聚集索引不会像聚集索引那样对磁盘上的数据进行排序

但是查询优化程序生成一个查询计划来扫描这个非聚集索引,它按照索引的顺序提供数据。这是因为索引与表完全匹配:表和索引中都有一列。因此,它使用索引,并对数据进行明显的排序

如果您添加了更多的列,使得索引对于完整表扫描没有用处,那么将生成一个扫描实际堆数据的查询计划。未使用索引

CREATE table comp_tb 
(
a tinyint
)


insert into comp_tb values('4')
insert into comp_tb values('1')
insert into comp_tb values('5')
insert into comp_tb values('6')
insert into comp_tb values('10')
insert into comp_tb values('3')
insert into comp_tb values('2')
insert into comp_tb values('8')


SELECT * from comp_tb ct --as order of insert 

create NONCLUSTERED INDEX NCI_a ON comp_tb(a)

SELECT * from comp_tb ct --sorts in acending order

drop INDEX NCI_a ON comp_tb

create CLUSTERED INDEX CI_a ON comp_tb(a)

SELECT * from comp_tb ct -- sorts in acending order

drop INDEX CI_a ON comp_tb

在物理上,表数据仅按聚集索引排列。根据查询计划使用不同的索引。在使用非聚集索引的情况下,数据不是从表中检索的,因此可以看到数据是有序的

CREATE table #comp_tb 
(
a tinyint,
payload1 char(3000) NOT NULL,
payload2 varchar(100) NOT NULL,
)

insert into #comp_tb values('4', '4' /*padded*/, '44444444444')
insert into #comp_tb values('1', '1' /*padded*/, '1111111111111111111111111111')
insert into #comp_tb values('5', '5' /*padded*/, '55')
insert into #comp_tb values('6', '6' /*padded*/, '666666666666666666666666666666666666')
insert into #comp_tb values('10', '10' /*padded*/, 'a')
insert into #comp_tb values('3', '3' /*padded*/, '3333333')
insert into #comp_tb values('2', '2' /*padded*/, REPLICATE('2', 50))
insert into #comp_tb values('8', '8' /*padded*/, '8888888888888888888888')

SELECT * from #comp_tb ct --as order of insert 

create NONCLUSTERED INDEX NCI_a ON #comp_tb(a DESC)

SELECT * from #comp_tb ct --as order of insert 

drop INDEX NCI_a ON #comp_tb