Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting int primary key to bigint in Sql Server

We have a production table with 770 million rows and change. We want(/need?) to change the Primary ID column from int to bigint to allow for future growth (and to avoid the sudden stop when the 32bit integer space is exhausted)

Experiments in DEV have shown that this is not as simple as altering the column as we would need to drop the index and then re-create it. So far in DEV (which is a bit humbler than PROD) the dropping of the index has not finished after 1 and a half hours. This table is hit 24/7 and having it offline for such a long time is not an option.

Has anyone else had to deal with a similar situation? How did you get it done?

Are there alternatives?

Edit: Additional Info:

  • The Primary key is clustered.
like image 978
My Other Me Avatar asked Mar 28 '11 15:03

My Other Me


1 Answers

You could attempt a staged approach.

  1. Create a new bigint column
  2. Create an insert trigger to keep new entries in sync with the 2 columns
  3. Execute an update to populate all the empty values in the bigint column with the converted value
  4. Change the primary index on the table from your old id column to the new one
  5. Point any FK's and queries to use the new column
  6. Change the new column to become your identity column and remove the insert trigger from #2
  7. Delete the old ID column

You should end up spreading the pain out over these 7 steps instead of hitting it all at once.

like image 51
Rich Hill Avatar answered Sep 21 '22 01:09

Rich Hill