I have a database on SQL Server 2014 SP1 CU5 with .mdf that is about 1.45 TB. I found one table that is 691 GB of that .mdf. The table has 1.7 billion rows. There is one primary filegroup and the Fusion IO card is running out of space. I am currently running a script where I dropped the PRIMARY KEY CLUSTERED constraint on the table and I am recreating it on the new filegroup on a new SAN drive that was 1.2 TB in size. The script has been going for many hours and for some reason the table is consuming almost the entire 1.2 TB drive. Why is it using more space as I move it to a new filegroup? Below is what I obtained from the internet to get me the table sizes for all the tables I eventually wanted to move. SET NOCOUNT ON; --DECLARE VARIABLESDECLARE @max INT, @min INT, @table_name NVARCHAR(256), @table_schema NVARCHAR(256), @sql NVARCHAR(4000); --DECLARE TABLE VARIABLEDECLARE @table TABLE(id INT IDENTITY(1, 1) PRIMARY KEY, table_name NVARCHAR(256), table_schema NVARCHAR(256)); --CREATE TEMP TABLE THAT STORES INFORMATION FROM SP_SPACEUSEDIF( SELECT OBJECT_ID('tempdb..#results')) IS NOT NULL BEGIN DROP TABLE #results; END;CREATE TABLE #results([table_schema] [NVARCHAR](256) NULL, [table_name] [NVARCHAR](256) NULL, [table_rows] [INT] NULL, [reserved_space] [NVARCHAR](55) NULL, [data_space] [NVARCHAR](55) NULL, [index_space] [NVARCHAR](55) NULL, [unused_space] [NVARCHAR](55) NULL); --LOOP THROUGH STATISTICS FOR EACH TABLEINSERT INTO @table(table_schema, table_name) SELECT table_schema, table_name FROM information_schema.tables WHERE table_name LIKE '%[_]Archive%'; --('dbo.MortgageLoanTerms_Archive')--('HumanResources.Employee','Production.Product', 'Purchasing.Vendor') --INSERT TABLE NAMES TO MONITORSELECT @min = 1, @max =( SELECT MAX(id) FROM @table);WHILE @min < @max + 1 BEGIN SELECT @table_name = table_name, @table_schema = table_schema FROM @table WHERE id = @min; --DYNAMIC SQL SELECT @sql = 'EXEC sp_spaceused ''['+@table_schema+'].['+@table_name+']'''; --INSERT RESULTS FROM SP_SPACEUSED TO TEMP TABLE INSERT INTO #results (table_name, table_rows, reserved_space, data_space, index_space, unused_space ) EXEC (@sql); --UPDATE SCHEMA NAME UPDATE #results SET table_schema = @table_schema WHERE table_name = @table_name; SELECT @min = @min + 1; END; --REMOVE "KB" FROM RESULTS FOR REPORTING (GRAPH) PURPOSESUPDATE #results SET data_space = SUBSTRING(data_space, 1, (LEN(data_space)-3));UPDATE #results SET reserved_space = SUBSTRING(reserved_space, 1, (LEN(reserved_space)-3));UPDATE #results SET index_space = SUBSTRING(index_space, 1, (LEN(index_space)-3));UPDATE #results SET unused_space = SUBSTRING(unused_space, 1, (LEN(unused_space)-3)); --INSERT RESULTS INTO TABLESIZEGROWTH--INSERT INTO TableSizeGrowth (table_schema, table_name, table_rows, reserved_space, data_space, index_space, unused_space)SELECT [table_schema], [table_name], [table_rows], CAST([reserved_space] AS FLOAT) / 1000 AS reserved_spaceMB, CAST([data_space] AS FLOAT) / 1000 AS data_spaceMB, CAST([index_space] AS FLOAT) / 1000 AS index_spaceMB, CAST([unused_space] AS FLOAT) / 1000 AS unused_spaceMBFROM #resultsORDER BY data_spaceMB DESC;SELECT SUM(CAST([reserved_space] AS FLOAT) / 1000) AS reserved_spaceMBFROM #results;--ORDER BY reserved_spaceMB) DESCDROP TABLE #results;I used the below to actually move the table.DROP TRIGGER [dbo].[MortgageLoanTerms_Archive_Trigger]; --There is an AFTER trigger on this table that writes UPDATEs or DELETEs to the _Archive table.GO--Create File and FileGroupUSE MyDatabase;GOALTER DATABASE [MyDatabase] ADD FILEGROUP ArchiveFG1;GOALTER DATABASE [MyDatabase] ADD FILE( NAME = ArchiveData1, FILENAME = 'F:\Data\ArchiveFG1.ndf', SIZE = 700000 MB --Current size of this one table is about 693 GB Allocated space on the SAN drive is 1.2 TB, FILEGROWTH = 1024 MB) TO FILEGROUP ArchiveFG1;GO--Move the Clustered Index to the new filegroup, which moves the table since the clustered index is the table at the leaf level/****** Object: Index [PK_dbo.MortgageLoanTermsArchive] Script Date: 3/11/2016 10:45:47 AM ******/ALTER TABLE [dbo].[MortgageLoanTerms_Archive] DROP CONSTRAINT [PK_dbo.MortgageLoanTermsArchive];GO/****** Object: Index [PK_dbo.MortgageLoanTermsArchive] Script Date: 3/11/2016 10:45:47 AM ******/ALTER TABLE [dbo].[MortgageLoanTerms_Archive]ADD CONSTRAINT [PK_dbo.MortgageLoanTermsArchive] PRIMARY KEY CLUSTERED([MortgageLoanTermsArchiveId] ASC) WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON ArchiveFG1;GO--Recreate the Trigger on MortgageLoanTerms
↧