SQL server 表分区 小Demo 参考学习

表分区demo, 按date来作为分区临界值

/*创建分区函数,并指定各分区临界值 */
create partition function fnPartition(DATE)
AS range right /*临界值归右边的分区 从左至右 从小到大*/
for values( '2004-01-01','2007-01-01') /*两个临界值 ,划分三个分区*/

select * from sys.partition_functions /*查看分区函数是否创建成功*/


/*创建分区架构,指定各个分区所属文件组*/
CREATE PARTITION SCHEME SchemaForPartition
AS PARTITION fnPartition
 TO ([PRIMARY], [PRIMARY], [PRIMARY] )  /*文件组*/

 select *  from sys.partition_schemes/*查看分区架构是否创建成功*/

 /*创建分区表*/
 create table partitionedTable
 ( id int,
 orderid int,
 SalesDate Date) On SchemaForPartition(SalesDate) /*制定分区关键字 以该关键字进行分区*/


 /*查看各分区 现有记录数*/
 select		
	convert(varchar(50), ps.name) as partition_scheme,
	p.partition_number, 
	convert(varchar(10), ds2.name) as filegroup, 
	convert(varchar(19), isnull(v.value, ''), 120) as range_boundary, 
	str(p.rows, 9) as rows
from sys.indexes i 
	join sys.partition_schemes ps on i.data_space_id = ps.data_space_id 
	join sys.destination_data_spaces dds
	on ps.data_space_id = dds.partition_scheme_id 
	join sys.data_spaces ds2 on dds.data_space_id = ds2.data_space_id 
	join sys.partitions p on dds.destination_id = p.partition_number
	and p.object_id = i.object_id and p.index_id = i.index_id 
	join sys.partition_functions pf on ps.function_id = pf.function_id 
	LEFT JOIN sys.Partition_Range_values v on pf.function_id = v.function_id
	and v.boundary_id = p.partition_number - pf.boundary_value_on_right 
WHERE i.object_id = object_id('PartitionedTable')
	and i.index_id in (0, 1) 
order by p.partition_number

/*随机插入数据 查看是否插入到对应分区*/
insert into partitionedTable
values( 0,1,'2010-01-05')

/*查询该表 会查出所有数据,只是底层分区进行存储和检索*/
select * from partitionedTable


底层逻辑概念 可参考 一下网址
http://www.cnblogs.com/sienpower/archive/2011/12/31/2308741.html

你可能感兴趣的:(SQL Server,表分区)