1.创建的代码如下:

CREATE PARTITION FUNCTION [01_SubjectiveScoreInfoPartitionFunction](NVARCHAR(26))
AS RANGE LEFT FOR VALUES
(
N'',
N'',
N'',...
) CREATE PARTITION SCHEME [01_SubjectiveScoreInfoPartitionSchema]
AS PARTITION [01_SubjectiveScoreInfoPartitionFunction] ALL TO ([PRIMARY]) CREATE CLUSTERED INDEX [IX_01_SubjectiveScoreInfo_Clustered] ON [01_SubjectiveScoreInfo] (QuestionGroupCode)
WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
ON [01_SubjectiveScoreInfoPartitionSchema](QuestionGroupCode)

2.增加分区值的代码如下:

ALTER PARTITION FUNCTION [01_SubjectiveScoreInfoPartitionFunction] ()
SPLIT RANGE ('');

3.查询当前分区情况:

SELECT ps.name,pf.name,boundary_id,value
FROM sys.partition_schemes ps
INNER JOIN sys.partition_functions pf ON pf.function_id=ps.function_id
INNER JOIN sys.partition_range_values prf ON pf.function_id=prf.function_id
WHERE pf.name = '01_SubjectiveScoreInfoPartitionFunction'

4.创建单一文件组分区完整示例代码如下:

CREATE PROCEDURE [dbo].[spPartitionSubjectiveScoreInfo]
(
@CourseID AS NVARCHAR(2)
)
AS
BEGIN
BEGIN TRAN
BEGIN TRY
DECLARE @sql NVARCHAR(MAX)
DECLARE @QuestionGroupCodeList NVARCHAR(MAX)
SET @QuestionGroupCodeList = ''
SET @sql = N'
SELECT @QuestionGroupCodeList = @QuestionGroupCodeList + ''N''''''+[Code]+'''''',''
FROM [OMSDB].[dbo].[QuestionGroupInfo]
WHERE LEFT(RIGHT(Code,8),2)=@CourseID'
EXECUTE sp_executesql @sql,N'@QuestionGroupCodeList NVARCHAR(MAX) OUTPUT,@CourseID NVARCHAR(2)',
@QuestionGroupCodeList=@QuestionGroupCodeList OUTPUT,
@CourseID=@CourseID IF RIGHT(@QuestionGroupCodeList,1)=','
SET @QuestionGroupCodeList = LEFT(@QuestionGroupCodeList,LEN(@QuestionGroupCodeList)-1) SET @sql = N'
CREATE PARTITION FUNCTION ['+@CourseID+'_SubjectiveScoreInfoPartitionFunction](NVARCHAR(26))
AS RANGE LEFT FOR VALUES ( '+ @QuestionGroupCodeList +' )'
EXECUTE sp_executesql @sql SET @sql = N'
CREATE PARTITION SCHEME ['+@CourseID+'_SubjectiveScoreInfoPartitionSchema]
AS PARTITION ['+@CourseID+'_SubjectiveScoreInfoPartitionFunction] ALL TO ([PRIMARY])'
EXECUTE sp_executesql @sql SET @sql = N'
CREATE CLUSTERED INDEX [IX_'+@CourseID+'_SubjectiveScoreInfo_Clustered] ON ['+@CourseID+'_SubjectiveScoreInfo] (QuestionGroupCode)
WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
ON ['+@CourseID+'_SubjectiveScoreInfoPartitionSchema](QuestionGroupCode)'
EXECUTE sp_executesql @sql
COMMIT TRAN END TRY
BEGIN CATCH
PRINT 'Error:' + CONVERT(VARCHAR,ERROR_NUMBER()) + ',Message:' + error_message()
ROLLBACK TRAN
END CATCH
END

5.创建多文件组分区完整代码如下:

USE [OMSDB]

DECLARE @dbPath NVARCHAR(MAX)
DECLARE @CourseID NVARCHAR(MAX)
SET @dbPath = 'D:\firstelite\data2'
SET @CourseID = '' DECLARE @sql NVARCHAR(MAX)
DECLARE @QuestionGroupCodes TABLE(QuestionGroupCode NVARCHAR(50))
DECLARE @QuestionGroupCodeList NVARCHAR(MAX) INSERT INTO @QuestionGroupCodes(QuestionGroupCode)
SELECT [Code] FROM [OMSDB].[dbo].[QuestionGroupInfo]
WHERE LEFT(RIGHT(Code,8),2)=@CourseID
ORDER BY [Code] ASC --删除所有文件组
SET @sql = N''
SELECT @sql = @sql + 'DBCC SHRINKFILE(N'''+QuestionGroupCode+''', EMPTYFILE);' + CHAR(13) + CHAR(10)
+ 'ALTER DATABASE OMSDB REMOVE FILE ['+QuestionGroupCode+'];' + CHAR(13) + CHAR(10)
+ 'ALTER DATABASE OMSDB REMOVE FILEGROUP FG' + QuestionGroupCode + ';' + CHAR(13) + CHAR(10)
FROM @QuestionGroupCodes
PRINT @sql
EXECUTE sp_executesql @sql --动态创建所有文件组
SET @sql = N''
SELECT @sql = @sql + 'ALTER DATABASE OMSDB ADD FILEGROUP FG' + QuestionGroupCode + ';' + CHAR(13) + CHAR(10)
+ 'ALTER DATABASE OMSDB ADD FILE(name=N'''+ QuestionGroupCode +''',filename=N'''+ @dbPath + '\' + QuestionGroupCode +'.ndf'',size=5MB,filegrowth=5MB) to filegroup FG' + QuestionGroupCode + ';' + CHAR(13) + CHAR(10)
FROM @QuestionGroupCodes
PRINT @sql
EXECUTE sp_executesql @sql --动态创建分区函数
SET @QuestionGroupCodeList = ''
SELECT @QuestionGroupCodeList = @QuestionGroupCodeList + 'N'''+[QuestionGroupCode]+''','
FROM @QuestionGroupCodes IF RIGHT(@QuestionGroupCodeList,1)=','
SET @QuestionGroupCodeList = LEFT(@QuestionGroupCodeList,LEN(@QuestionGroupCodeList)-1) SET @sql = N'
CREATE PARTITION FUNCTION ['+@CourseID+'_SubjectiveScoreInfoPartitionFunction](NVARCHAR(26))
AS RANGE LEFT FOR VALUES ( '+ @QuestionGroupCodeList +' )'
PRINT @sql
EXECUTE sp_executesql @sql --动态创建分区模式
SET @QuestionGroupCodeList = '[PRIMARY],'
SELECT @QuestionGroupCodeList = @QuestionGroupCodeList + '[FG'+[QuestionGroupCode]+'],'
FROM @QuestionGroupCodes IF RIGHT(@QuestionGroupCodeList,1)=','
SET @QuestionGroupCodeList = LEFT(@QuestionGroupCodeList,LEN(@QuestionGroupCodeList)-1) SET @sql = N'
CREATE PARTITION SCHEME ['+@CourseID+'_SubjectiveScoreInfoPartitionSchema]
AS PARTITION ['+@CourseID+'_SubjectiveScoreInfoPartitionFunction] TO (' + @QuestionGroupCodeList + ')'
PRINT @sql
EXECUTE sp_executesql @sql --创建表分区
SET @sql = N'
CREATE CLUSTERED INDEX [IX_'+@CourseID+'_SubjectiveScoreInfo_Clustered] ON ['+@CourseID+'_SubjectiveScoreInfo]
(
QuestionGroupCode,
ID
)
WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
ON ['+@CourseID+'_SubjectiveScoreInfoPartitionSchema](QuestionGroupCode)'
PRINT @sql
EXECUTE sp_executesql @sql --SET @sql = N'
-- CREATE CLUSTERED INDEX [IX_'+@CourseID+'_SubjectiveScoreInfoFlag_Clustered] ON ['+@CourseID+'_SubjectiveScoreInfoFlag]
-- (
-- [QuestionGroupCode]
-- ) WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
-- ON ['+@CourseID+'_SubjectiveScoreInfoPartitionSchema](QuestionGroupCode)'
--PRINT @sql
--EXECUTE sp_executesql @sql --其它相关语句
--DROP PARTITION SCHEME [01_SubjectiveScoreInfoPartitionSchema]
--DROP PARTITION FUNCTION [01_SubjectiveScoreInfoPartitionFunction] --ALTER DATABASE OMSDB ADD FILEGROUP FG11111111111500040401000301;
--ALTER DATABASE OMSDB ADD FILE(name=N'11111111111500040401000301',filename=N'D:\firstelite\data2\11111111111500040401000301.ndf',size=5MB,filegrowth=5MB); --DBCC SHRINKFILE(N'11111111111500040401000301', EMPTYFILE)
--ALTER DATABASE OMSDB REMOVE FILE [11111111111500040401000301]
--ALTER DATABASE OMSDB REMOVE FILEGROUP FG11111111111500040401000301;

6.MSDN示例代码如下:

A. 将已分区表或索引的一个分区拆分为两个分区

以下示例创建了一个分区函数,以将表或索引划分为四个分区。ALTER PARTITION FUNCTION 将某个分区拆分为两个分区,从而总共创建了五个分区。

CREATE PARTITION FUNCTION myRangePF1 (int)
AS RANGE LEFT FOR VALUES ( 1, 100, 1000 );
GO
--Split the partition between boundary_values 100 and 1000
--to create two partitions between boundary_values 100 and 500
--and between boundary_values 500 and 1000. ALTER PARTITION SCHEME SchemaForParirion NEXT USED 'PRIMARY'
ALTER PARTITION FUNCTION myRangePF1 ()
SPLIT RANGE (500);

B. 将已分区表的两个分区合并为一个分区

以下示例与上例创建同一分区函数,然后将两个分区合并为一个分区,从而总共创建了三个分区。

CREATE PARTITION FUNCTION myRangePF1 (int)
AS RANGE LEFT FOR VALUES ( 1, 100, 1000 );
GO
--Merge the partitions between boundary_values 1 and 100
--and between boundary_values 100 and 1000 to create one partition
--between boundary_values 1 and 1000.
ALTER PARTITION FUNCTION myRangePF1 ()
MERGE RANGE (100);

总结:

  目前无法做如何删除分区,只能更改分区值,所以分区结构一旦创建,无法更改。

  网上有人给出如下2种方案:

  1.新建一张表,把数据插入新表,删除旧表,才能删除分区,然后重命名旧表,新建表分区。一般分区的表数据量都特别大,迁移数据耗时,本人没有试验。

2.按如下步骤,但我试了,没有成功,最后2步一直提示分区使用中,无法操作。

假设你创建的步骤是这样的:
1、CREATE PARTITION FUNCTION PF1(INT) AS RANGE LEFT FOR VALUES (1, 2, 3, 4)
2、CREATE PARTITION SCHEME PS1 AS PARTITION PF1 ALL TO ([PRIMARY])
3、CREATE CLUSTERED INDEX CIDX_X ON X(col1) ON PS1(col1)
删除的步骤应该是这样的:
1、DROP INDEX CIDX_X on X
2、CREATE CLUSTERED INDEX CIDX_X1 ON X(col1) ON [PRIMARY]
3、DROP PARTITION SCHEME PS1
4、DROP PARTITION FUNCTION PF1
5、DROP INDEX CIDX_X1 ON X

SQL Server 表分区备忘的更多相关文章

  1. SQL Server表分区的NULL值问题

    SQL Server表分区的NULL值问题 SQL Server表分区只支持range分区这一种类型,但是本人觉得已经够用了 虽然MySQL支持四种分区类型:RANGE分区.LIST分区.HASH分区 ...

  2. SQL Server表分区【转】

    转自:http://www.cnblogs.com/knowledgesea/p/3696912.html SQL Server表分区   什么是表分区 一般情况下,我们建立数据库表时,表数据都存放在 ...

  3. SQL Server表分区详解

    原文:SQL Server表分区详解 什么是表分区 一般情况下,我们建立数据库表时,表数据都存放在一个文件里. 但是如果是分区表的话,表数据就会按照你指定的规则分放到不同的文件里,把一个大的数据文件拆 ...

  4. SQL Server表分区-水平分区

    SQL Server表分区,sql server水平分区 转自:http://www.cnblogs.com/knowledgesea/p/3696912.html  根据时间的,直接上T-SQL代码 ...

  5. 8、SQL Server 表分区

    什么是表分区?表分区其实就是将一个大表分成若干个小表.表分区可以从物理上将一个大表分成几个小表,但是逻辑上还是一个表.所以当执行插入.更新等操作的时候,不需要我们去判断应该插入或更新到哪个表中.只需要 ...

  6. SQL Server表分区案例

    --学习创建表分区脚本/*SQL SERVER 2005中以上版本,终于引入了表分区,就是说,当一个表里的数据很多时,可以将其分拆到多个的表里,大大提高了性能.下面举例子说明之*/ --------- ...

  7. sql server 表分区

    背景: 一般情况下,我们建立数据库表时,表数据都存放在一个文件里. 但是如果是分区表的话,表数据就会按照你指定的规则分放到不同的文件里,把一个大的数据文件拆分为多个小文件,还可以把这些小文件放在不同的 ...

  8. SQL Server表分区(水平分区及垂直分区)

    什么是表分区? 表分区分为水平表分区和垂直表分区,水平表分区就是将一个具有大量数据的表,进行拆分为具有相同表结构的若干个表:而垂直表分区就是把一个拥有多个字段的表,根据需要进行拆分列,然后根据某一个字 ...

  9. SQL Server 表分区之水平表分区

    什么是表分区? 表分区分为水平表分区和垂直表分区,水平表分区就是将一个具有大量数据的表,进行拆分为具有相同表结构的若干个表:而垂直表分区就是把一个拥有多个字段的表,根据需要进行拆分列,然后根据某一个字 ...

随机推荐

  1. InterBase 数据库与驱动 版本不同

    [Window Title] Project1 - Delphi 10.1 Berlin - Unit1 [Content] Failed: "unsupported on-disk str ...

  2. sql语句最后一行显示统计。

    SELECT id, username, id_Num FROM users UNION ALL SELECT '合计', count(*), null FROM users ORDER BY id_ ...

  3. mac mamp环境 和linux下 安装redis 和可视化工具 Redis Desktop Manager

    mac下安装 第一步:安装redis 1. brew install redis 2.启动服务/usr/local/opt/redis/bin/redis-server 3.配置redis密码访问 编 ...

  4. 搭建Hadoop2.7.1的分布式集群

    Hadoop 2.7.1 (2015-7-6更新),hadoop的环境配置不是特别的复杂,但是确实有很多细节需要注意,不然会造成许多配置错误的情况.尽量保证一次配置正确防止反复修改. 网上教程有很多关 ...

  5. ubuntu下修改MYSQL数据库密码

    在介绍修改密码之前,先介绍一个文件/etc/MySQL/debian.cnf.其主要内容如下图: 里面有一个debian-sys-maint用户,这个用户只有Debian或Ubuntu服务器才有,所以 ...

  6. dapper 简单多表查询

    public List<Book> GetBookList() { List<Book> bList = null; try { using (var t = new SqlC ...

  7. StringUtils详细介绍

    StringUtils详细介绍 public static void TestStr(){ #null 和 "" 操作~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...

  8. U盘如何去除写保护

    用一个简单而粗暴的方法去除U盘写保护: www.upantool.com/hfxf/xiufu//FormatTool.html#softdown 没有比这个更牛的软件了.

  9. SharePoint Framework 构建你的第一个web部件(二)

    博客地址:http://blog.csdn.net/FoxDave 本篇接上一讲,介绍一下web部件项目中的代码. 下面首先列举一下项目中的一些关键文件. Web部件类 HelloWorldWebPa ...

  10. css 1) calc() 函数的使用. 2)box-sizing:border-box

    calc() 是一个css 函数, 可以实现.计算 ---------------------------- 1. 每个div宽度是25%; 总共4个div. 同时 前三个div 有 border-r ...