See this exampleCREATE TABLE tSample ( i INT )CREATE UNIQUE CLUSTERED INDEX IX_Sample ON tSample (i)INSERT tSample ( i )SELECT NumberFROM TallyNumbersWHERE Number < 10000ORDER BY NEWID()DBCC SHOWCONTIG(tSample) WITH FASTTRUNCATE TABLE tSampleINSERT tSample ( i )SELECT NumberFROM TallyNumbersWHERE Number < 10000ORDER BY NumberDBCC SHOWCONTIG(tSample) WITH FASTALTER INDEX IX_Sample ON tSample REORGANIZEDBCC SHOWCONTIG(tSample) WITH FASTALTER INDEX IX_Sample ON tSample REBUILDDBCC SHOWCONTIG(tSample) WITH FASTDROP TABLE tSample
The output isDBCC SHOWCONTIG scanning 'tSample' table...Table: 'tSample' (1500584434); index ID: 1, database ID: 6TABLE level scan performed.- Pages Scanned................................: 17- Extent Switches..............................: 3- Scan Density [Best Count:Actual Count].......: 75.00% [3:4]- Logical Scan Fragmentation ..................: 11.76%DBCC execution completed. If DBCC printed error messages, contact your system administrator.DBCC SHOWCONTIG scanning 'tSample' table...Table: 'tSample' (1500584434); index ID: 1, database ID: 6TABLE level scan performed.- Pages Scanned................................: 17- Extent Switches..............................: 4- Scan Density [Best Count:Actual Count].......: 60.00% [3:5]- Logical Scan Fragmentation ..................: 23.53%DBCC execution completed. If DBCC printed error messages, contact your system administrator.DBCC SHOWCONTIG scanning 'tSample' table...Table: 'tSample' (1500584434); index ID: 1, database ID: 6TABLE level scan performed.- Pages Scanned................................: 17- Extent Switches..............................: 4- Scan Density [Best Count:Actual Count].......: 60.00% [3:5]- Logical Scan Fragmentation ..................: 23.53%DBCC execution completed. If DBCC printed error messages, contact your system administrator.DBCC SHOWCONTIG scanning 'tSample' table...Table: 'tSample' (1500584434); index ID: 1, database ID: 6TABLE level scan performed.- Pages Scanned................................: 17- Extent Switches..............................: 7- Scan Density [Best Count:Actual Count].......: 37.50% [3:8]- Logical Scan Fragmentation ..................: 35.29%DBCC execution completed. If DBCC printed error messages, contact your system administrator.
Seems rather strange that reorganize is more efficient than rebuild, and that insert order by newid() creates less fragmentation than order by number.And that order by number creates fragmentation at all.
E 12°55'05.63"N 56°04'39.26"