In SQL Server 2008 R2 I am exploring the use of partitioning tables.
We have a soft-delete scheme which is sometimes an IsDeleted bit NOT NULL
and sometimes a DateDeleted date NULL
I want to focus on the first case, the bit.
I am considering partitioning this table on the bit because the table is almost always queried and joined including this column.
How would I go about this process? Is it a bad idea?
I first tried to step through the wizard but the script it generated for me seemed awkward (multiple drops of constraints, creation of a new CI)
Best Answer
The following is a sample script I just created that shows how to create a table that contains an
IsDeleted
Field.After the table is created and filled with 10,000 rows, I add a partitioning function and scheme, and some filegroups and files to contain the partitions. In real life, you would want to create these filegroups and files on dedicated LUNs to take advantage of extra I/Os and piecemeal restore.
I then alter the table so it makes use of the partitioning function, thereby moving the existing rows into the appropriate filegroup. Since you cannot perform an 'ALTER TABLE...ALTER CONSTRAINT` to alter the primary key to use the partitioning function, you are forced to drop the primary key and recreate it with the new partitioning function. This does mean the clustered index will be turned into a heap until you can create the new clustered index with the partitioning function.
I just ran this and got the following results:
As you can see, the table had an almost perfect distribution of rows in the
IsDeleted = 0
andIsDeleted = 1
state. Updating a row from 0 to 1 resulted in the row moving to the other partition.I hope this helps show how to alter a table to take advantage of partitioning on an
IsDeleted
soft-deleteBIT
field.When I ran the wizard to partition this table, with the option to script to a new query window, I got this code:
As far as I can tell, this code looks like it doesn't work. It drops the PK, then recreates it, without using the partitioning function. It then creates a clustered index, using the partitioning function, but then immediately drops it!