简体   繁体   English

SQL Server 2012-对现有表进行分区

[英]SQL Server 2012 - Partitioning an exisiting table

We are in process of partitioning an existing table. 我们正在对现有表进行分区。 Let me share a brief about the background: 让我分享一下背景简介:

Issue Background: Have got few guidelines from here: https://dba.stackexchange.com/questions/48011/how-to-partition-an-existing-non-partitioned-table 问题背景:此处的指导原则很少: https : //dba.stackexchange.com/questions/48011/how-to-partition-an-existing-non-partitioned-table

It says, 它说,

  • Step 1: first create a partition function and partition scheme 步骤1:首先创建分区功能和分区方案
  • Steps 2 & 3 not applicable to my case 第2步和第3步不适用于我的情况
  • Step 4: If your table does not have a clustered index, then you can just create one on the right partition using the partition scheme. 步骤4:如果您的表没有聚集索引,则可以使用分区方案在正确的分区上创建一个索引。

Question: 题:

  • I've done with Step1. 我已经完成了Step1。 While performing step 4, is it mandatory to create a clustered index. 在执行步骤4时,必须创建聚簇索引。 If so, Why ? 如果是这样, 为什么

  • I have a datekey column(INT datatype)of the table, which would have plenty of rows for one particular datekey(sample-20150825). 我有一个表的datekey列(INT数据类型),其中一个特定的datekey(sample-20150825)会有很多行。 I'm planning to choose the characters '201508' as the partition key and all records for that month should flow in that partition. 我打算选择字符“ 201508”作为分区键,该月的所有记录都应在该分区中流动。 Is this possible to proceed with ? 这可能继续吗? If so, please help me with right directions. 如果是这样,请帮助我正确的方向。

Many Thanks. 非常感谢。 Lakshman. 拉克什曼。

I would probably go with something like this. 我可能会喜欢这样的东西。 I supposed that you insert datakey in acsending order without going back or any update on it. 我以为您以升序插入datakey而不返回或对其进行任何更新。 In this example, I assumed that your oldest datekey is in January 2015. 在此示例中,我假设您最早的日期键是在2015年1月。

Create a test table with no clustered index: 创建一个没有聚集索引的测试表:

create table dbo.test(id int identity(0, 1) primary key nonclustered, datekey int, data nchar(2000))
go
insert into test(datekey, data) values 
(20150125, ''), (20150120, ''), (20150118, ''), (20150118, ''), (20150118, '')
, (20150205, ''), (20150215, ''), (20150215, ''), (20150215, ''), (20150215, '')
, (20150305, ''), (20150315, '')

Create Filegroups and files: 创建文件组和文件:

Alter Database [Test] Add Filegroup [Part_201501]
Alter Database [Test] Add Filegroup [Part_201502]
Alter Database [Test] Add Filegroup [Part_201503]
Alter Database [Test] Add FILE ( NAME = N'Part_201501', FILENAME = N'...\Part_201501.ndf' , SIZE = 5120KB , FILEGROWTH = 1024KB ) TO Filegroup [Part_201501]
Alter Database [Test] Add FILE ( NAME = N'Part_201502', FILENAME = N'...\Part_201502.ndf' , SIZE = 5120KB , FILEGROWTH = 1024KB ) TO Filegroup [Part_201502]
Alter Database [Test] Add FILE ( NAME = N'Part_201503', FILENAME = N'...\Part_201503.ndf' , SIZE = 5120KB , FILEGROWTH = 1024KB ) TO Filegroup [Part_201503]

Create function starting with everything before 20150201 (meaning 01-2015): 创建从20150201之前的所有内容开始的函数(表示01-2015):

Create Partition Function DateKeyPartFunction (int)
as Range Right For Values (20150201, 20150301)

Note that I cannot partition by part of the datakey like 201501. This is why I partition by the first day of the following month. 请注意,我无法按数据键的一部分进行分区,例如201501。这就是为什么要在下个月的第一天进行分区的原因。 All datekey >= 20150201 and < 20150301 will be part of the Part_201502 partition. 所有日期键> = 20150201和<20150301将成为Part_201502分区的一部分。

Create Scheme: 创建方案:

Create Partition Scheme DateKeyPartScheme as Partition DateKeyPartFunction
To ([Part_201501], [Part_201502], [Part_201503])

Create a clustered index: 创建聚簇索引:

Create Clustered Index IDX_Part On dbo.Test(datekey) On DateKeyPartScheme(datekey);

If you have a clustered primary key. 如果您具有集群主键。 You have to replace it by a non clustered PK (+remove/add FKs). 您必须将其替换为非群集PK(+删除/添加FK)。 This won't change the types of your table. 这不会更改表格的类型。

Once you reach April, you only have to add a new Part_201504 filegroup and split the function on 20150401... 到达4月后,只需添加一个新的Part_201504文件组并在20150401上拆分该功能...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM