SQLServer index fragmentation will affect the non-clustered index do after rebuilding the clustered index

 

Morning (20,171,011) see qq group sent a link Yunqi General Assembly, point looked into sqlserver special, just a Q & A session
someone asked a question, can not remember the exact words,
about the meaning (of his own think) that is: "SQLServer will affect after rebuilding the clustered index fragmentation non-clustered index, non-clustered indexes also incidentally rebuild"
I think probably he himself believes that "after rebuilding the clustered index will affect the index of non-clustered index fragmentation "
asker exchange views with the experts, after a start asking questions also withdraw a few heap table RID, clustered table key value her.
Experts say the two are not the beginning of a relationship (after rebuilding the clustered index does not affect the non-clustered index fragmentation index), it may be a bit nervous after being asked back, changed to say that did not pay attention to this issue.

 

First throw Conclusion: For clustered index, rebuilding the clustered index after the index fragmentation will not affect the non-clustered index, rebuilding the clustered index is no relationship between with the non-clustered index fragmentation, do not take Ga.
These problems, in fact, try to test themselves not to clear it?

 

After rebuilding the clustered index, any impact on the non-clustered index

First of all, for the time being to not pull the aggregate table heap table Han, and directly say clustered table,
non-clustered index leaf-level direct storage is a clustered index key value in rebuilding the clustered index (or restructuring) before and after the non-clustered index stored corresponding to the key value is unchanged
after rebuilding the clustered index, the house where data is stored may vary, it will affect the physical storage and fragmentation clustered index
but for the non-clustered index, the nonclustered indexes store clustered index key value corresponds to the same,
that the non-clustered index is rebuilt with the clustered index fragmentation or not there is a relationship between hair.
As I have cell phone records of a person's phone number, I just dial the phone will be able to find him, I though he was going to Beijing to work or go to Nanjing on a business trip, with his people in particular where (rebuilding clustered index, the physical location changes ) hairy relations.

If these problems are not sure, test results came out, ah, I do not think there's any doubt.

 

Test, test table TestFragment in, Id1 field is uniqueidentifier, with the establishment of the clustered index,
use of randomness uniqueidentifier, after large quantities of data written to its fragmentation becomes large
contrast, Id2 field type INT, in increasing the value of the write data, after writing large quantities of data in its index fragmentation will be very small
and then reproduce the index on the Id1, Id2 index fragmentation on the observation because the index will not rebuild on the Id1 varies

Copy the code
create table TestFragment
(
    Id1 uniqueidentifier,
    Id2 int,
    OtherCol2 varchar(50)
)
go


create unique clustered index IDX_Id1 on TestFragment(Id1);
go

create unique index IDX_Id2 on TestFragment(Id2);
go

begin tran
    declare @i int = 0
    while @i<1000000
    begin
        insert into TestFragment values(NEWID(),@i+1,NEWID());
        set @i = @i+1
    end
commit
go
Copy the code

Observed debris on the index after writing two 100W data,

For aggregation index (the index IDX_Id1 Id1):
Clearly, aggregation index (because uniqueidentifier type of field),
which is high avg_fragmentation_in_percent (99.2557236469541), while the lower avg_page_space_used_in_percent (68.9408574252533)
for non-clustered index (the index IDX_Id2 Id1 ):
Id2 index because it is increasing its avg_fragmentation_in_percent low (0.528606965174129), that is a very low degree of fragmentation

Here harm no matter the degree of fragmentation clustered index and non-clustered index, where the focus "would be if non-clustered index after rebuilding the clustered index fragmentation affect"

After rebuilding the clustered index, the index fragmentation re-observe here to see Screenshot sys.dm_db_index_physical_stats query results
can be very clear that, after rebuilding the clustered index, a clustered index fragmentation itself has undergone great changes in the basic fragments completely eliminated (avg_fragmentation_in_percent0. 0116986429574169),
but the fragmentation of non-clustered index at any point has not changed.


Not difficult to understand in theory:
clustered index and non-clustered indexes are two completely separate physical storage structure (of course, can be said to be the logical storage structure)
whose only contact is the non-clustered index node B leaves the store clustered index key value
key value stored in its clustered index is not its physical location, gathered position change index or the data itself and not because of changes in key values
therefore said reconstruction or reorganization will not affect the clustered index fragmentation non-clustered index

 

Eliminate debris heap table

对于堆表的索引碎片消除,也是可以通过alter table xxx rebuild重建的,
当然也有一种很挫的做法就不想提了(fix heap fragmentation by creating and dropping a clustered index.)
记住这是一种很挫的做法,可能是SQL Server 2008之前的版本中,alter table xxx rebuild语法被支持之前的无奈之举,
这里暂不表述这种做法。
对于堆表,alter table xxx rebuild可以通过重建表来消除碎片,但其功能不限于次,还会重建堆表上的非聚集索引

测试示例

Copy the code
create table TestHeapFragment
(
    Id1 uniqueidentifier,
    Id2 int,
    OtherCol2 varchar(50)
)
go

create unique index IDX_Id1 on TestHeapFragment(Id1);
go

create unique index IDX_Id3 on TestHeapFragment(OtherCol2);
go

begin tran
    declare @i int = 0
    while @i<1000000
    begin
        insert into TestHeapFragment values(NEWID(),@i+1,NEWID());
        set @i = @i+1
    end
commit
go
Copy the code

通过alter table xxx rebuild对堆表重建,发现非聚集索引也会因为堆表的重建而发生索引重建。

 

If you think you can use ALTER TABLE … REBUILD to fix heap fragmentation, you can, but it causes all the nonclustered indexes to be rebuilt as the heap record locations obviously change.

Temporarily do not know the specific implementation process alter table xxx rebuild, but from a variety of performance, he did also rebuilt the non-clustered index in the process of rebuilding the table.
But "the process of reconstruction of the table indeed rebuild the non-clustered index" may be considered "so that the non-clustered index to become more good, not bad,"
to note here is the alter table xxx rebuild to rebuild the heap table is not right right non-clustered index on the table side effects

Emphasize that,
for the non-clustered table, alter table xxx rebuild will rebuild all non-clustered indexes
for aggregate table, alter table xxx rebuild will rebuild the clustered index, but does not rebuild nonclustered indexes

 

 

To be honest, the questioner said, "after rebuilding the clustered index will affect the non-clustered index fragmentation" This is the first time I heard my point of view,
if you really understand the index, it should know that both (rebuild clustered index and non-clustered not between the index fragmentation) necessarily related
to the inexplicable conclusion, in the end is still hearsay was true, why not try it yourself?

 

Turn: https://www.cnblogs.com/wy123/p/7650215.html

Guess you like

Origin www.cnblogs.com/VicLiu/p/11654623.html
Recommended