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] Raw resources in the Lake and Resource Id integer map #4652

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5,583 changes: 5,583 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 @@ -92,5 +92,7 @@ public enum SchemaVersion
V80 = 80,
V81 = 81,
V82 = 82,
V83 = 83,
V84 = 84,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace Microsoft.Health.Fhir.SqlServer.Features.Schema
{
public static class SchemaVersionConstants
{
public const int Min = (int)SchemaVersion.V80;
public const int Max = (int)SchemaVersion.V82;
public const int Min = (int)SchemaVersion.V84;
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 @@ -13,3 +13,12 @@ CREATE SEQUENCE dbo.ResourceSurrogateIdUniquifierSequence
CYCLE
CACHE 1000000
GO
CREATE SEQUENCE dbo.ResourceIdIntMapSequence
AS int
START WITH 0
INCREMENT BY 1
MINVALUE 0
MAXVALUE 79999
CYCLE
CACHE 1000000
GO
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ Go

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

Go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
CREATE PROCEDURE dbo.AssignResourceIdInts @Count int, @FirstIdInt bigint OUT
AS
set nocount on
DECLARE @SP varchar(100) = 'AssignResourceIdInts'
,@Mode varchar(200) = 'Cnt='+convert(varchar,@Count)
,@st datetime = getUTCdate()
,@FirstValueVar sql_variant
,@LastValueVar sql_variant
,@SequenceRangeFirstValue int

BEGIN TRY
SET @FirstValueVar = NULL
WHILE @FirstValueVar IS NULL
BEGIN
EXECUTE sys.sp_sequence_get_range @sequence_name = 'dbo.ResourceIdIntMapSequence', @range_size = @Count, @range_first_value = @FirstValueVar OUT, @range_last_value = @LastValueVar OUT
SET @SequenceRangeFirstValue = convert(int,@FirstValueVar)
IF @SequenceRangeFirstValue > convert(int,@LastValueVar)
SET @FirstValueVar = NULL
END

SET @FirstIdInt = datediff_big(millisecond,'0001-01-01',sysUTCdatetime()) * 80000 + @SequenceRangeFirstValue
END TRY
BEGIN CATCH
IF error_number() = 1750 THROW -- Real error is before 1750, cannot trap in SQL.
IF @@trancount > 0 ROLLBACK TRANSACTION
EXECUTE dbo.LogEvent @Process=@SP,@Mode=@Mode,@Status='Error';
THROW
END CATCH
GO
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ BEGIN TRY
END
,MatchedVersion = isnull(D.Version,0)
,MatchedRawResource = D.RawResource
,MatchedTransactionId = D.TransactionId
,MatchedOffsetInFile = D.OffsetInFile
-- ResourceIndex allows to deal with more than one late arrival per resource
FROM (SELECT TOP (@DummyTop) *, ResourceIndex = convert(int,row_number() OVER (PARTITION BY ResourceTypeId, ResourceId ORDER BY ResourceSurrogateId DESC)) FROM @ResourceDateKeys) A
OUTER APPLY (SELECT TOP 1 * FROM dbo.Resource B WITH (INDEX = IX_Resource_ResourceTypeId_ResourceId_Version) WHERE B.ResourceTypeId = A.ResourceTypeId AND B.ResourceId = A.ResourceId AND B.Version > 0 AND B.ResourceSurrogateId < A.ResourceSurrogateId ORDER BY B.ResourceSurrogateId DESC) L -- lower
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,55 +22,67 @@ BEGIN TRY
SELECT B.ResourceTypeId
,B.ResourceId
,ResourceSurrogateId
,B.Version
,C.Version
,IsDeleted
,IsHistory
,RawResource
,IsRawResourceMetaSet
,SearchParamHash
,TransactionId
,OffsetInFile
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.ResourceIdIntMap B WITH (INDEX = U_ResourceIdIntMap_ResourceId_ResourceTypeId) ON B.ResourceTypeId = A.ResourceTypeId AND B.ResourceId = A.ResourceId
JOIN dbo.ResourceTbl C WITH (INDEX = IXU_ResourceIdInt_ResourceId_Version_ResourceTypeId) ON C.ResourceTypeId = A.ResourceTypeId AND C.ResourceIdInt = B.ResourceIdInt AND C.ResourceId = '' AND C.Version = A.Version
OPTION (MAXDOP 1, OPTIMIZE FOR (@DummyTop = 1))
ELSE
SELECT *
FROM (SELECT B.ResourceTypeId
,B.ResourceId
,ResourceSurrogateId
,B.Version
,C.Version
,IsDeleted
,IsHistory
,RawResource
,IsRawResourceMetaSet
,SearchParamHash
,TransactionId
,OffsetInFile
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.ResourceIdIntMap B WITH (INDEX = U_ResourceIdIntMap_ResourceId_ResourceTypeId) ON B.ResourceTypeId = A.ResourceTypeId AND B.ResourceId = A.ResourceId
JOIN dbo.ResourceTbl C WITH (INDEX = IXU_ResourceIdInt_ResourceId_Version_ResourceTypeId) ON C.ResourceTypeId = A.ResourceTypeId AND C.ResourceIdInt = B.ResourceIdInt AND C.ResourceId = '' AND C.Version = A.Version
UNION ALL
SELECT B.ResourceTypeId
,B.ResourceId
,ResourceSurrogateId
,B.Version
,C.Version
,IsDeleted
,IsHistory
,RawResource
,IsRawResourceMetaSet
,SearchParamHash
,TransactionId
,OffsetInFile
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.ResourceIdIntMap B WITH (INDEX = U_ResourceIdIntMap_ResourceId_ResourceTypeId) ON B.ResourceTypeId = A.ResourceTypeId AND B.ResourceId = A.ResourceId
JOIN dbo.ResourceTbl C WITH (INDEX = IXU_ResourceIdInt_ResourceId_ResourceTypeId_INCLUDE_Version_IsDeleted_WHERE_IsHistory_0) ON C.ResourceTypeId = A.ResourceTypeId AND C.ResourceIdInt = B.ResourceIdInt AND C.ResourceId = ''
WHERE IsHistory = 0
) A
OPTION (MAXDOP 1, OPTIMIZE FOR (@DummyTop = 1))
ELSE
SELECT B.ResourceTypeId
,B.ResourceId
,ResourceSurrogateId
,B.Version
,C.Version
,IsDeleted
,IsHistory
,RawResource
,IsRawResourceMetaSet
,SearchParamHash
,TransactionId
,OffsetInFile
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.ResourceIdIntMap B WITH (INDEX = U_ResourceIdIntMap_ResourceId_ResourceTypeId) ON B.ResourceTypeId = A.ResourceTypeId AND B.ResourceId = A.ResourceId
JOIN dbo.ResourceTbl C WITH (INDEX = IXU_ResourceIdInt_ResourceId_ResourceTypeId_INCLUDE_Version_IsDeleted_WHERE_IsHistory_0) ON C.ResourceTypeId = A.ResourceTypeId AND C.ResourceIdInt = B.ResourceIdInt AND C.ResourceId = ''
WHERE IsHistory = 0
OPTION (MAXDOP 1, OPTIMIZE FOR (@DummyTop = 1))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ BEGIN TRY
,IsRawResourceMetaSet
,SearchParamHash
,RequestMethod
,TransactionId
,OffsetInFile
FROM (SELECT TOP (@DummyTop) * FROM @Keys) A
JOIN dbo.Resource B ON ResourceTypeId = TypeId AND ResourceSurrogateId = SurrogateId
WHERE IsHistory = 0 OR @IncludeHistory = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ BEGIN TRY
OPTION (MAXDOP 1, OPTIMIZE FOR (@DummyTop = 1))
END

SELECT ResourceTypeId, ResourceId, Version, IsDeleted, ResourceSurrogateId, RequestMethod, IsMatch = convert(bit,1), IsPartial = convert(bit,0), IsRawResourceMetaSet, SearchParamHash, RawResource
SELECT ResourceTypeId, ResourceId, Version, IsDeleted, ResourceSurrogateId, RequestMethod, IsMatch = convert(bit,1), IsPartial = convert(bit,0), IsRawResourceMetaSet, SearchParamHash, RawResource, TransactionId, OffsetInFile
FROM dbo.Resource
WHERE ResourceTypeId = @ResourceTypeId
AND ResourceSurrogateId BETWEEN @StartId AND @EndId
AND (IsHistory = 0 OR @IncludeHistory = 1)
AND (IsDeleted = 0 OR @IncludeDeleted = 1)
UNION ALL
SELECT ResourceTypeId, ResourceId, Version, IsDeleted, ResourceSurrogateId, RequestMethod, IsMatch = convert(bit,1), IsPartial = convert(bit,0), IsRawResourceMetaSet, SearchParamHash, RawResource
SELECT ResourceTypeId, ResourceId, Version, IsDeleted, ResourceSurrogateId, RequestMethod, IsMatch = convert(bit,1), IsPartial = convert(bit,0), IsRawResourceMetaSet, SearchParamHash, RawResource, TransactionId, OffsetInFile
FROM @SurrogateIds
JOIN dbo.Resource ON ResourceTypeId = @ResourceTypeId AND ResourceSurrogateId = MaxSurrogateId
WHERE IsHistory = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ BEGIN TRY
BEGIN
-- PAGLOCK allows deallocation of empty page without waiting for ghost cleanup
DELETE FROM B FROM @SurrogateIds A INNER LOOP JOIN dbo.ResourceWriteClaim B WITH (INDEX = 1, FORCESEEK, PAGLOCK) ON B.ResourceSurrogateId = A.ResourceSurrogateId OPTION (MAXDOP 1)
DELETE FROM B FROM @SurrogateIds A INNER LOOP JOIN dbo.ReferenceSearchParam B WITH (INDEX = 1, FORCESEEK, PAGLOCK) ON B.ResourceTypeId = @ResourceTypeId AND B.ResourceSurrogateId = A.ResourceSurrogateId OPTION (MAXDOP 1)
DELETE FROM dbo.ReferenceSearchParam WHERE ResourceTypeId = @ResourceTypeId AND ResourceSurrogateId IN (SELECT ResourceSurrogateId FROM @SurrogateIds)
DELETE FROM B FROM @SurrogateIds A INNER LOOP JOIN dbo.TokenSearchParam B WITH (INDEX = 1, FORCESEEK, PAGLOCK) ON B.ResourceTypeId = @ResourceTypeId AND B.ResourceSurrogateId = A.ResourceSurrogateId OPTION (MAXDOP 1)
DELETE FROM B FROM @SurrogateIds A INNER LOOP JOIN dbo.TokenText B WITH (INDEX = 1, FORCESEEK, PAGLOCK) ON B.ResourceTypeId = @ResourceTypeId AND B.ResourceSurrogateId = A.ResourceSurrogateId OPTION (MAXDOP 1)
DELETE FROM B FROM @SurrogateIds A INNER LOOP JOIN dbo.StringSearchParam B WITH (INDEX = 1, FORCESEEK, PAGLOCK) ON B.ResourceTypeId = @ResourceTypeId AND B.ResourceSurrogateId = A.ResourceSurrogateId OPTION (MAXDOP 1)
Expand Down
Loading
Loading