Microsoft - How To Implement Automatic Sliding Window in Partitioned Table
Microsoft - How To Implement Automatic Sliding Window in Partitioned Table
90)
Summary: This article shows how to implement an automatic sliding window in a partitioned table on
Microsoft SQL Server 2005. (8 printed pages)
Horizontal partitioning is a new feature of SQL Server 2005. When trying to use horizontal partitioning,
a common problem is how to make it fully automated without the need of manual intervention.
The sliding sample in the AdventureWorks sample database is hard-coded. The objective of this
document is to show how to implement it in a real world environment. The sample has a 60-day sliding
window with 60 partitions.
The idea is to create automated stored procedures with no parameters needed so that they can be
scheduled to run through SQL Agent on a daily basis. Two stored procedures are needed: one for the
right side and another for the left side. The first one will create a new partition for the next day. The
second one will kill the most left partition.
The first problem encountered in transforming the script is the ADD CONSTRAINT statement used to
establish a left boundary on the source table. In order to automate the script, it is necessary to have all
dates based on the partition 1 date. In earlier versions of SQL Server, this was accomplished by using
ALTER TABLE statements; however, this will not work because ALTER TABLE doesn't accept variables
but just scalar values. There are two ways to work around this:
Write a C# assembly that mounts the command and issues the statement in an ad-hoc way.
Move the second partition and not the first.
This article follows the second approach, keeping the first partition only to set a left boundary.
The second problem is that you can't issue any table command such TRUNCATE TABLE directly on the
partition. If you have to copy the data or even delete it, you have to use a second auxiliary table. This
table must have the same structure as the original one.
The following schema shows how the left part works.
--**************************************************************************************
--
-- Summary: Range partition tables CDR and CDR_AUX
-- Creates partition function pfDaily on the End_Date column of the CDR table,
so that each partition contains one day of data.
-- Creates partition scheme pfDaily to map the partitions to filegroups.
All partitions reside on the same filegroup (Primary).
-- Drops and re-creates the CDR table specifying the partition scheme pfDaily
as the location for the table.
-- Creates partition function pfDaily_Aux on the End_Date column of the CDR_AUX table.
-- Creates partition scheme pfDaily_Aux to map the partitions to filegroups.
All partitions reside on the same filegroup (Primary).
-- Drops and re-creates the CDR_AUX table specifying the partition scheme pfDaily_Aux
as the location for the table.
--
-- SQL Server Version: 9.00
--**************************************************************************************
use AdventureWorks
go
--To drop all to re[eat the test remove the following lines
--truncate table cdr
--drop table cdr
--drop pARTITION SCHEME pfDaily
--drop partition function pfDaily;
--truncate table cdr_aux
--drop table cdr_aux
--drop pARTITION SCHEME pfDaily_aux
--drop partition function pfDaily_aux;
create partition function pfDaily (datetime)
as RANGE RIGHT for values(
'2005-05-07', '2005-05-08', '2005-05-09', '2005-05-10', '2005-05-11', '2005-05-12',
'2005-05-13', '2005-05-14',
'2005-05-15', '2005-05-16', '2005-05-17', '2005-05-18', '2005-05-19', '2005-05-20',
'2005-05-21', '2005-05-22',
'2005-05-23', '2005-05-24', '2005-05-25', '2005-05-26', '2005-05-27', '2005-05-28',
'2005-05-29', '2005-05-30',
'2005-05-31', '2005-06-01', '2005-06-02', '2005-06-03', '2005-06-04', '2005-06-05',
'2005-06-06', '2005-06-07',
'2005-06-08', '2005-06-09', '2005-06-10', '2005-06-11', '2005-06-12', '2005-06-13',
'2005-06-14', '2005-06-15',
'2005-06-16', '2005-06-17', '2005-06-18', '2005-06-19', '2005-06-20', '2005-06-21',
'2005-06-22', '2005-06-23',
'2005-06-24', '2005-06-25', '2005-06-26', '2005-06-27', '2005-06-28', '2005-06-29',
'2005-06-30', '2005-07-01',
'2005-07-02', '2005-07-03', '2005-07-04')
go
--**************************************************************************************
--
-- Summary: Managing a Range Partitioned Table
-- Creates a new partition at Right
-- Add a new partition on the end of table CDR for the next day
-- There is no need to pass any parameter. The routine reads CDR
-- metadata to discover right boundary.
--
--**************************************************************************************
USE AdventureWorks;
GO
GO
--**************************************************************************************
--
-- Summary: Managing a Range Partitioned Table
-- Delete data on 2nd most left partition.
-- It means that the most left partition will always stay there
-- to guarantee the size of the second one. This one will be
-- moved. The most left partition will be empty.
--
--**************************************************************************************
USE AdventureWorks;
GO
-- STEP 1
-- Add a new partition to table CDR_Aux to hold the
-- Data from 2nd Left Partition of CDR.
-- STEP 3
-- Merge the 1st and 2nd partitions of table CDR.
-- STEP 4
-- Merge the partition of table CDR_AUX
-- with the first partition.
GO
To see how it works, just call the stored procedures and look at the schema.