Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server Sql Server聚集索引“;分区";。主要关键问题_Sql Server_Database_Partitioning_Clustered Index - Fatal编程技术网

Sql server Sql Server聚集索引“;分区";。主要关键问题

Sql server Sql Server聚集索引“;分区";。主要关键问题,sql-server,database,partitioning,clustered-index,Sql Server,Database,Partitioning,Clustered Index,我正在尝试实现“穷人分区”策略,即将多个具有聚集索引的表与一个视图相结合。以下是我如何努力实现这一目标的示例: 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_

我正在尝试实现“穷人分区”策略,即将多个具有聚集索引的表与一个视图相结合。以下是我如何努力实现这一目标的示例:

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...
如果日期是我的聚集主键的一部分,这将是允许的,并且我将留下一个重复的customer\u id,customer\u index combo


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

我建议创建一个替代触发器:


通过这种方式,您可以手动实现更新逻辑。当在视图上尝试插入时,触发器可以触发并实现您自己的自定义逻辑,根据记录的日期将记录添加到哪个基础表中。

可能从两个表开始,然后尝试使用这两个表。也许这些表中只有一个具有违反规则的检查约束。有时发现问题更容易分而治之。正如和@fauxmosapien所建议的,使用一个而不是触发器。有人知道SQL Server中的真正分区(使用分区函数)是否以同样的方式工作吗?我可以不使用日期作为主键按日期进行分区吗?看起来很有希望。我试试这个。
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')