diff --git a/src/main/kotlin/org/wfanet/measurement/kingdom/deploy/gcloud/spanner/queries/StreamMeasurements.kt b/src/main/kotlin/org/wfanet/measurement/kingdom/deploy/gcloud/spanner/queries/StreamMeasurements.kt index 46c04047ecb..7d17067ebf6 100644 --- a/src/main/kotlin/org/wfanet/measurement/kingdom/deploy/gcloud/spanner/queries/StreamMeasurements.kt +++ b/src/main/kotlin/org/wfanet/measurement/kingdom/deploy/gcloud/spanner/queries/StreamMeasurements.kt @@ -206,6 +206,11 @@ class StreamMeasurements( } } + if (filter.hasAfter() || filter.hasUpdatedAfter()) { + // Include shard ID to use sharded index on UpdateTime appropriately. + conjuncts.add("MeasurementIndexShardId != -1") + } + if (conjuncts.isEmpty()) { return } diff --git a/src/main/resources/kingdom/spanner/changelog.yaml b/src/main/resources/kingdom/spanner/changelog.yaml index 924404d65a1..9df52627af1 100644 --- a/src/main/resources/kingdom/spanner/changelog.yaml +++ b/src/main/resources/kingdom/spanner/changelog.yaml @@ -71,4 +71,7 @@ databaseChangeLog: relativeToChangeLogFile: true - include: file: drop-json-columns.sql + relativeToChangeLogFile: true +- include: + file: shard-measurements-by-continuation-token.sql relativeToChangeLogFile: true \ No newline at end of file diff --git a/src/main/resources/kingdom/spanner/shard-measurements-by-continuation-token.sql b/src/main/resources/kingdom/spanner/shard-measurements-by-continuation-token.sql new file mode 100644 index 00000000000..c958f8b1835 --- /dev/null +++ b/src/main/resources/kingdom/spanner/shard-measurements-by-continuation-token.sql @@ -0,0 +1,37 @@ +-- liquibase formatted sql + +-- Copyright 2024 The Cross-Media Measurement Authors +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. + +-- changeset sanjayvas:21 dbms:cloudspanner +-- comment: Shard the MeasurementsByContinuationToken index to avoid hotspotting. + +START BATCH DDL; + +ALTER TABLE Measurements +ADD COLUMN MeasurementIndexShardId INT64 NOT NULL AS ( + MOD(FARM_FINGERPRINT(CAST(MeasurementId AS STRING)), 64) +) STORED; + +DROP INDEX MeasurementsByContinuationToken; + +CREATE INDEX MeasurementsByContinuationToken ON Measurements( + MeasurementIndexShardId, + UpdateTime, + ExternalComputationId, + State +); + +RUN BATCH; +