如何按日期分区表?

时间:2012-01-22 20:29:24

标签: sql-server sql-server-2000 sql-server-2008-r2 partitioning

如何按日期对SQL Server中的行进行分区?

Bonus Chatter

在SQL Server中,按对表进行分区相对容易,例如:

  • Orders_2000
  • Orders_2001
  • Orders_2002
  • ...
  • Orders_2011
  • Orders_2012

您为每年创建一个单独的表,确保包含一个Year列,其中的检查约束与表匹配:

CREATE TABLE Transactions_1999 (Year int NOT NULL DEFAULT 1999, TransactionID int NOT NULL, CustomerName varchar(50))
    ALTER TABLE Transactions_1999 ADD CONSTRAINT PK_Transactions1999 PRIMARY KEY CLUSTERED (Year, TransactionID)
    ALTER TABLE Transactions_1999 ADD CONSTRAINT CK_Transactions1999_Year CHECK (Year = '1999')

CREATE TABLE Transactions_2000 (Year int NOT NULL DEFAULT 2000, TransactionID int NOT NULL, CustomerName varchar(50))
    ALTER TABLE Transactions_2000 ADD CONSTRAINT PK_Transactions2000 PRIMARY KEY (Year, TransactionID)
    ALTER TABLE Transactions_2000 ADD CONSTRAINT CK_Transactions2000_Year CHECK (Year = '2000')

CREATE TABLE Transactions_2001 (Year int NOT NULL DEFAULT 2001, TransactionID int NOT NULL, CustomerName varchar(50))
    ALTER TABLE Transactions_2001 ADD CONSTRAINT PK_Transactions2001 PRIMARY KEY (Year, TransactionID)
    ALTER TABLE Transactions_2001 ADD CONSTRAINT CK_Transactions2001_Year CHECK (Year = '2001')
...
CREATE TABLE Transactions_2011 (year int NOT NULL DEFAULT 2011, TransactionID int NOT NULL, CustomerName varchar(50))
    ALTER TABLE Transactions_2011 ADD CONSTRAINT PK_Transactions2011 PRIMARY KEY (Year, TransactionID)
    ALTER TABLE Transactions_2011 ADD CONSTRAINT CK_Transactions2011_Year CHECK (Year = '2011')

CREATE TABLE Transactions_2012 (Year int NOT NULL DEFAULT 2012, TransactionID int NOT NULL, CustomerName varchar(50))
    ALTER TABLE Transactions_2012 ADD CONSTRAINT PK_Transactions2012 PRIMARY KEY (Year, TransactionID)
    ALTER TABLE Transactions_2012 ADD CONSTRAINT CK_Transactions2012_Year CHECK (Year = '2012')

然后,您可以构建一个VIEW,它将表合并到一起:

CREATE VIEW Transactions AS
SELECT * FROM Transactions_1999
UNION ALL
SELECT * FROM Transactions_2000
UNION ALL
SELECT * FROM Transactions_2001
UNION ALL
--...
SELECT * FROM Transactions_2011
UNION ALL
SELECT * FROM Transactions_2012

现在您可以查询此Transactions视图:

SELECT * FROM Transactions
WHERE (CustomerName LIKE 'boyd%')
OR (CustomerName LIKE '% boyd%')

数据可以插入到视图中,SQL Server会自动将其放入它应该进入的表中:

INSERT INTO Transactions(Year, TransactionID, CustomerName)
VALUES (2012, 378922384, 'Ian Boyd')

但那不是我想要的

我想通过 年龄 将数据分区到表格中。如果TransactionDate超过90天的行应该放在一个表中,而且TransactionDate超过90天的行将进入另一个表:

  • Transactions_olderThan90Days
  • Transactions_newerThan90Days

所以说SQL:

CREATE TABLE Transactions_NewerThan90Days (
    TransactionDate datetime NOT NULL,
    TransactionID int NOT NULL,
    CustomeName varchar(50))


CREATE TABLE Transactions_OlderThan90Days (
    TransactionDate datetime NOT NULL,
    TransactionID int NOT NULL,
    CustomeName varchar(50))

在SQL Server中是否可以这样做?

我意识到这听起来不可能,因为这意味着SQL Server必须不断地移动数据。

1 个答案:

答案 0 :(得分:0)

从SQL 2008开始,您可以按周或按月创建表分区,具体取决于您希望切换数据的频率。看看并行处理,如果你的大多数问题调用三个或四个分区,那么相同数量的处理器开始并行处理你的答案。 (我不建议在白天使用分区,因为在3年内达到了1000个分区的限制。)