首页 > 代码库 > 表分区常用脚本

表分区常用脚本

use SalesDB1go-- 查看表是否已经分区SELECT *FROM sys .tables AS tJOIN 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_idWHERE t .name = table ; -- 只加表名不需要加上架构名GO-- 查询库中的那些表有分区select  tbl .namefrom    sys .partition_functions pfjoin    sys .partition_schemes ps on pf. function_id = ps. function_idjoin    sys .indexes idx on idx. data_space_id = ps. data_space_id and idx.index_id >1join    sys .tables tbl on idx. object_id = tbl. object_idorder by tbl. name-- 确定已经分区的表的列:SELECT    t.[object_id] AS ObjectID    , t .name AS TableName    , ic .column_id AS PartitioningColumnID    , c .name AS PartitioningColumnNameFROM sys .tables AS t JOIN sys.indexes AS i    ON t .[object_id] = i .[object_id]    AND i .[type] <= 1 -- clustered index or a heapJOIN sys .partition_schemes AS ps    ON ps .data_space_id = i .data_space_idJOIN 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 columnJOIN sys .columns AS c    ON t .[object_id] = c .[object_id]     AND ic .column_id = c .column_idWHERE 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 BoundaryValueFROM sys .tables AS tJOIN sys .indexes AS i    ON t .object_id = i .object_idJOIN sys .partitions AS p    ON i .object_id = p .object_id AND i .index_id = p .index_idJOIN  sys .partition_schemes AS s    ON i .data_space_id = s .data_space_idJOIN sys .partition_functions AS f    ON s .function_id = f .function_idLEFT JOIN sys. partition_range_values AS r    ON f .function_id = r .function_id and r .boundary_id = p.partition_numberWHERE t .name = Table AND i.type <= 1ORDER 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)