Skip to content

Commit

Permalink
FK
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyGaluzo committed Nov 19, 2024
1 parent 848195c commit 61b1c06
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 102 deletions.
213 changes: 111 additions & 102 deletions src/Microsoft.Health.Fhir.SqlServer/Features/Schema/Migrations/84.sql
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,114 @@ CREATE TYPE dbo.UriSearchParamList AS TABLE (
SearchParamId SMALLINT NOT NULL,
Uri VARCHAR (256) COLLATE Latin1_General_100_CS_AS NOT NULL PRIMARY KEY (ResourceTypeId, ResourceSurrogateId, SearchParamId, Uri));

CREATE TABLE dbo.Resource (
ResourceTypeId SMALLINT NOT NULL,
ResourceSurrogateId BIGINT NOT NULL,
ResourceId VARCHAR (64) COLLATE Latin1_General_100_CS_AS NOT NULL,
Version INT NOT NULL,
IsHistory BIT NOT NULL,
IsDeleted BIT NOT NULL,
RequestMethod VARCHAR (10) NULL,
RawResource VARBINARY (MAX) NULL,
IsRawResourceMetaSet BIT NOT NULL,
SearchParamHash VARCHAR (64) NULL,
TransactionId BIGINT NULL,
HistoryTransactionId BIGINT NULL,
FileId BIGINT NULL,
OffsetInFile INT NULL
);


GO
DROP TABLE dbo.Resource;


GO
CREATE TABLE dbo.ResourceIdIntMap (
ResourceTypeId SMALLINT NOT NULL,
ResourceIdInt BIGINT NOT NULL,
ResourceId VARCHAR (64) COLLATE Latin1_General_100_CS_AS NOT NULL CONSTRAINT PKC_ResourceIdIntMap_ResourceIdInt_ResourceTypeId PRIMARY KEY CLUSTERED (ResourceIdInt, ResourceTypeId) WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId),
CONSTRAINT U_ResourceIdIntMap_ResourceId_ResourceTypeId UNIQUE (ResourceId, ResourceTypeId) WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId)
);

ALTER TABLE dbo.ResourceIdIntMap SET (LOCK_ESCALATION = AUTO);


GO
CREATE TABLE dbo.RawResources (
ResourceTypeId SMALLINT NOT NULL,
ResourceSurrogateId BIGINT NOT NULL,
RawResource VARBINARY (MAX) NULL CONSTRAINT PKC_RawResources_ResourceTypeId_ResourceSurrogateId PRIMARY KEY CLUSTERED (ResourceTypeId, ResourceSurrogateId) WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId)
);

ALTER TABLE dbo.RawResources SET (LOCK_ESCALATION = AUTO);


GO
CREATE TABLE dbo.CurrentResources (
ResourceTypeId SMALLINT NOT NULL,
ResourceSurrogateId BIGINT NOT NULL,
ResourceIdInt BIGINT NOT NULL,
Version INT NOT NULL,
IsHistory BIT CONSTRAINT DF_CurrentResources_IsHistory DEFAULT 0 NOT NULL,
IsDeleted BIT NOT NULL,
RequestMethod VARCHAR (10) NULL,
IsRawResourceMetaSet BIT CONSTRAINT DF_CurrentResources_IsRawResourceMetaSet DEFAULT 0 NOT NULL,
SearchParamHash VARCHAR (64) NULL,
TransactionId BIGINT NULL,
HistoryTransactionId BIGINT NULL,
FileId BIGINT NULL,
OffsetInFile INT NULL CONSTRAINT PKC_CurrentResources_ResourceSurrogateId_ResourceTypeId PRIMARY KEY CLUSTERED (ResourceSurrogateId, ResourceTypeId) WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId),
CONSTRAINT CH_ResourceCurrent_IsHistory CHECK (IsHistory = 0),
CONSTRAINT U_CurrentResources_ResourceIdInt_ResourceTypeId UNIQUE (ResourceIdInt, ResourceTypeId) WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId)
);

ALTER TABLE dbo.CurrentResources
ADD CONSTRAINT FK_CurrentResources_ResourceIdInt_ResourceTypeId_ResourceIdIntMap FOREIGN KEY (ResourceIdInt, ResourceTypeId) REFERENCES dbo.ResourceIdIntMap (ResourceIdInt, ResourceTypeId);

ALTER TABLE dbo.CurrentResources SET (LOCK_ESCALATION = AUTO);

CREATE INDEX IX_TransactionId_ResourceTypeId_WHERE_TransactionId_NOT_NULL
ON dbo.CurrentResources(TransactionId, ResourceTypeId) WHERE TransactionId IS NOT NULL WITH (DATA_COMPRESSION = PAGE)
ON PartitionScheme_ResourceTypeId (ResourceTypeId);

CREATE INDEX IX_HistoryTransactionId_ResourceTypeId_WHERE_HistoryTransactionId_NOT_NULL
ON dbo.CurrentResources(HistoryTransactionId, ResourceTypeId) WHERE HistoryTransactionId IS NOT NULL WITH (DATA_COMPRESSION = PAGE)
ON PartitionScheme_ResourceTypeId (ResourceTypeId);


GO
CREATE TABLE dbo.HistoryResources (
ResourceTypeId SMALLINT NOT NULL,
ResourceSurrogateId BIGINT NOT NULL,
ResourceIdInt BIGINT NOT NULL,
Version INT NOT NULL,
IsHistory BIT CONSTRAINT DF_HistoryResources_IsHistory DEFAULT 1 NOT NULL,
IsDeleted BIT NOT NULL,
RequestMethod VARCHAR (10) NULL,
IsRawResourceMetaSet BIT CONSTRAINT DF_HistoryResources_IsRawResourceMetaSet DEFAULT 0 NOT NULL,
SearchParamHash VARCHAR (64) NULL,
TransactionId BIGINT NULL,
HistoryTransactionId BIGINT NULL,
FileId BIGINT NULL,
OffsetInFile INT NULL CONSTRAINT PKC_HistoryResources_ResourceSurrogateId_ResourceTypeId PRIMARY KEY CLUSTERED (ResourceSurrogateId, ResourceTypeId) WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId),
CONSTRAINT CH_HistoryResources_IsHistory CHECK (IsHistory = 1),
CONSTRAINT U_HistoryResources_ResourceIdInt_Version_ResourceTypeId UNIQUE (ResourceIdInt, Version, ResourceTypeId) WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId)
);

ALTER TABLE dbo.HistoryResources
ADD CONSTRAINT FK_HistoryResources_ResourceIdInt_ResourceTypeId_ResourceIdIntMap FOREIGN KEY (ResourceIdInt, ResourceTypeId) REFERENCES dbo.ResourceIdIntMap (ResourceIdInt, ResourceTypeId);

ALTER TABLE dbo.HistoryResources SET (LOCK_ESCALATION = AUTO);

CREATE INDEX IX_TransactionId_ResourceTypeId_WHERE_TransactionId_NOT_NULL
ON dbo.HistoryResources(TransactionId, ResourceTypeId) WHERE TransactionId IS NOT NULL WITH (DATA_COMPRESSION = PAGE)
ON PartitionScheme_ResourceTypeId (ResourceTypeId);

CREATE INDEX IX_HistoryTransactionId_ResourceTypeId_WHERE_HistoryTransactionId_NOT_NULL
ON dbo.HistoryResources(HistoryTransactionId, ResourceTypeId) WHERE HistoryTransactionId IS NOT NULL WITH (DATA_COMPRESSION = PAGE)
ON PartitionScheme_ResourceTypeId (ResourceTypeId);

CREATE TABLE dbo.ClaimType (
ClaimTypeId TINYINT IDENTITY (1, 1) NOT NULL,
Name VARCHAR (128) COLLATE Latin1_General_100_CS_AS NOT NULL,
Expand Down Expand Up @@ -566,6 +674,9 @@ CREATE TABLE dbo.ResourceReferenceSearchParams (
CONSTRAINT CH_ResourceReferenceSearchParams_IsResourceRef CHECK (IsResourceRef = 1)
);

ALTER TABLE dbo.ResourceReferenceSearchParams
ADD CONSTRAINT FK_ResourceReferenceSearchParams_ReferenceResourceIdInt_ReferenceResourceTypeId_ResourceIdIntMap FOREIGN KEY (ReferenceResourceIdInt, ReferenceResourceTypeId) REFERENCES dbo.ResourceIdIntMap (ResourceIdInt, ResourceTypeId);

ALTER TABLE dbo.ResourceReferenceSearchParams SET (LOCK_ESCALATION = AUTO);

CREATE CLUSTERED INDEX IXC_ResourceSurrogateId_SearchParamId_ResourceTypeId
Expand Down Expand Up @@ -635,108 +746,6 @@ CREATE TABLE dbo.ReindexJob (
CONSTRAINT PKC_ReindexJob PRIMARY KEY CLUSTERED (Id)
);

CREATE TABLE dbo.Resource (
ResourceTypeId SMALLINT NOT NULL,
ResourceSurrogateId BIGINT NOT NULL,
ResourceId VARCHAR (64) COLLATE Latin1_General_100_CS_AS NOT NULL,
Version INT NOT NULL,
IsHistory BIT NOT NULL,
IsDeleted BIT NOT NULL,
RequestMethod VARCHAR (10) NULL,
RawResource VARBINARY (MAX) NULL,
IsRawResourceMetaSet BIT NOT NULL,
SearchParamHash VARCHAR (64) NULL,
TransactionId BIGINT NULL,
HistoryTransactionId BIGINT NULL,
FileId BIGINT NULL,
OffsetInFile INT NULL
);


GO
DROP TABLE dbo.Resource;


GO
CREATE TABLE dbo.ResourceIdIntMap (
ResourceTypeId SMALLINT NOT NULL,
ResourceIdInt BIGINT NOT NULL,
ResourceId VARCHAR (64) COLLATE Latin1_General_100_CS_AS NOT NULL CONSTRAINT PKC_ResourceIdIntMap_ResourceIdInt_ResourceTypeId PRIMARY KEY CLUSTERED (ResourceIdInt, ResourceTypeId) WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId),
CONSTRAINT U_ResourceIdIntMap_ResourceId_ResourceTypeId UNIQUE (ResourceId, ResourceTypeId) WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId)
);

ALTER TABLE dbo.ResourceIdIntMap SET (LOCK_ESCALATION = AUTO);


GO
CREATE TABLE dbo.RawResources (
ResourceTypeId SMALLINT NOT NULL,
ResourceSurrogateId BIGINT NOT NULL,
RawResource VARBINARY (MAX) NULL CONSTRAINT PKC_RawResources_ResourceTypeId_ResourceSurrogateId PRIMARY KEY CLUSTERED (ResourceTypeId, ResourceSurrogateId) WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId)
);

ALTER TABLE dbo.RawResources SET (LOCK_ESCALATION = AUTO);


GO
CREATE TABLE dbo.CurrentResources (
ResourceTypeId SMALLINT NOT NULL,
ResourceSurrogateId BIGINT NOT NULL,
ResourceIdInt BIGINT NOT NULL,
Version INT NOT NULL,
IsHistory BIT CONSTRAINT DF_CurrentResources_IsHistory DEFAULT 0 NOT NULL,
IsDeleted BIT NOT NULL,
RequestMethod VARCHAR (10) NULL,
IsRawResourceMetaSet BIT CONSTRAINT DF_CurrentResources_IsRawResourceMetaSet DEFAULT 0 NOT NULL,
SearchParamHash VARCHAR (64) NULL,
TransactionId BIGINT NULL,
HistoryTransactionId BIGINT NULL,
FileId BIGINT NULL,
OffsetInFile INT NULL CONSTRAINT PKC_CurrentResources_ResourceSurrogateId_ResourceTypeId PRIMARY KEY CLUSTERED (ResourceSurrogateId, ResourceTypeId) WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId),
CONSTRAINT CH_ResourceCurrent_IsHistory CHECK (IsHistory = 0),
CONSTRAINT U_CurrentResources_ResourceIdInt_ResourceTypeId UNIQUE (ResourceIdInt, ResourceTypeId) WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId)
);

ALTER TABLE dbo.CurrentResources SET (LOCK_ESCALATION = AUTO);

CREATE INDEX IX_TransactionId_ResourceTypeId_WHERE_TransactionId_NOT_NULL
ON dbo.CurrentResources(TransactionId, ResourceTypeId) WHERE TransactionId IS NOT NULL WITH (DATA_COMPRESSION = PAGE)
ON PartitionScheme_ResourceTypeId (ResourceTypeId);

CREATE INDEX IX_HistoryTransactionId_ResourceTypeId_WHERE_HistoryTransactionId_NOT_NULL
ON dbo.CurrentResources(HistoryTransactionId, ResourceTypeId) WHERE HistoryTransactionId IS NOT NULL WITH (DATA_COMPRESSION = PAGE)
ON PartitionScheme_ResourceTypeId (ResourceTypeId);


GO
CREATE TABLE dbo.HistoryResources (
ResourceTypeId SMALLINT NOT NULL,
ResourceSurrogateId BIGINT NOT NULL,
ResourceIdInt BIGINT NOT NULL,
Version INT NOT NULL,
IsHistory BIT CONSTRAINT DF_HistoryResources_IsHistory DEFAULT 1 NOT NULL,
IsDeleted BIT NOT NULL,
RequestMethod VARCHAR (10) NULL,
IsRawResourceMetaSet BIT CONSTRAINT DF_HistoryResources_IsRawResourceMetaSet DEFAULT 0 NOT NULL,
SearchParamHash VARCHAR (64) NULL,
TransactionId BIGINT NULL,
HistoryTransactionId BIGINT NULL,
FileId BIGINT NULL,
OffsetInFile INT NULL CONSTRAINT PKC_HistoryResources_ResourceSurrogateId_ResourceTypeId PRIMARY KEY CLUSTERED (ResourceSurrogateId, ResourceTypeId) WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId),
CONSTRAINT CH_HistoryResources_IsHistory CHECK (IsHistory = 1),
CONSTRAINT U_HistoryResources_ResourceIdInt_Version_ResourceTypeId UNIQUE (ResourceIdInt, Version, ResourceTypeId) WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId)
);

ALTER TABLE dbo.HistoryResources SET (LOCK_ESCALATION = AUTO);

CREATE INDEX IX_TransactionId_ResourceTypeId_WHERE_TransactionId_NOT_NULL
ON dbo.HistoryResources(TransactionId, ResourceTypeId) WHERE TransactionId IS NOT NULL WITH (DATA_COMPRESSION = PAGE)
ON PartitionScheme_ResourceTypeId (ResourceTypeId);

CREATE INDEX IX_HistoryTransactionId_ResourceTypeId_WHERE_HistoryTransactionId_NOT_NULL
ON dbo.HistoryResources(HistoryTransactionId, ResourceTypeId) WHERE HistoryTransactionId IS NOT NULL WITH (DATA_COMPRESSION = PAGE)
ON PartitionScheme_ResourceTypeId (ResourceTypeId);

CREATE TABLE dbo.ResourceChangeData (
Id BIGINT IDENTITY (1, 1) NOT NULL,
Timestamp DATETIME2 (7) CONSTRAINT DF_ResourceChangeData_Timestamp DEFAULT sysutcdatetime() NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ CREATE TABLE dbo.CurrentResources
,CONSTRAINT U_CurrentResources_ResourceIdInt_ResourceTypeId UNIQUE (ResourceIdInt, ResourceTypeId) WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId)
)

ALTER TABLE dbo.CurrentResources ADD CONSTRAINT FK_CurrentResources_ResourceIdInt_ResourceTypeId_ResourceIdIntMap FOREIGN KEY (ResourceIdInt, ResourceTypeId) REFERENCES dbo.ResourceIdIntMap (ResourceIdInt, ResourceTypeId)

ALTER TABLE dbo.CurrentResources SET ( LOCK_ESCALATION = AUTO )

CREATE INDEX IX_TransactionId_ResourceTypeId_WHERE_TransactionId_NOT_NULL ON dbo.CurrentResources (TransactionId, ResourceTypeId) WHERE TransactionId IS NOT NULL WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId)
Expand All @@ -86,6 +88,8 @@ CREATE TABLE dbo.HistoryResources
,CONSTRAINT U_HistoryResources_ResourceIdInt_Version_ResourceTypeId UNIQUE (ResourceIdInt, Version, ResourceTypeId) WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId)
)

ALTER TABLE dbo.HistoryResources ADD CONSTRAINT FK_HistoryResources_ResourceIdInt_ResourceTypeId_ResourceIdIntMap FOREIGN KEY (ResourceIdInt, ResourceTypeId) REFERENCES dbo.ResourceIdIntMap (ResourceIdInt, ResourceTypeId)

ALTER TABLE dbo.HistoryResources SET ( LOCK_ESCALATION = AUTO )

CREATE INDEX IX_TransactionId_ResourceTypeId_WHERE_TransactionId_NOT_NULL ON dbo.HistoryResources (TransactionId, ResourceTypeId) WHERE TransactionId IS NOT NULL WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ CREATE TABLE dbo.ResourceReferenceSearchParams
,IsResourceRef bit NOT NULL CONSTRAINT DF_ResourceReferenceSearchParams_IsResourceRef DEFAULT 1, CONSTRAINT CH_ResourceReferenceSearchParams_IsResourceRef CHECK (IsResourceRef = 1)
)

ALTER TABLE dbo.ResourceReferenceSearchParams ADD CONSTRAINT FK_ResourceReferenceSearchParams_ReferenceResourceIdInt_ReferenceResourceTypeId_ResourceIdIntMap FOREIGN KEY (ReferenceResourceIdInt, ReferenceResourceTypeId) REFERENCES dbo.ResourceIdIntMap (ResourceIdInt, ResourceTypeId)

ALTER TABLE dbo.ResourceReferenceSearchParams SET ( LOCK_ESCALATION = AUTO )

CREATE CLUSTERED INDEX IXC_ResourceSurrogateId_SearchParamId_ResourceTypeId
Expand Down

0 comments on commit 61b1c06

Please sign in to comment.