/*
建立结构如下的分区表:
Userid int --用户ID
reason int --变更原因
money int --财富
updatetime datetime --更新时间
*/
/*
按照更新时间分区建立分区函数如下
*/
create partition function ChangeLogPF_updatetime (datetime)
as range right for values(
'20030201','20030301','20030401',
'20030501','20030601','20030701',
'20030801','20030901', '20031001',
'20031101','20031201');
/*
按照更新时间分区建立分区
*/
create partition scheme ChangeLogPS_updatetime
as partition ChangeLogPF_updatetime
all to([primary]);
/*
跟据分区函数和分区方案建立分区表
*/
Create table MOney_change_log(
userid int,
reason int,
money int,
updatetime datetime
)on ChangeLogPS_updatetime(updatetime)
/*
跟据分区函数和分区方案建立分区索引
*/
Create index idx_userid_reason on Money_Change_Log(userid,reason)
on ChangeLogPs_updatetime(updatetime)
/*
插入测试数据
userid reason money updatetime
1000 1 100 2003-01-01 11:00:00.000
1000 1 200 2003-01-31 13:23:00.000
1000 1 -50 2003-02-01 23:59:59.000
1000 1 -50 2003-03-01 00:00:00.000
*/
/*
查询各分区中的行的数量
*/
select $partition.ChangeLogPF_updatetime(updatetime),COUNT(*)
from MOney_change_log
group by $partition.ChangeLogPF_updatetime(updatetime)
/*
合并分区
*/
Alter partition ChangeLogPF_Updatetime() Marge Range('20030201')
说明:删除分区'20030201'并把数据移入下一个分区中
/*
新建分区
*/
Alter Partition SCHEM ChangeLogPS_updatetime NEXT USED [primary]
Alter partition ChangeLogPF_Updatetime() SPLIT Range('20040101')
12-21
83万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
03-11