Skip to content

Commit

Permalink
feat: Add use_case_id index to generic metrics (#5655)
Browse files Browse the repository at this point in the history
The use_case_id is being sent with every query, and in the cases where a
metric_id is not specified, can be used to very effectively reduce the number
of rows scanned.

The granularity is set a little higher than for the tags hash maps, since the
cardinality of use case ID is quite high and the data should be fairly clumpy
(metric_id should all be stored together and each metric_id has only one use
case ID).
  • Loading branch information
evanh authored Mar 14, 2024
1 parent e3ad616 commit 7ddd864
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions snuba/migrations/group_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ def get_migrations(self) -> Sequence[str]:
"0026_gauges_add_raw_tags_hash_column",
"0027_sets_add_raw_tags_column",
"0028_distributions_add_indexed_tags_column",
"0029_add_use_case_id_index",
]


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from typing import Sequence

from snuba.clusters.storage_sets import StorageSetKey
from snuba.migrations import migration, operations


class Migration(migration.ClickhouseNodeMigration):
blocking = False

def forwards_ops(self) -> Sequence[operations.SqlOperation]:
return [
operations.AddIndex(
storage_set=StorageSetKey.GENERIC_METRICS_COUNTERS,
table_name="generic_metric_counters_aggregated_local",
index_name="set_idx_use_case_id",
index_expression="use_case_id",
index_type="set(0)",
granularity=1,
target=operations.OperationTarget.LOCAL,
),
operations.AddIndex(
storage_set=StorageSetKey.GENERIC_METRICS_SETS,
table_name="generic_metric_sets_local",
index_name="set_idx_use_case_id",
index_expression="use_case_id",
index_type="set(0)",
granularity=1,
target=operations.OperationTarget.LOCAL,
),
operations.AddIndex(
storage_set=StorageSetKey.GENERIC_METRICS_DISTRIBUTIONS,
table_name="generic_metric_distributions_aggregated_local",
index_name="set_idx_use_case_id",
index_expression="use_case_id",
index_type="set(0)",
granularity=1,
target=operations.OperationTarget.LOCAL,
),
operations.AddIndex(
storage_set=StorageSetKey.GENERIC_METRICS_GAUGES,
table_name="generic_metric_gauges_aggregated_local",
index_name="set_idx_use_case_id",
index_expression="use_case_id",
index_type="set(0)",
granularity=1,
target=operations.OperationTarget.LOCAL,
),
]

def backwards_ops(self) -> Sequence[operations.SqlOperation]:
return [
operations.DropIndex(
storage_set=StorageSetKey.GENERIC_METRICS_GAUGES,
table_name="generic_metric_gauges_aggregated_local",
index_name="set_idx_use_case_id",
target=operations.OperationTarget.LOCAL,
),
operations.DropIndex(
storage_set=StorageSetKey.GENERIC_METRICS_DISTRIBUTIONS,
table_name="generic_metric_distributions_aggregated_local",
index_name="set_idx_use_case_id",
target=operations.OperationTarget.LOCAL,
),
operations.DropIndex(
storage_set=StorageSetKey.GENERIC_METRICS_COUNTERS,
table_name="generic_metric_counters_aggregated_local",
index_name="set_idx_use_case_id",
target=operations.OperationTarget.LOCAL,
),
operations.DropIndex(
storage_set=StorageSetKey.GENERIC_METRICS_SETS,
table_name="generic_metric_sets_local",
index_name="set_idx_use_case_id",
target=operations.OperationTarget.LOCAL,
),
]

0 comments on commit 7ddd864

Please sign in to comment.