HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMajor

Move Primary Key to Filegroup (SQL Server 2012)

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
primary2012sqlfilegroupmoveserverkey

Problem

How can I move a clustered primary key to a new filegroup? I already have found a possible "algorithm" but it is horribly inefficient:

  • Drop non-clustered indexed (requires them to be resorted and rebuilt)



  • Drop clustered index (requires the whole table to be resorted)



  • Create new primary key constraint (huge sort operation)



  • Create all non-clustered indexes (sorting and writing required)



Is there a more efficient way? This is horribly inefficient and will take a long time as the table is 50GB in size on a weak server.

Isn't there a way to skip all of these and just do a rebuild on a new filegroup? That would not require any sorting of data.

Solution

CREATE UNIQUE CLUSTERED INDEX Your_PK_Name
    ON YourTable(YourColumnList)
WITH (DROP_EXISTING =  ON )
ON [YourOtherFileGroup]


This preserves the logical PK property despite it not being mentioned in the syntax.

Code Snippets

CREATE UNIQUE CLUSTERED INDEX Your_PK_Name
    ON YourTable(YourColumnList)
WITH (DROP_EXISTING =  ON )
ON [YourOtherFileGroup]

Context

StackExchange Database Administrators Q#28872, answer score: 30

Revisions (0)

No revisions yet.