楼主可以在表里加一个字段,存储(插入日期是本年的第几天+1)%92,然后按这个字段分区--计算某个日期是本年的第几天
select datepart(dy, '2008-09-22')/*
-----------
266(1 row(s) affected)
*/
select datepart(dy, '2008-09-22')/*
-----------
266(1 row(s) affected)
*/
调试欢乐多
表分区对交易环境和数据仓库环境来说,都是一个重要的特征。数据仓库用户最主要的抱怨是移动事实表(fact table)会花费太多时间。当装载数据到事实表的时候,用户查询(立方体处理查询)的性能会明显下降,甚至是完全无法成功。因此,装载大量的数据到事实表的时候常常需要停机。如果使用表分区,就不再出现这样的情况——确切的讲,你一眨眼的工夫就可以移动事实表。为了演示这是如何生效的,我使用上面例子中相同的分区函数和表结构来创建一个新的表,这个表叫做fact_time_entry2。表的主键从五千万开始,这样fact_time_entry2就不会包含表fact_time_entry中已经有的数据。 现在我把2007年的数据移动到这张fact_time_entry2中。同时让我们假设fact_time_entry表中包含着2007年之前的数据。在fact_time_entry2表完成数据的转移,我执行下面的语句: ALTER TABLE fact_time_entry2 SWITCH PARTITION 8 TO fact_time_entry PARTITION 8
这条语句将编号为8的分区,这个分区恰好包含着2007年的数据,从fact_time_entry2移动到了fact_time_entry表中,在我的笔记本电脑上,这个过程只花费了3毫秒。在这短短的3毫秒中,我的事实表就增加了五百万条记录!的确,我需要在交换分区之前,将数据移动到中间表,但是我的用户不需要担心——事实表随时都可以查询!在这幕后,实际上没有数据移动——只是两张表的元数据发生了变化。 我可以使用类似的查询删除事实表中不在需要的数据。例如,假设我们决定我们不再关心2004年的记录。下面的语句可以将这些记录转移到我们创建的工作表中: ALTER TABLE fact_time_entry SWITCH PARTITION 2 TO fact_time_entry2 PARTITION 2
这样的语句依旧在毫秒级内完成了。现在,我可以删除fact_time_entry2或者将它移到其他的服务器上。我的事实表不会包含2004年的任何记录。这个分区还是需要在目的表中存在,而且它必须是空的。你不能将分区转移到一个包含重复数据的表中。源表和目的表的分区必须一致,同时被转移的数据必须在同一个文件组中。即使受到这么多的限制,转换分区和无需停机就可以移动数据表的功能必将让数据仓库的实现变的前所未有的轻松。
create table tb1
(
dy int primary key
, col1 int
--, ...
,check(dy=1)
)
create table tb2
(
dy int primary key
, col1 int
--, ...
,check(dy=2)
)
--...go
create view tb as
select * from tb1
union all select * from tb2
--union all select * from tb3
--...go
insert tb select datepart(dy, '2008-01-01'),1
union all select datepart(dy, '2008-01-02'),2
--union all select datepart(dy, '2008-01-03'),3select * from tb1
/*
dy col1
----------- -----------
1 1(1 row(s) affected)
*/select * from tb2
/*
dy col1
----------- -----------
2 2(1 row(s) affected)
*/drop view tb
drop table tb1,tb2
(
dy int primary key
, col1 int
--, ...
,check(dy=1)
)
create table tb2
(
dy int primary key
, col1 int
--, ...
,check(dy=2)
)
--...go
create view tb as
select * from tb1
union all select * from tb2
--union all select * from tb3
--...go
insert tb select datepart(dy, '2008-01-01'),1
union all select datepart(dy, '2008-01-02'),2
--union all select datepart(dy, '2008-01-03'),3--查看tb1中的数据
select * from tb1
/*
dy col1
----------- -----------
1 1(1 row(s) affected)
*/--查看tb2中的数据
select * from tb2
/*
dy col1
----------- -----------
2 2(1 row(s) affected)
*/--从视图中查
select * from tb where dy=datepart(dy, '2008-01-02')
/*
dy col1
----------- -----------
2 2(1 row(s) affected)
*/drop view tb
drop table tb1,tb2
create table tb1
(
dy int primary key
, col1 int
--, ...
,check(dy=1)
)
create table tb2
(
dy int primary key
, col1 int
--, ...
,check(dy=2)
)
--...go
create view tb as
select * from tb1
union all select * from tb2
--union all select * from tb3
--...go
insert tb select (datepart(dy, '2008-01-01')-1)%92+1,1
union all select (datepart(dy, '2008-01-02')-1)%92+1,2
--union all select datepart(dy, '2008-01-03'),3--查看tb1中的数据
select * from tb1
/*
dy col1
----------- -----------
1 1(1 row(s) affected)
*/--查看tb2中的数据
select * from tb2
/*
dy col1
----------- -----------
2 2(1 row(s) affected)
*/--从视图中查
select * from tb where dy=(datepart(dy, '2008-01-02')-1)%92+1
/*
dy col1
----------- -----------
2 2(1 row(s) affected)
*/drop view tb
drop table tb1,tb2
1.先在表里面添加一个分区列 该列为日期计算出来的 计算列 datediff(day,'1900-01-01',时间)%92 来划分该记录保存的分区
2. 创建分区函数
CREATE PARTITION FUNCTION myRangePF1 (int)
AS RANGE LEFT FOR VALUES (1, 2, 3,4……91) ;
3.创建分区架构
CREATE PARTITION SCHEME myRangePS1
AS PARTITION myRangePF1
TO (test1fg, test2fg, test3fg, test4fg……test92fg)
4. 创建分区表
CREATE TABLE PartitionTable (col1 int, col2 char(10),fq_flag int as datediff(day,'1900-01-01',时间)%92)
ON myRangePS1 (fq_flag) ;
/code]
===200W数据量限制的 BLOG地址:http://www.cnblogs.com/yizhu2000/archive/2007/12/13/992901.html=== 微软msdn关于2005建立方案 http://msdn2.microsoft.com/zh-cn/library/ms181036.aspx
建议换Oracle吧,,,SQL不适合处理这样的大数据。
就行了(分区函数的分区边界值就是 0- 92)
select cast('2008-01-01' as datetime)+92+92+92+9200:00:00.000Create PARTITION FUNCTION F_PARTITION (datetime)
AS RANGE RIGHT FOR VALUES
('2008-01-01', '2008-04-02', '2008-07-03',
'2008-10-03', '2009-01-03')go
Create PARTITION SCHEME S_PARTITION
as PARTITION F_PARTITION
ALL to ([PRIMARY])go
CREATE TABLE dbo.Partition_Table(
ID int identity(1,1),
Date datetime not null
) ON S_PARTITION (Data)
go
---插入数据
insert Partition_Table(Date)
select '2008-01-01' union all
select '2008-04-02' union all
select '2008-07-03' union all
select '2008-10-03' union all
select '2009-01-03'------------查看分区表存储:
select
convert(varchar(16), 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