日期:2014-05-17 浏览次数:20495 次
--分区函数
CREATE PARTITION FUNCTION [MyPartitonFun1](int) AS RANGE RIGHT FOR VALUES (3000000, 6000000, 9000000)
--分区方案
CREATE PARTITION SCHEME [MyPartitonScheme] AS PARTITION [MyPartitonFun1] TO ([PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY])
--建表
create table tableABC
(
id1 int,
id2 int,
remark varchar(50)
)
--插入数据
insert into TableABC values (1,1,'123123')
insert into TableABC values (2,100000000,'123123')
--建立聚集索引的时候,
--on tableABC(id1),指在id1上建立聚集索引,
--on MyPartitonScheme(id1)值按id1来分区,、
--若on MyPartitonScheme(id2)指按id2上分区
create clustered index index1 on tableABC(id1)
on MyPartitonScheme(id1)
--建立完索引后,可以根据下面的sql来查看每个分区的数据
--若MyPartitonScheme(id1) ,则两条数据都在第一个分区中
--若MyPartitonScheme(id2),则两条数据位于不同分区中
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('TableABC')
and i.index_id in (0, 1)
order by p.partition_number