簡體   English   中英

通過外鍵列在SQL Server上進行表分區

[英]Table partitioning on sql server by foreign key column

我發現的所有表分區示例都非常簡單,但是我需要按一個條件對許多表進行分區。

例如,我有以下表:Contractors and Products,其中Products表中的ContractorId是外鍵。

我為ContractorId創建了函數和架構。 它非常適合Contractors表,但涉及到Products表...

我不知道應該如何使用它,因為嘗試時總是得到以下信息:“為聚簇索引'PK_dbo.Products'指定的文件組'PRIMARY'被用於表'dbo.Products',即使分區方案'scheme_Contractors為此指定了“。 我的產品表如下所示:

CREATE TABLE [dbo].[Products](
[ProductId] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](max) NULL,
[Amount] [int] NULL,
[Color] [nvarchar](max) NULL,
[Price] [decimal](18, 2) NULL,
[Guarantee] [nvarchar](max) NULL,
[GuaranteeType] [int] NULL,
[AdditionalFeatures] [nvarchar](max) NULL,
[Valid] [bit] NULL,
[ContractorId] [int] NOT NULL,
[ProducerId] [int] NOT NULL,
[ProductCategoryId] [int] NOT NULL,
 CONSTRAINT [PK_dbo.Products] PRIMARY KEY ( [ProductId] ASC ) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] )
GO
ALTER TABLE [dbo].[Products]  WITH CHECK ADD  CONSTRAINT [FK_dbo.Products_dbo.Contractors_ContractorId] FOREIGN KEY([ContractorId]) 
REFERENCES [dbo].[Contractors] ([ContractorId])
GO
ALTER TABLE [dbo].[Products] CHECK CONSTRAINT [FK_dbo.Products_dbo.Contractors_ContractorId]
GO

誰能告訴我-是否可以在ContractorId列上使用我的架構? 先感謝您!

與Dan Guzman達成一致,我想指出表定義中應該沒有[PRIMARY]規范。

我們大規模使用分區。 將所有表按相同的分區方案進行分區非常舒適,因為SQL引擎將充分利用其多處理器並行化功能。

當某個分區組位於一個數據庫文件中而另一分區位於另一個文件中時,您甚至可以靈活使用磁盤使用和備份。

因此,您首先需要一個分區函數來定義分區方案的值:

CREATE PARTITION FUNCTION [ContractorPartitionFunction](int) AS RANGE LEFT
FOR VALUES (contractor1,contractor2,...)

然后,您需要創建分區方案

CREATE PARTITION SCHEME [ContractorPartitionScheme] 
AS PARTITION [ContractorPartitionFunction] 
TO ([File_001],[File_002],...,[PRIMARY])

然后,對於您現在創建的所有表和索引,應從定義中刪除ON [PRIMARY]作為目標文件組,而應使用ON [ContractorPartitionScheme](ContractorId)

因此,您的表定義現在應顯示為:

CREATE TABLE [dbo].[Products](
[ProductId] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](max) NULL,
[Amount] [int] NULL,
[Color] [nvarchar](max) NULL,
[Price] [decimal](18, 2) NULL,
[Guarantee] [nvarchar](max) NULL,
[GuaranteeType] [int] NULL,
[AdditionalFeatures] [nvarchar](max) NULL,
[Valid] [bit] NULL,
[ContractorId] [int] NOT NULL,
[ProducerId] [int] NOT NULL,
[ProductCategoryId] [int] NOT NULL)
ON ContractorPartitionScheme(ContractorId)

CREATE UNIQUE NONCLUSTERED INDEX PK_dbo.Products ON Products
    (
    productId,
    ConstructorId
    ) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
 ON ContractorPartitionScheme(ContractorId) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM