SQL NOT--PARTITION

USE DB3
GO
--Create partition function
CREATE PARTITION FUNCTION PF_Test(INT)
AS RANGE LEFT FOR VALUES (1000,2000)
GO
--Create patition schema
CREATE PARTITION SCHEME PS_Test
AS PARTITION PF_Test TO (FG1,FG2,FG3)
GO
--Crate partition table
CREATE TABLE Users
(
    UserId INT PRIMARY KEY,
    UserName NVARCHAR(200)
)ON PS_Test(UserId);
GO
--insert data
WITH T AS
(
    SELECT ROW_NUMBER()OVER(ORDER BY O.OBJECT_ID) AS RowNum FROM sys.all_columns O
)
INSERT INTO dbo.Users(UserId,UserName)
SELECT RowNum AS UserId,'User'+CAST(RowNum AS NVARCHAR(20))AS UserName FROM T
WHERE T.RowNum<3000

--检查数据分布
SELECT p.partition_id,p.rows AS RowNum FROM sys.partitions P
INNER JOIN sys.objects O ON P.object_id=O.object_id
WHERE O.name='Users'

--选择新分区指向的文件组
ALTER PARTITION SCHEME PS_Test NEXT USED 'FG1'
--拆分数据分区
ALTER PARTITION FUNCTION PF_Test()
SPLIT RANGE(3000)
--插入数据
WITH T AS
(
    SELECT ROW_NUMBER()OVER(ORDER BY O.OBJECT_ID) AS RowNum FROM sys.all_columns O
)
INSERT INTO dbo.Users(UserId,UserName)
SELECT RowNum AS UserId,'User'+CAST(RowNum AS NVARCHAR(20))AS UserName FROM T
WHERE T.RowNum>3000 AND T.RowNum<4000

--合并分区
ALTER PARTITION FUNCTION PF_Test()
MERGE RANGE(1000)
--可以使用SWITCH把数据转出或转入

 

posted on 2012-10-17 18:37  笑东风  阅读(323)  评论(0编辑  收藏  举报

导航