----------------------------------------建立分割槽表start
--分割槽函式
create partition function partfunbfq (datetime)
as range right for values ('20110101','20120101','20130101','20140101','20150101')
--分割槽方案
create partition scheme partschbfq
as partition partfunbfq
to (
bfq2011,
bfq2012,
bfq2013,
bfq2014,
bfq2015,
bfq2016
) --建立表
create table bfq_test(
[id] [int] identity(1,1) not null,
[name] [varchar](16) not null,
[bfqtime][datetime] not null
) on partschbfq([bfqtime])
create table bfq_test_a(
[id] [int] identity(1,1) not null,
[name] [varchar](16) not null,
[bfqtime][datetime] not null
) on partschbfq([bfqtime])
--向表中插入資料
insert into bfq_test_a values('測試一','2010-01-01 00:00:00');
insert into bfq_test_a values('測試二','2011-01-01 00:00:00');
insert into bfq_test_a values('測試三','2012-01-01 00:00:00');
insert into bfq_test_a values('測試四','2013-01-01 00:00:00');
insert into bfq_test_a values('測試五','2014-01-01 00:00:00');
insert into bfq_test_a values('測試六','2015-01-01 00:00:00');
insert into bfq_test_a values('測試七','2016-01-01 00:00:00');
--查詢所有分割槽表資料
select * from bfq_test;
--查詢資料在那個分割槽
select $partition.partfunbfq ('2010-10-1');
select $partition.partfunbfq ('2011-10-1');
select $partition.partfunbfq ('2012-10-1');
select $partition.partfunbfq ('2013-10-1');
select $partition.partfunbfq ('2014-10-1');
select $partition.partfunbfq ('2015-10-1');
select $partition.partfunbfq ('2016-10-1');
--查詢各個分區內的內容
select * from bfq_test where $partition.partfunbfq(bfqtime)=1;
select * from bfq_test where $partition.partfunbfq(bfqtime)=2;
select * from bfq_test where $partition.partfunbfq(bfqtime)=3;
select * from bfq_test where $partition.partfunbfq(bfqtime)=4;
select * from bfq_test where $partition.partfunbfq(bfqtime)=5;
select * from bfq_test where $partition.partfunbfq(bfqtime)=6;
--統計每個物理分割槽表中的記錄數
select $partition.partfunbfq(bfqtime) as 分割槽編號, * from bfq_test;
select $partition.partfunbfq(bfqtime) as 分割槽編號,count(id) as 記錄數 from bfq_test group by $partition.partfunbfq(bfqtime);
--統計所有分割槽中的記錄總數
select $partition.partfunbfq(bfqtime) as 分割槽編號,count(id) as 記錄數 from bfq_test group by $partition.partfunbfq(bfqtime);
update bfq_test set bfqtime = '2010-01-01 00:00:00' where name = '測試七';
----------------------------------------建立分割槽表end
----------------------------------------將普通表轉換成分割槽表start
--刪除表
drop table bfq_test_a;
--新建乙個普通的資料表
create table bfq_test_a(
[id] [int] identity(1,1) not null, --自動增長
[name] [varchar](16) not null,
[time] [datetime] not null,
constraint [pk_bfq_test_a] primary key clustered --建立主鍵
( [id] asc
) )
--向表中插入資料
insert into bfq_test_a values('測試1', '2010-01-01 00:00:00');
insert into bfq_test_a values('測試2', '2011-01-01 00:00:00');
insert into bfq_test_a values('測試3', '2011-01-01 00:00:00');
insert into bfq_test_a values('測試4', '2012-01-01 00:00:00');
insert into bfq_test_a values('測試5', '2013-01-01 00:00:00');
insert into bfq_test_a values('測試6', '2013-01-01 00:00:00');
insert into bfq_test_a values('測試7', '2014-01-01 00:00:00');
insert into bfq_test_a values('測試8', '2014-01-01 00:00:00');
insert into bfq_test_a values('測試9', '2015-01-01 00:00:00');
insert into bfq_test_a values('測試10', '2015-01-01 00:00:00');
insert into bfq_test_a values('測試11', '2016-01-01 00:00:00');
insert into bfq_test_a values('測試12', '2016-01-01 00:00:00');
insert into bfq_test_a values('測試13', '2017-01-01 00:00:00');
--刪掉主鍵
alter table bfq_test_a drop constraint pk_bfq_test_a
--建立主鍵,但不設為聚集索引
alter table bfq_test_a add constraint pk_bfq_test_a primary key nonclustered
( [id] asc
) on [primary]
--建立乙個新的聚集索引,在該聚集索引中使用分割槽方案
create clustered index ct_bfq_test_a on bfq_test_a([time])
on partschbfq([time])
--統計每個物理分割槽表中的記錄數
select $partition.partfunbfq(time) as 分割槽編號, * from bfq_test_a;
----------------------------------------將普通表轉換成分割槽表end
Sqlserver表分割槽
use testsplitdb 先建立好資料庫 1.建立檔案組 alter database testsplitdb add filegroup testsplitgroup1 alter database testsplitdb add filegroup testsplitgroup2 alte...
SQL Server表分割槽
第一步,建立檔案組 1 右擊資料庫 屬性 檔案組 2 t sql alter database 資料庫名稱 add filegroup 檔案組名 例項 alter database bussystem add filegroup testgroup1 第二步,建立資料檔案到檔案組 1 右擊資料庫 屬...
SQL Server表分割槽
你是否在千方百計優化sql server 資料庫的效能?如果你的資料庫中含有大量的 把這些 分割槽放入獨立的檔案組可能會讓你受益匪淺。sql server 2005引入的表分割槽技術,讓使用者能夠把資料分散存放到不同的物理磁碟中,提高這些磁碟的並行處理效能以優化查詢效能。sql server資料庫表...