Technical Article

Automated Script to Partition Tables

,

 

I work in a very big environment and all of our Fact Tables are partitioned on a day-day basis.

The date partition is from our dimension process. Hence, the fact tables are partitioned on the date dimension.

 

The below script gives you the ease of generating an automatic script which prints the filegroup, file for the partition and also prints the Alter Partiton scheme to use the filegroup and also the splits the partiton function.

 

Our SAN is carved with smaller chunks for optimal performance and i follow a round-robin algorithm while partitoning. So, that is the reason for the drive names are named with Mount points and practically each one used for a specific partition.

 

This gives an ease for extending partitions on a day-day basis.

 

I hope this script will help a lot of folks out there...working in a warehouse!!!

 

****************************************************************************

 

Declare @drives table (Drive Varchar(8) Null, srlno tinyint identity(1,1));
Declare @cnt smallint, @srlno smallint, @DName Varchar(8), @FileGroup Varchar(30);
Declare @Stmt Varchar(500), @Tname Varchar(30), @SDay SmallInt, @Tname1 Varchar(30); 
Declare @Tname2 Varchar(30), @Tname3 Varchar(30), @Tname4 Varchar(30);
Declare @Filegroup1 Varchar(30), @Filegroup2 Varchar(30), @Filegroup3 Varchar(30), @Filegroup4 Varchar(30)

 


Insert into @drives (Drive) Values('Q:\Q001\');
Insert into @drives (Drive) Values('Q:\Q002\');
Insert into @drives (Drive) Values('Q:\Q003\');
Insert into @drives (Drive) Values('Q:\Q004\');
Insert into @drives (Drive) Values('Q:\Q005\');
Insert into @drives (Drive) Values('Q:\Q006\');
Insert into @drives (Drive) Values('Q:\Q007\');
Insert into @drives (Drive) Values('Q:\Q008\');
Insert into @drives (Drive) Values('Q:\Q009\');
Insert into @drives (Drive) Values('Q:\Q010\');
Insert into @drives (Drive) Values('Q:\Q011\');



Select @srlno = 1, @cnt = count(*) from @drives where Drive is not null;

Set @Tname = 'Table1';


 

Set @SDay = 1259;

While (@srlno <= @cnt)
Begin
 Set @FileGroup = @Tname + '_DAY'+ Convert(Varchar,@SDay) 

 
 
 Set @Stmt = 'Alter Database DatabaseName Add Filegroup [FG_' + @FileGroup +'];' + char(13) 
 Print @Stmt
 Select @DName = Drive from @Drives where srlno = @srlno
 Set @Stmt = 'Alter Database DatabaseName Add File (Name = [F_' + @FileGroup + '],' + char(13) + 'Filename = ''' + @DName + 'MSSQL\DATA\F_' + @FileGroup + '.ndf''' + ',' + char(13) + 'Size = 100 MB, Maxsize = 10240 MB, Filegrowth = 200 MB ) ' + char(13) + 'To FileGroup [FG_' + @FileGroup + '];' + char(13) 
 Print @Stmt;
 
 Set @Stmt = 'Alter Partition Scheme [PS] Next Used [FG_' + @FileGroup + '];' + char(13) 
 Print @Stmt;
 
 
 Set @Stmt = 'Alter Partition Function [PFN]() Split Range (' + Convert(Varchar,@SDay) + ');' + char(13) 
 Print @Stmt;

Set @Stmt = '--------------------------------------------------';
 Print @Stmt;

 

Set @srlno = @srlno + 1;
 Set @SDay = @SDay + 7;
End
Go

 

******************************************************************************

 

Modifications to the script are welcome!

Rate

4.5 (2)

You rated this post out of 5. Change rating

Share

Share

Rate

4.5 (2)

You rated this post out of 5. Change rating