Skip to content

Commit

Permalink
fix: Shard MeasurementsByContinuationToken index to avoid hotspotting
Browse files Browse the repository at this point in the history
From the [Cloud Spanner documentation on secondary indexes](https://cloud.google.com/spanner/docs/secondary-indexes):

> Be aware that using the commit timestamp column as the first part of the secondary index can create hotspots and reduce write performance.

Sharding is one of the recommendations given for how to avoid hotspotting.
  • Loading branch information
SanjayVas committed Oct 11, 2024
1 parent ede5484 commit 5bfb5b1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/kingdom/spanner/changelog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 5bfb5b1

Please sign in to comment.