表分区常用脚本
use SalesDB1 go -- 查看表是否已经分区 SELECT * FROM sys .tables AS t JOIN sys .indexes AS i ON t .[object_id] = i .[object_id] AND i .[type] IN ( 0,1 ) JOIN sys .partition_schemes ps ON i .data_space_id = ps .data_space_id WHERE t .name = 'table' ; -- 只加表名不需要加上架构名 GO -- 查询库中的那些表有分区 select tbl .name from sys .partition_functions pf join sys .partition_schemes ps on pf. function_id = ps. function_id join sys .indexes idx on idx. data_space_id = ps. data_space_id and idx.index_id >1 join sys .tables tbl on idx. object_id = tbl. object_id order by tbl. name -- 确定已经分区的表的列: SELECT t.[object_id] AS ObjectID , t .name AS TableName , ic .column_id AS PartitioningColumnID , c .name AS PartitioningColumnName FROM sys .tables AS t JOIN sys.indexes AS i ON t .[object_id] = i .[object_id] AND i .[type] <= 1 -- clustered index or a heap JOIN sys .partition_schemes AS ps ON ps .data_space_id = i .data_space_id JOIN sys .index_columns AS ic ON ic .[object_id] = i .[object_id] AND ic .index_id = i .index_id AND ic .partition_ordinal >= 1 -- because 0 = non-partitioning column JOIN sys .columns AS c ON t .[object_id] = c .[object_id] AND ic .column_id = c .column_id WHERE t .name = 'Table' ; GO -- 每个分区的边界值 SELECT t .name AS TableName , i .name AS IndexName , p .partition_number, p.partition_id , i .data_space_id, f. function_id, f.type_desc , r.boundary_id , r .value AS BoundaryValue FROM sys .tables AS t JOIN sys .indexes AS i ON t .object_id = i .object_id JOIN sys .partitions AS p ON i .object_id = p .object_id AND i .index_id = p .index_id JOIN sys .partition_schemes AS s ON i .data_space_id = s .data_space_id JOIN sys .partition_functions AS f ON s .function_id = f .function_id LEFT JOIN sys. partition_range_values AS r ON f .function_id = r .function_id and r .boundary_id = p.partition_number WHERE t .name = 'Table' AND i.type <= 1 ORDER BY p. partition_number; select * from sys.filegroups -- 查看数据库的文件组 select * from sys.sysfiles -- 查看数据库文件 select * from sys.partition_functions -- 查看分区函数 select * from sys.partition_range_values -- 边界值 select * from sys.partition_schemes --分区架构 -- 添加文件组,并指定文件组的文件 ALTER DATABASE [Data Partition DB2]ADD FILEGROUP [Data Partition DB FG5] ALTER DATABASE [Data Partition DB2]ADD FILE(NAME='Data Partition DB FG5', FILENAME='D:\Database\Data Partition DB FG5.ndf') TO FILEGROUP [Data Partition DB FG5]; -- 添加分区函数,及分区架构 alter partition scheme ps_OrderDate next used [FG4] alter partition function pf_OrderDate() split range ('2005/01/01') --删除分区又称合并分区,简单讲就是两个分区的数据进行合并,比如我们想合并年的分区和年的分区到一个分区,我们可以用如下的代码: alter partition function pf_OrderDate() merge range ('2003/01/01')