Quick one today:
Earlier, there was an interesting question that came up during a discussion. When we perform ‘ALTER INDEX ALL ON dbo.test REBUILD’ does it change the data COMPRESSION status of the table?
In short, no. It keeps it the way it was before the REBUILD started.
Since this is a REBUILD (without changing the definition), to reduce the fragmentation, my guess is it does not read the contents of the data (in detail). It will read it just enough to write to a different contiguous location. So, there is no un-compressing and re-compressing operation going on here.
But if we mention the index option “DATA_COMPRESSION = PAGE”, it will compress the data, no matter what the previous state.
--
-- Sample code to test
--
CREATE TABLE dbo.test (
, ID INT IDENTITY(1,1) PRIMARY KEY
, Name1 VARCHAR(50) DEFAULT CONVERT(VARCHAR, (RAND()*10000))
, Name2 VARCHAR(50) DEFAULT CONVERT(VARCHAR, (RAND()*10000))
, Name3 VARCHAR(50) DEFAULT CONVERT(VARCHAR, (RAND()*10000))
)
GO
--
-- Put some dummy data
--
INSERT INTO dbo.test DEFAULT VALUES
GO 1000
Now, lets check the compression status of the CLUSTERED index.
--
-- Check the compression status
--
SELECT OBJECT_NAME(object_id), data_compression_desc
FROM sys.partitions
WHERE object_id = OBJECT_ID('test')
AND index_id = 1
As you can see, the table data is not compressed.
Table Not Compressed
Now, when we REBUILD, the compression status does not change.
--
-- Now REBUILD without any INDEX OPTIONS.
--
ALTER INDEX ALL ON dbo.test REBUILD
GO
As part of REBUILD, once we add any changes to the definition of the index, the compression status changes.
--
-- Now REBUILD and test.
--
ALTER INDEX ALL ON dbo.test REBUILD
WITH (DATA_COMPRESSION = PAGE)
GO
Rebuild with COMPRESSION change
Hope this helps,
_Sqltimes
Read Full Post »