简体   繁体   English

如何在SQL Server中进行自动数据存档?

[英]How to do automatic data archiving in SQL Server?

I have table for which every day I want to do automatic archiving. 我有桌子每天我想做自动存档。 So to be clear every day I want to take information generated during that day and move it into another partition (of same table) not in another archive table. 因此,为了清楚每天我想要获取当天生成的信息并将其移动到另一个分区(同一个表)而不是另一个归档表中。 That's because I want old data to be accessible with same query as new ones. 那是因为我希望可以使用与新查询相同的查询来访问旧数据。

I'm using SQL Server 2005, I've read http://msdn.microsoft.com/en-us/library/ms345146(SQL.90).aspx article but couldn't find out how can I write partitioning function to satisfy my needs. 我正在使用SQL Server 2005,我已阅读http://msdn.microsoft.com/en-us/library/ms345146(SQL.90).aspx文章,但无法找出如何编写分区功能满足我的需求。

So the solution I hope exists should be one time configuration which won't need any further interference. 所以我希望存在的解决方案应该是一次性配置,不需要任何进一步的干扰。 Do you have any suggestions? 你有什么建议吗?

You can easily do this with partioned tables; 您可以使用分区表轻松完成此操作; example script below -- it creates a temporary db TestDB; 下面的示例脚本 - 它创建一个临时的db TestDB; if you dont want to use it change the database to something else. 如果您不想使用它,请将数据库更改为其他内容。 It cleans itself up at the end IF you run the script as is it Creates the database; 如果您运行脚本,它会在最后清理它自己创建数据库; adds a partitioning funcition based on a Bit. 添加基于Bit的分区函数。 Creates a table TestTable; 创建一个表TestTable; applied with the partitioning function Inerts 3 "Live" rows into the table Shows that 3 rows are all in one of the partition tables by selecting details from sys.partitions THen updates one of the records to make it archived Reselects information from sys.partitions to show that the record has moved to the second schema. 应用分区函数Inerts 3“实时”行到表中通过从sys.partitions中选择详细信息,显示3行都在其中一个分区表中。更新其中一条记录以使其归档从sys.partitions中重新选择信息显示记录已移至第二个架构。

All you would need to do is setup a process to archive the records. 您需要做的就是设置一个存档记录的过程。

USE master;
GO
--- Step 1 : Create New Test Database with two different filegroups.
IF EXISTS (
SELECT name
FROM sys.databases
WHERE name = N'TestDB')
DROP DATABASE TestDB;
GO
CREATE DATABASE TestDB
ON PRIMARY
(NAME='TestDB_Part1',
FILENAME=
'c:\sqldata\TestDB_Part1.mdf',
SIZE=3,
MAXSIZE=100,
FILEGROWTH=1 ),
FILEGROUP TestDB_Part2
(NAME = 'TestDB_Part2',
FILENAME =
'c:\sqldata\TestDB_Part2.ndf',
SIZE =3,
MAXSIZE=100,
FILEGROWTH=1 );
GO



USE TestDB;
GO
--- Step 2 : Create Partition Range Function
CREATE PARTITION FUNCTION TestDB_PartitionRange (Bit)
AS RANGE right FOR
VALUES (1);
GO

CREATE PARTITION SCHEME TestDB_PartitionScheme
AS PARTITION TestDB_PartitionRange
TO ([PRIMARY], TestDB_Part2);
GO


CREATE TABLE TestTable
(Archived Bit NOT NULL,
Date DATETIME)
ON TestDB_PartitionScheme (Archived);
GO


INSERT INTO TestTable (Archived, Date) 
VALUES (0,'2010-01-01');
INSERT INTO TestTable (Archived, Date) 
VALUES (0,'2010-02-01');
INSERT INTO TestTable (Archived, Date) 
VALUES (0,'2010-03-01');
GO

SELECT * FROM TestTable;


SELECT * FROM sys.partitions
WHERE OBJECT_NAME(OBJECT_ID)='TestTable';

update TestTable 
set Archived = 1 where Date = '2010-03-01'

SELECT * FROM sys.partitions
WHERE OBJECT_NAME(OBJECT_ID)='TestTable';


use master
go
drop database testdb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM