How to add an identifier column to an existing database table that has a large number of rows

I have a database table that has ~ 40,000,000 rows. I want to add an identifier column to this table. How to do it in a magazine-friendly form?

When I do the following:

ALTER TABLE table_1 ADD id INT IDENTITY 

it just fills the entire log space.

Is there a way to do this in a magazine-friendly way? The database is on SQL Server 2008.

Thanks, Mohan.

+6
source share
3 answers

In general, the process is likely to be much slower if it has more than the total amount of blocking, but if you only care about the size of the transaction log, you can try the following.

  • Add a column with a zero number without an identifier (change only metadata).
  • Enter the code to update it with unique consecutive integers. This will reduce the size of each individual transaction and reduce the size of the log (assuming a simple recovery model). My code below does this in batches of 100, I hope you have a working PC that you can use to pick up where you left off, and not re-checks that will last longer.
  • use ALTER TABLE ... ALTER COLUMN to mark a column as NOT NULL . This will require that the entire table is locked and scanned to check for changes, but does not require a lot of logs.
  • Use ALTER TABLE ... SWITCH to make the column an โ€‹โ€‹identifier. This change is for metadata only.

Code example below

 /*Set up test table with just one column*/ CREATE TABLE table_1 ( original_column INT ) INSERT INTO table_1 SELECT DISTINCT number FROM master..spt_values /*Step 1 */ ALTER TABLE table_1 ADD id INT NULL /*Step 2 */ DECLARE @Counter INT = 0 , @PrevCounter INT = -1 WHILE @PrevCounter <> @Counter BEGIN SET @PrevCounter = @Counter; WITH T AS ( SELECT TOP 100 * , ROW_NUMBER() OVER ( ORDER BY @@SPID ) + @Counter AS new_id FROM table_1 WHERE id IS NULL ) UPDATE T SET id = new_id SET @Counter = @Counter + @@ROWCOUNT END BEGIN TRY; BEGIN TRANSACTION ; /*Step 3 */ ALTER TABLE table_1 ALTER COLUMN id INT NOT NULL /*Step 4 */ DECLARE @TableScript NVARCHAR(MAX) = ' CREATE TABLE dbo.Destination( original_column INT, id INT IDENTITY(' + CAST(@Counter + 1 AS VARCHAR) + ',1) ) ALTER TABLE dbo.table_1 SWITCH TO dbo.Destination; ' EXEC(@TableScript) DROP TABLE table_1 ; EXECUTE sp_rename N'dbo.Destination', N'table_1', 'OBJECT' ; COMMIT TRANSACTION ; END TRY BEGIN CATCH IF XACT_STATE() <> 0 ROLLBACK TRANSACTION ; PRINT ERROR_MESSAGE() ; END CATCH ; 
+4
source

There are two ways to add an identity column to a table with existing data:

  • Create a new table with an identifier, copy the data into this new table, then cancel the existing table, and then rename the temp table.

  • Create a new column with id and release the existing column

Link: http://cavemansblog.wordpress.com/2009/04/02/sql-how-to-add-an-identity-column-to-a-table-with-data/

+1
source

I just did this with my table containing over 2700 rows. Go to the table design, add a new column, set it to not allow NULL, set the column as an identifier column in the column properties, and this should be done. I literally did it less than 5 minutes ago, and it worked for me. Please select an answer if this answers your question.

+1
source

All Articles