Submitted byCategory
Review Cycle
.
Public
Joachim Mutter/sysarc
on 10/04/2019 at 02:43 PM
SQLServer

Partitioning (Public)

There are a lot of articles in the web regarding partitioning of a SQL-Server database table, but none of them describes a full approach how to implement partitioning.
Due to this I had to get all bits and pieces togehther and with some help of SSmS I was able to create a more or less automated approach for creating partitions on fact tables which could be done in a schmeatic SSiS package to load data into a datawarehouse.

There are a few steps for creating a partition on a table:

· You need a column which could be used for partitioning, which is regulary a datetime columns or a derived one from such a column by converting the datetime to a bigint via

· Create a partition function
· Create a partition scheme based on the partition function
· You need a index based on this column, which is regulary a "CLUSTERED COLUMNSTORE INDEX" based on the created partition scheme and the partition column
(if you check the code genereated by SSmS for a new partition)

· Afterwards you need to define the boundaries for the partitions, (i.e. by given amount of boundaries) and switching the partitions to an empty table with the same structure than the main table (Afterwards all partitions are empty)
By

· You should empty the partition by an empty copy (Buffer) of the main table
(
for this the Buffer table must be also partitoned)


· Incremental loads will be done with the same command, but with a filled table
(i.e.filled by a SSiS task)
(
for this the Switch table must be also partitoned and the target partiton MUST be empty)


Due to the described scheme, we can implement a handy amount of stored procedures, which could by called to dynamically create
· A Filegroup
· A partition with a partition scheme and a partition function
· the needed boundaries.

In principle it is a 4 step approach

1. Prepare the meta data table which defines the month to be loaded
This is normally done where the data is loaded into stage

2. Create / Prepare / Clean the partition
by the meta data table

3. Load the data into the switch table

4. Transfer the data from switch into Main table
by the meta data table
(To make the code robust, we have to check here if the partition is empty, if not clean it like done in step 2)



This is the code to test this

/* Partitioning Test */ /* Truncate Switch Table */ /* 1. Fill the metadata table */ /* 2. Create/Clean the partitions */ /* 3. Load data */ /* 4. Switch Table to Main */ /* 5. Show some Log informations */
Create FilegroupCreate PartitionPrepare Partitioning MonthGet partitionIDMove table Switch to MainLogging
sp_PartitionCreateNewFilegroup.sql