Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[do not review] History separation with separated raw resources #4683

Open
wants to merge 89 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
771e5bf
History separation V0
SergeyGaluzo Aug 9, 2023
e501a01
Forgotten resource
SergeyGaluzo Aug 9, 2023
a1a02a3
replace current by view
SergeyGaluzo Aug 10, 2023
2729d78
TRUNCATE
SergeyGaluzo Aug 10, 2023
3b8d3bd
HardDelete
SergeyGaluzo Aug 10, 2023
5477f94
delete history v0
SergeyGaluzo Aug 10, 2023
06e62b8
missed 64 and removed dead code from SQL query generator
SergeyGaluzo Aug 10, 2023
fea417a
Removed history from current in merge
SergeyGaluzo Aug 10, 2023
3c81739
Triggers
SergeyGaluzo Aug 10, 2023
774674f
tests and tran
SergeyGaluzo Aug 10, 2023
67b0e2a
disable/enable indexes
SergeyGaluzo Aug 11, 2023
ac1c875
rollback hard delete and merge
SergeyGaluzo Aug 11, 2023
627cd02
rollback delete invisible history
SergeyGaluzo Aug 11, 2023
b548964
TRUNCATE -> DELETE
SergeyGaluzo Aug 11, 2023
412667c
line
SergeyGaluzo Aug 11, 2023
5a1ce01
leftovers
SergeyGaluzo Aug 11, 2023
a2c117d
PK check
SergeyGaluzo Aug 11, 2023
ca6f5f8
Cosmetic
SergeyGaluzo Aug 11, 2023
a715c4f
WHERE nanes
SergeyGaluzo Aug 11, 2023
c2e962f
Get resources with forced indexes
SergeyGaluzo Aug 11, 2023
7b39c98
Removed redundant where
SergeyGaluzo Aug 11, 2023
5ae529c
Adding feature flag for raw resource dedupping
SergeyGaluzo Aug 11, 2023
65f7bcc
Merge branch 'users/sergal/rawresourcededuppingfeatureflag' into user…
SergeyGaluzo Aug 12, 2023
4a17a26
Merge branch 'main' into users/sergal/historyseparationtriggers
SergeyGaluzo Sep 2, 2023
2366ee9
Enable invisible history by default
SergeyGaluzo Sep 5, 2023
b8e6468
parameters
SergeyGaluzo Sep 5, 2023
7c2a186
commit
SergeyGaluzo Sep 5, 2023
e76f023
fixed typo
SergeyGaluzo Sep 6, 2023
11a8253
merge from invisible history
SergeyGaluzo Sep 6, 2023
1c71865
65 + more realistic diff
SergeyGaluzo Sep 7, 2023
35b9f1c
adjusted update trigger
SergeyGaluzo Sep 7, 2023
463d03b
right trigger
SergeyGaluzo Sep 7, 2023
e06d153
next iteration
SergeyGaluzo Sep 8, 2023
a44c4e0
merge from main
SergeyGaluzo Sep 9, 2023
de7131d
special chars
SergeyGaluzo Sep 9, 2023
5cf2af2
Added delete and update
SergeyGaluzo Sep 12, 2023
6113ddc
\r
SergeyGaluzo Sep 12, 2023
5112de7
Adding script runner
SergeyGaluzo Sep 12, 2023
37bf681
Added verification
SergeyGaluzo Sep 12, 2023
d4d0248
cosmetic
SergeyGaluzo Sep 12, 2023
3aeaa66
HOLDLOCK hint
SergeyGaluzo Sep 13, 2023
8c2ca3a
removed incorrect comments
SergeyGaluzo Sep 13, 2023
af21b5e
RT
SergeyGaluzo Sep 13, 2023
3f841ef
lock timeout
SergeyGaluzo Sep 14, 2023
dbcaf97
180 and remove empty
SergeyGaluzo Sep 14, 2023
b2e0c38
no changes in update resource search params
SergeyGaluzo Sep 15, 2023
6283102
blob rewriter tool
SergeyGaluzo Sep 15, 2023
21525cc
Revert "no changes in update resource search params"
SergeyGaluzo Sep 15, 2023
701cb26
comment
SergeyGaluzo Sep 15, 2023
12d1041
Generic disable indexes and update search params
SergeyGaluzo Sep 15, 2023
0d23a3c
Dummy resources
SergeyGaluzo Sep 15, 2023
c5abfe2
fixes
SergeyGaluzo Sep 17, 2023
ea8a65b
Dummy records based on surr id
SergeyGaluzo Sep 17, 2023
a8d3565
adjust test to filter dummy rows
SergeyGaluzo Sep 17, 2023
a9e8b10
exclude history and current
SergeyGaluzo Sep 17, 2023
b2ce2d2
Adding PerfTest V-1
SergeyGaluzo Sep 18, 2023
cd0d95d
Get asyn wrapper without type string to id function
SergeyGaluzo Sep 20, 2023
d48b63e
tool
SergeyGaluzo Sep 20, 2023
cff9377
not exists on data copy
SergeyGaluzo Sep 21, 2023
e6b3922
packages back
SergeyGaluzo Sep 22, 2023
c74221e
Added comments.
SergeyGaluzo Sep 22, 2023
e8e10ea
Correct filtered index on resource current
SergeyGaluzo Sep 22, 2023
4f83928
Added redundant IsHistory=0 to index
SergeyGaluzo Sep 22, 2023
f63762c
deduping
SergeyGaluzo Sep 22, 2023
3d756e4
Removed dummy records
SergeyGaluzo Sep 23, 2023
02646e3
pp-p
SergeyGaluzo Sep 23, 2023
efedb6b
history clause
SergeyGaluzo Sep 23, 2023
ae3ce94
deduping
SergeyGaluzo Sep 24, 2023
4b2e667
testing parameters
SergeyGaluzo Sep 24, 2023
87e9d6f
get resource by type and surr id range with many versions
SergeyGaluzo Sep 29, 2023
baa603f
skip large databases
SergeyGaluzo Sep 29, 2023
09ba2c8
Added calls to fhir
SergeyGaluzo Oct 1, 2023
aa967ad
added conflict
SergeyGaluzo Oct 2, 2023
4b51a45
max retries = 3
SergeyGaluzo Oct 2, 2023
16223f1
database pings
SergeyGaluzo Oct 2, 2023
5bfe2ec
examples
SergeyGaluzo Oct 2, 2023
483bec8
reverse
SergeyGaluzo Oct 2, 2023
bb3f54e
start closer
SergeyGaluzo Oct 2, 2023
9421a74
put logic
SergeyGaluzo Oct 9, 2023
bf4517c
Create
SergeyGaluzo Oct 11, 2023
6ef7b3b
Move 65 to 84
SergeyGaluzo Oct 2, 2024
9b75a15
Merge from main
SergeyGaluzo Oct 2, 2024
d527016
Fixes after merge
SergeyGaluzo Oct 2, 2024
8253648
Adding RawResources table
SergeyGaluzo Oct 2, 2024
21185b7
change capture tweeks
SergeyGaluzo Oct 3, 2024
91cb772
Test fix
SergeyGaluzo Oct 4, 2024
0494a12
Added MI
SergeyGaluzo Oct 8, 2024
d99ddd9
inline insert
SergeyGaluzo Oct 9, 2024
2a3052a
Repeat on updates
SergeyGaluzo Oct 9, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public void GivenParametersThatShouldNotBeHashed_WhenAdded_ResultsInNoChangeToHa
AssertDoesNotChangeHash(parameters, () =>
{
parameters.AddParameter(1, includeInHash: false);
parameters.AddParameter(VLatest.Resource.ResourceId, "abc", false);
parameters.AddParameter(VLatest.Resource.ResourceId, (object)"123", false);
parameters.AddParameter(VLatest.ResourceCurrent.ResourceId, "abc", false);
parameters.AddParameter(VLatest.ResourceCurrent.ResourceId, (object)"123", false);
});

Assert.False(parameters.HasParametersToHash);
Expand Down

Large diffs are not rendered by default.

5,409 changes: 5,409 additions & 0 deletions src/Microsoft.Health.Fhir.SqlServer/Features/Schema/Migrations/84.sql

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,6 @@ public enum SchemaVersion
V81 = 81,
V82 = 82,
V83 = 83,
V84 = 84,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Microsoft.Health.Fhir.SqlServer.Features.Schema
public static class SchemaVersionConstants
{
public const int Min = (int)SchemaVersion.V80;
public const int Max = (int)SchemaVersion.V83;
public const int Max = (int)SchemaVersion.V84;
public const int MinForUpgrade = (int)SchemaVersion.V80; // this is used for upgrade tests only
public const int SearchParameterStatusSchemaVersion = (int)SchemaVersion.V6;
public const int SupportForReferencesWithMissingTypeVersion = (int)SchemaVersion.V7;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ Go

INSERT INTO dbo.SchemaVersion
VALUES
(83, 'started')
(84, 'started')

Go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ BEGIN TRY
WHERE NOT EXISTS (SELECT * FROM dbo.IndexProperties WHERE TableName = Tbl AND IndexName = Ind)
EXECUTE dbo.LogEvent @Process=@SP,@Mode=@Mode,@Status='Info',@Target='IndexProperties',@Action='Insert',@Rows=@@rowcount

DELETE FROM @Indexes WHERE Tbl = 'Resource' OR IndId = 1
DELETE FROM @Indexes WHERE Tbl IN ('Resource','ResourceCurrent','ResourceHistory') OR IndId = 1
EXECUTE dbo.LogEvent @Process=@SP,@Mode=@Mode,@Status='Info',@Target='@Indexes',@Action='Delete',@Rows=@@rowcount

WHILE EXISTS (SELECT * FROM @Indexes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ BEGIN TRY
,IsRawResourceMetaSet
,SearchParamHash
FROM (SELECT TOP (@DummyTop) * FROM @ResourceKeys) A
JOIN dbo.Resource B WITH (INDEX = IX_Resource_ResourceTypeId_ResourceId_Version) ON B.ResourceTypeId = A.ResourceTypeId AND B.ResourceId = A.ResourceId AND B.Version = A.Version
JOIN dbo.Resource B ON B.ResourceTypeId = A.ResourceTypeId AND B.ResourceId = A.ResourceId AND B.Version = A.Version
OPTION (MAXDOP 1, OPTIMIZE FOR (@DummyTop = 1))
ELSE
SELECT *
Expand All @@ -43,7 +43,7 @@ BEGIN TRY
,IsRawResourceMetaSet
,SearchParamHash
FROM (SELECT TOP (@DummyTop) * FROM @ResourceKeys WHERE Version IS NOT NULL) A
JOIN dbo.Resource B WITH (INDEX = IX_Resource_ResourceTypeId_ResourceId_Version) ON B.ResourceTypeId = A.ResourceTypeId AND B.ResourceId = A.ResourceId AND B.Version = A.Version
JOIN dbo.Resource B ON B.ResourceTypeId = A.ResourceTypeId AND B.ResourceId = A.ResourceId AND B.Version = A.Version
UNION ALL
SELECT B.ResourceTypeId
,B.ResourceId
Expand All @@ -55,7 +55,7 @@ BEGIN TRY
,IsRawResourceMetaSet
,SearchParamHash
FROM (SELECT TOP (@DummyTop) * FROM @ResourceKeys WHERE Version IS NULL) A
JOIN dbo.Resource B WITH (INDEX = IX_Resource_ResourceTypeId_ResourceId) ON B.ResourceTypeId = A.ResourceTypeId AND B.ResourceId = A.ResourceId
JOIN dbo.Resource B ON B.ResourceTypeId = A.ResourceTypeId AND B.ResourceId = A.ResourceId
WHERE IsHistory = 0
) A
OPTION (MAXDOP 1, OPTIMIZE FOR (@DummyTop = 1))
Expand All @@ -70,7 +70,7 @@ BEGIN TRY
,IsRawResourceMetaSet
,SearchParamHash
FROM (SELECT TOP (@DummyTop) * FROM @ResourceKeys) A
JOIN dbo.Resource B WITH (INDEX = IX_Resource_ResourceTypeId_ResourceId) ON B.ResourceTypeId = A.ResourceTypeId AND B.ResourceId = A.ResourceId
JOIN dbo.Resource B ON B.ResourceTypeId = A.ResourceTypeId AND B.ResourceId = A.ResourceId
WHERE IsHistory = 0
OPTION (MAXDOP 1, OPTIMIZE FOR (@DummyTop = 1))

Expand All @@ -83,5 +83,5 @@ BEGIN CATCH
END CATCH
GO
--DECLARE @ResourceKeys dbo.ResourceKeyList
--INSERT INTO @ResourceKeys SELECT TOP 1 ResourceTypeId, ResourceId, NULL FROM Resource
--INSERT INTO @ResourceKeys SELECT 96, newid(), NULL
--EXECUTE dbo.GetResources @ResourceKeys
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ BEGIN CATCH

EXECUTE dbo.LogEvent @Process=@SP,@Mode=@Mode,@Status='Error',@Start=@st;

IF @RaiseExceptionOnConflict = 1 AND error_number() IN (2601, 2627) AND error_message() LIKE '%''dbo.Resource''%'
IF @RaiseExceptionOnConflict = 1 AND error_number() IN (2601, 2627) AND (error_message() LIKE '%''dbo.Resource%' OR error_message() LIKE '%''dbo.RawResources''%') -- handles old and separated tables
THROW 50409, 'Resource has been recently updated or added, please compare the resource content in code for any duplicate updates', 1;
ELSE
THROW
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ BEGIN TRY
BEGIN TRANSACTION

-- Update the search parameter hash value in the main resource table
UPDATE B
SET SearchParamHash = A.SearchParamHash
OUTPUT deleted.ResourceTypeId, deleted.ResourceSurrogateId INTO @Ids
FROM @Resources A JOIN dbo.Resource B ON B.ResourceTypeId = A.ResourceTypeId AND B.ResourceSurrogateId = A.ResourceSurrogateId
WHERE B.IsHistory = 0
-- Avoid join to enable update via view
UPDATE A
SET SearchParamHash = (SELECT SearchParamHash FROM @Resources B WHERE B.ResourceTypeId = A.ResourceTypeId AND B.ResourceSurrogateId = A.ResourceSurrogateId)
FROM dbo.Resource A
WHERE IsHistory = 0
AND EXISTS (SELECT * FROM @Resources B WHERE B.ResourceTypeId = A.ResourceTypeId AND B.ResourceSurrogateId = A.ResourceSurrogateId)
SET @Rows = @@rowcount

-- First, delete all the search params of the resources to reindex.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,104 @@
CREATE TABLE dbo.Resource
CREATE TABLE dbo.RawResources
(
ResourceTypeId smallint NOT NULL,
ResourceId varchar(64) COLLATE Latin1_General_100_CS_AS NOT NULL,
Version int NOT NULL,
IsHistory bit NOT NULL,
ResourceSurrogateId bigint NOT NULL,
IsDeleted bit NOT NULL,
RequestMethod varchar(10) NULL,
RawResource varbinary(max) NOT NULL,
IsRawResourceMetaSet bit NOT NULL DEFAULT 0,
SearchParamHash varchar(64) NULL,
TransactionId bigint NULL, -- used for main CRUD operation
HistoryTransactionId bigint NULL -- used by CRUD operation that moved resource version in invisible state

CONSTRAINT PKC_Resource PRIMARY KEY CLUSTERED (ResourceTypeId, ResourceSurrogateId) WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId(ResourceTypeId),
CONSTRAINT CH_Resource_RawResource_Length CHECK (RawResource > 0x0)
)

ALTER TABLE dbo.Resource SET ( LOCK_ESCALATION = AUTO )

CREATE INDEX IX_ResourceTypeId_TransactionId ON dbo.Resource (ResourceTypeId, TransactionId) WHERE TransactionId IS NOT NULL ON PartitionScheme_ResourceTypeId (ResourceTypeId)
CREATE INDEX IX_ResourceTypeId_HistoryTransactionId ON dbo.Resource (ResourceTypeId, HistoryTransactionId) WHERE HistoryTransactionId IS NOT NULL ON PartitionScheme_ResourceTypeId (ResourceTypeId)
ResourceTypeId smallint NOT NULL
,ResourceSurrogateId bigint NOT NULL
,RawResource varbinary(max) NULL

CREATE UNIQUE NONCLUSTERED INDEX IX_Resource_ResourceTypeId_ResourceId_Version ON dbo.Resource
(
ResourceTypeId,
ResourceId,
Version
CONSTRAINT PKC_RawResources_ResourceTypeId_ResourceSurrogateId PRIMARY KEY CLUSTERED (ResourceTypeId, ResourceSurrogateId) WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId)
)
ON PartitionScheme_ResourceTypeId(ResourceTypeId)

CREATE UNIQUE NONCLUSTERED INDEX IX_Resource_ResourceTypeId_ResourceId ON dbo.Resource
(
ResourceTypeId,
ResourceId
)
INCLUDE -- We want the query in UpsertResource, which is done with UPDLOCK AND HOLDLOCK, to not require a key lookup
ALTER TABLE dbo.RawResources SET ( LOCK_ESCALATION = AUTO )
GO
CREATE TABLE dbo.ResourceCurrent
(
Version,
IsDeleted
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 CONSTRAINT DF_ResourceCurrent_IsHistory DEFAULT 0, CONSTRAINT CH_ResourceCurrent_IsHistory CHECK (IsHistory = 0)
,IsDeleted bit NOT NULL
,RequestMethod varchar(10) NULL
,RawResource varbinary(max) NULL
,IsRawResourceMetaSet bit NOT NULL CONSTRAINT DF_ResourceCurrent_IsRawResourceMetaSet DEFAULT 0
,SearchParamHash varchar(64) NULL
,TransactionId bigint NULL -- used for main CRUD operation
,HistoryTransactionId bigint NULL -- used by CRUD operation that moved resource version in invisible state

CONSTRAINT PKC_ResourceCurrent_ResourceTypeId_ResourceSurrogateId PRIMARY KEY CLUSTERED (ResourceTypeId, ResourceSurrogateId) WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId)
,CONSTRAINT U_ResourceCurrent_ResourceTypeId_ResourceId UNIQUE (ResourceTypeId, ResourceId) WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId)
)
WHERE IsHistory = 0
ON PartitionScheme_ResourceTypeId(ResourceTypeId)

CREATE UNIQUE NONCLUSTERED INDEX IX_Resource_ResourceTypeId_ResourceSurrgateId ON dbo.Resource
ALTER TABLE dbo.ResourceCurrent SET ( LOCK_ESCALATION = AUTO )

-- Strictly speaking, IsHistory=0 filter in the index is not required because table has check constraint.
-- But SQL Server has a bug. For queries having IsHistory=0 in WHERE, SQL knows that only current table should be looked at.
-- But when choosing indexes it forgets about this unless IsHistory=0 is also included in index definition.
-- Without this redundant filtering clause in the index SQL chooses clustered index scan. For large tables it is a difference between running OK and timing out.
--CREATE UNIQUE INDEX IXU_ResourceTypeId_ResourceSurrogateId_WHERE_IsHistory_0_IsDeleted_0 ON dbo.ResourceCurrent (ResourceTypeId, ResourceSurrogateId) WHERE IsHistory = 0 AND IsDeleted = 0 WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId)
CREATE INDEX IX_ResourceTypeId_TransactionId_WHERE_TransactionId_NOT_NULL ON dbo.ResourceCurrent (ResourceTypeId, TransactionId) WHERE TransactionId IS NOT NULL WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId)
CREATE INDEX IX_ResourceTypeId_HistoryTransactionId_WHERE_HistoryTransactionId_NOT_NULL ON dbo.ResourceCurrent (ResourceTypeId, HistoryTransactionId) WHERE HistoryTransactionId IS NOT NULL WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId)
GO
EXECUTE sp_rename 'ResourceCurrent', 'ResourceCurrentTbl'
GO
CREATE VIEW dbo.ResourceCurrent
AS
SELECT A.ResourceTypeId
,A.ResourceSurrogateId
,ResourceId
,Version
,IsHistory
,IsDeleted
,RequestMethod
,B.RawResource
,IsRawResourceMetaSet
,SearchParamHash
,TransactionId
,HistoryTransactionId
FROM dbo.ResourceCurrentTbl A
LEFT OUTER JOIN dbo.RawResources B ON B.ResourceTypeId = A.ResourceTypeId AND B.ResourceSurrogateId = A.ResourceSurrogateId
GO
CREATE TABLE dbo.ResourceHistory
(
ResourceTypeId,
ResourceSurrogateId
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 CONSTRAINT DF_ResourceHistory_IsHistory DEFAULT 1, CONSTRAINT CH_ResourceHistory_IsHistory CHECK (IsHistory = 1)
,IsDeleted bit NOT NULL
,RequestMethod varchar(10) NULL
,RawResource varbinary(max) NULL
,IsRawResourceMetaSet bit NOT NULL CONSTRAINT DF_ResourceHistory_IsRawResourceMetaSet DEFAULT 0
,SearchParamHash varchar(64) NULL
,TransactionId bigint NULL -- used for main CRUD operation
,HistoryTransactionId bigint NULL -- used by CRUD operation that moved resource version in invisible state

CONSTRAINT PKC_ResourceHistory_ResourceTypeId_ResourceSurrogateId PRIMARY KEY CLUSTERED (ResourceTypeId, ResourceSurrogateId) WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId)
,CONSTRAINT U_ResourceHistory_ResourceTypeId_ResourceId_Version UNIQUE (ResourceTypeId, ResourceId, Version) WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId)
)
WHERE IsHistory = 0 AND IsDeleted = 0
ON PartitionScheme_ResourceTypeId(ResourceTypeId)

ALTER TABLE dbo.ResourceHistory SET ( LOCK_ESCALATION = AUTO )

CREATE INDEX IX_ResourceTypeId_TransactionId_WHERE_TransactionId_NOT_NULL ON dbo.ResourceHistory (ResourceTypeId, TransactionId) WHERE TransactionId IS NOT NULL WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId)
CREATE INDEX IX_ResourceTypeId_HistoryTransactionId_WHERE_HistoryTransactionId_NOT_NULL ON dbo.ResourceHistory (ResourceTypeId, HistoryTransactionId) WHERE HistoryTransactionId IS NOT NULL WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId)
GO
EXECUTE sp_rename 'ResourceHistory', 'ResourceHistoryTbl'
GO
CREATE VIEW dbo.ResourceHistory
AS
SELECT A.ResourceTypeId
,A.ResourceSurrogateId
,ResourceId
,Version
,IsHistory
,IsDeleted
,RequestMethod
,B.RawResource
,IsRawResourceMetaSet
,SearchParamHash
,TransactionId
,HistoryTransactionId
FROM dbo.ResourceHistoryTbl A
LEFT OUTER JOIN dbo.RawResources B ON B.ResourceTypeId = A.ResourceTypeId AND B.ResourceSurrogateId = A.ResourceSurrogateId
GO
CREATE TABLE dbo.Dummy (Dummy int)
GO
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
CREATE VIEW dbo.Resource
AS
SELECT ResourceTypeId
,ResourceSurrogateId
,ResourceId
,Version
,IsHistory
,IsDeleted
,RequestMethod
,RawResource
,IsRawResourceMetaSet
,SearchParamHash
,TransactionId
,HistoryTransactionId
FROM dbo.ResourceHistory
UNION ALL
SELECT ResourceTypeId
,ResourceSurrogateId
,ResourceId
,Version
,IsHistory
,IsDeleted
,RequestMethod
,RawResource
,IsRawResourceMetaSet
,SearchParamHash
,TransactionId
,HistoryTransactionId
FROM dbo.ResourceCurrent
GO
CREATE TRIGGER dbo.ResourceIns ON dbo.Resource INSTEAD OF INSERT
AS
BEGIN
INSERT INTO dbo.RawResources
( ResourceTypeId, ResourceSurrogateId, RawResource )
SELECT ResourceTypeId, ResourceSurrogateId, RawResource
FROM Inserted

INSERT INTO dbo.ResourceCurrentTbl
( ResourceTypeId, ResourceSurrogateId, ResourceId, Version, IsDeleted, RequestMethod, IsRawResourceMetaSet, SearchParamHash, TransactionId, HistoryTransactionId )
SELECT ResourceTypeId, ResourceSurrogateId, ResourceId, Version, IsDeleted, RequestMethod, IsRawResourceMetaSet, SearchParamHash, TransactionId, HistoryTransactionId
FROM Inserted
WHERE IsHistory = 0

INSERT INTO dbo.ResourceHistoryTbl
( ResourceTypeId, ResourceSurrogateId, ResourceId, Version, IsDeleted, RequestMethod, IsRawResourceMetaSet, SearchParamHash, TransactionId, HistoryTransactionId )
SELECT ResourceTypeId, ResourceSurrogateId, ResourceId, Version, IsDeleted, RequestMethod, IsRawResourceMetaSet, SearchParamHash, TransactionId, HistoryTransactionId
FROM Inserted
WHERE IsHistory = 1
END
GO
CREATE TRIGGER dbo.ResourceUpd ON dbo.Resource INSTEAD OF UPDATE
AS
BEGIN
IF UPDATE(IsDeleted) AND UPDATE(RawResource) AND UPDATE(SearchParamHash) AND UPDATE(HistoryTransactionId) AND NOT UPDATE(IsHistory) -- HardDeleteResource
BEGIN
UPDATE B
SET RawResource = A.RawResource
FROM Inserted A
JOIN dbo.RawResources B ON B.ResourceTypeId = A.ResourceTypeId AND B.ResourceSurrogateId = A.ResourceSurrogateId

UPDATE B
SET IsDeleted = A.IsDeleted
,SearchParamHash = A.SearchParamHash
,HistoryTransactionId = A.HistoryTransactionId
FROM Inserted A
JOIN dbo.ResourceCurrentTbl B ON B.ResourceTypeId = A.ResourceTypeId AND B.ResourceSurrogateId = A.ResourceSurrogateId

RETURN
END

IF UPDATE(SearchParamHash) AND NOT UPDATE(IsHistory)
BEGIN
UPDATE B
SET SearchParamHash = A.SearchParamHash
FROM Inserted A
JOIN dbo.ResourceCurrentTbl B ON B.ResourceTypeId = A.ResourceTypeId AND B.ResourceSurrogateId = A.ResourceSurrogateId
WHERE A.IsHistory = 0

RETURN
END

IF UPDATE(RawResource) -- invisible records
UPDATE B
SET RawResource = A.RawResource
FROM Inserted A
JOIN dbo.RawResources B ON B.ResourceTypeId = A.ResourceTypeId AND B.ResourceSurrogateId = A.ResourceSurrogateId

IF NOT UPDATE(IsHistory)
RAISERROR('Generic updates are not supported via Resource view',18,127)

DELETE FROM A
FROM dbo.ResourceCurrentTbl A
WHERE EXISTS (SELECT * FROM Inserted B WHERE B.ResourceTypeId = A.ResourceTypeId AND B.ResourceSurrogateId = A.ResourceSurrogateId AND B.IsHistory = 1)

INSERT INTO dbo.ResourceHistoryTbl
( ResourceTypeId, ResourceSurrogateId, ResourceId, Version, IsDeleted, RequestMethod, IsRawResourceMetaSet, SearchParamHash, TransactionId, HistoryTransactionId )
SELECT ResourceTypeId, ResourceSurrogateId, ResourceId, Version, IsDeleted, RequestMethod, IsRawResourceMetaSet, SearchParamHash, TransactionId, HistoryTransactionId
FROM Inserted
WHERE IsHistory = 1
END
GO
CREATE TRIGGER dbo.ResourceDel ON dbo.Resource INSTEAD OF DELETE
AS
BEGIN
DELETE FROM A
FROM dbo.ResourceCurrentTbl A
WHERE EXISTS (SELECT * FROM Deleted B WHERE B.ResourceTypeId = A.ResourceTypeId AND B.ResourceSurrogateId = A.ResourceSurrogateId AND B.IsHistory = 0)

DELETE FROM A
FROM dbo.ResourceHistoryTbl A
WHERE EXISTS (SELECT * FROM Deleted B WHERE B.ResourceTypeId = A.ResourceTypeId AND B.ResourceSurrogateId = A.ResourceSurrogateId AND B.IsHistory = 1)

DELETE FROM A
FROM dbo.RawResources A
WHERE EXISTS (SELECT * FROM Deleted B WHERE B.ResourceTypeId = A.ResourceTypeId AND B.ResourceSurrogateId = A.ResourceSurrogateId)
AND NOT EXISTS (SELECT * FROM Resource B WHERE B.ResourceTypeId = A.ResourceTypeId AND B.ResourceSurrogateId = A.ResourceSurrogateId)
END
GO
Loading
Loading