Sql Server集群索引“分区”。主要问题

问题描述 投票:0回答:1

我正在尝试实现“穷人的分区”策略,即将多个表与聚簇索引结合使用视图。以下是我尝试实现此目标的示例:

CREATE TABLE customer_data_Q1_2014
(
    customer_index integer not null,
    customer_id int not null,
    customer_name varchar(128),
    transaction_date datetime2,
    CONSTRAINT PK_data_Q1_2014 PRIMARY KEY CLUSTERED (customer_index, customer_id)
) ON [FileGroup_1];


CREATE TABLE customer_data_Q2_2014
(
    -- same as above
)

CREATE TABLE customer_data_Q3_2014
(
    --same as above
)

--and so on...

ALTER TABLE DBO.customer_data_Q1_2014
ADD CONSTRAINT CK_customer_data_Q1_2014 CHECK (transaction_date >= '2014-01-01 00:00:00.000000' AND transaction_date < '2014-04-01 00:00:00.000000');

-- and so on...



USE PROD_DB
GO
CREATE VIEW customer_data_view 
WITH SCHEMABINDING
AS

SELECT customer_index, customer_id, customer_name, transaction_date 
FROM DBO.customer_data_Q1_2014
UNION ALL
SELECT customer_index, customer_id, customer_name, transaction_date 
FROM DBO.customer_data_Q1_2014
UNION ALL
-- AND SO ON...

现在这一切都很好,直到我尝试在我的视图中插入任何内容。我得到的消息是“Partitioned View not updatable”

据我所知,它与未将日期作为主键的一部分有关。问题在于,对我而言,将日期作为主键的一部分实际上是不正确的。

例如:

假设我正在尝试插入重复的customer_index和customer_id

insert into customer_data (customer_index, customer_id, transaction_date) 
values (1,2,'2018-01-01 00:00:00.000000')

insert into customer_data (customer_index, customer_id, transaction_date) 
values (1,2,'2017-05-12 00:00:00.000000')

如果日期是我的群集主键的一部分,那么这将是允许的,我将留下一个重复的customer_id,customer_index组合。

如何在不将日期作为我的群集主键的一部分的情况下实现我的穷人的分区?

sql-server database partitioning clustered-index
1个回答
1
投票

我建议创建一个INSTEAD OF触发器:https://msdn.microsoft.com/en-us/library/def01zh2.aspx

这样您就可以手动实现更新逻辑。当在视图上尝试INSERT时,触发器可以根据其日期触发并实现您自己的自定义逻辑,该逻辑关于添加记录的基础表。

© www.soinside.com 2019 - 2024. All rights reserved.