-
Notifications
You must be signed in to change notification settings - Fork 28
Partition token_balances #291
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ CREATE TABLE IF NOT EXISTS token_balances | |
| `insert_timestamp` DateTime DEFAULT now(), | ||
| `is_deleted` UInt8 DEFAULT 0, | ||
|
|
||
| INDEX idx_block_number block_number TYPE minmax GRANULARITY 1, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainAlso add an upgrade migration for the new block_number index. Same issue: CREATE IF NOT EXISTS won’t add idx_block_number to existing tables; add ALTER + MATERIALIZE. ALTER TABLE token_balances ADD INDEX IF NOT EXISTS idx_block_number block_number TYPE minmax GRANULARITY 1;
ALTER TABLE token_balances MATERIALIZE INDEX idx_block_number;Optional: keep an eye on storage overhead; minmax with GRANULARITY 1 is usually cheap and helps when queries filter by block_number without token/owner predicates. 🏁 Script executed: #!/bin/bash
set -euo pipefail
echo "PWD: $(pwd)"
echo
echo "== Show file internal/tools/clickhouse/0008_clickhouse_create_token_balances.sql (if present) =="
if [ -f internal/tools/clickhouse/0008_clickhouse_create_token_balances.sql ]; then
sed -n '1,240p' internal/tools/clickhouse/0008_clickhouse_create_token_balances.sql || true
else
echo "MISSING: internal/tools/clickhouse/0008_clickhouse_create_token_balances.sql"
fi
echo
echo "== Search for idx_block_number / token_balances / MATERIALIZE INDEX / ALTER TABLE occurrences =="
rg -n --hidden --no-ignore -S "idx_block_number|MATERIALIZE INDEX idx_block_number|MATERIALIZE INDEX|ALTER TABLE token_balances|ADD INDEX idx_block_number|ADD INDEX IF NOT EXISTS idx_block_number|token_balances" || true
echo
echo "== List files in internal/tools/clickhouse (if dir exists) =="
if [ -d internal/tools/clickhouse ]; then
ls -1 internal/tools/clickhouse | sed -n '1,200p' || true
else
echo "DIR NOT FOUND: internal/tools/clickhouse"
fiLength of output: 5878 Add ALTER migration to materialize idx_block_number on existing tables CREATE TABLE IF NOT EXISTS does not add new indexes to pre-existing tables — add an ALTER + MATERIALIZE migration for token_balances (and token_transfers if already deployed). Locations:
Suggested SQL: ALTER TABLE token_balances ADD INDEX IF NOT EXISTS idx_block_number block_number TYPE minmax GRANULARITY 1;
ALTER TABLE token_balances MATERIALIZE INDEX idx_block_number;
ALTER TABLE token_transfers ADD INDEX IF NOT EXISTS idx_block_number block_number TYPE minmax GRANULARITY 1;
ALTER TABLE token_transfers MATERIALIZE INDEX idx_block_number;🤖 Prompt for AI Agents |
||
| INDEX idx_block_timestamp block_timestamp TYPE minmax GRANULARITY 1, | ||
| INDEX idx_token_address token_address TYPE bloom_filter GRANULARITY 3, | ||
| INDEX idx_owner_address owner_address TYPE bloom_filter GRANULARITY 3, | ||
|
|
@@ -62,6 +63,6 @@ CREATE TABLE IF NOT EXISTS token_balances | |
| ) | ||
| ) | ||
| ENGINE = ReplacingMergeTree(insert_timestamp, is_deleted) | ||
| PARTITION BY chain_id | ||
| PARTITION BY (chain_id, toStartOfQuarter(block_timestamp)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Partition change won’t apply to existing deployments; add an explicit migration plan. This file uses CREATE TABLE IF NOT EXISTS, so clusters that already have token_balances (partitioned by chain_id) will not be altered. You’ll ship a schema drift where fresh installs get (chain_id, quarter) while existing ones stay on chain_id-only. Recommended migration (outline):
Example SQL (adapt to your deploy tooling and cluster topology): -- 1) Create new table
CREATE TABLE token_balances_v2
(
-- same columns, indexes, projections as current token_balances
) ENGINE = ReplacingMergeTree(insert_timestamp, is_deleted)
PARTITION BY (chain_id, toStartOfQuarter(block_timestamp))
ORDER BY (chain_id, owner_address, token_address, token_id, block_number, transaction_index, log_index, direction)
SETTINGS index_granularity = 8192,
lightweight_mutation_projection_mode = 'rebuild',
deduplicate_merge_projection_mode = 'rebuild',
allow_part_offset_column_in_projections = 1;
-- 2) Backfill
INSERT INTO token_balances_v2 SELECT * FROM token_balances;
-- 3) Swap (atomic)
RENAME TABLE token_balances TO token_balances_old, token_balances_v2 TO token_balances;
-- 4) Resume writes; validate; then drop old
DROP TABLE token_balances_old;If this file is intended only for fresh installs, add a separate numbered migration that performs the above for upgrades to avoid silent divergence. 🤖 Prompt for AI Agents |
||
| ORDER BY (chain_id, owner_address, token_address, token_id, block_number, transaction_index, log_index, direction) | ||
| SETTINGS index_granularity = 8192, lightweight_mutation_projection_mode = 'rebuild', deduplicate_merge_projection_mode = 'rebuild', allow_part_offset_column_in_projections=1; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add an upgrade migration to materialize the new index on existing clusters.
CREATE TABLE IF NOT EXISTS won’t retrofit this index; existing deployments will miss idx_block_number unless you run ALTER + MATERIALIZE.
Suggested numbered migration (use ON CLUSTER if applicable):
🤖 Prompt for AI Agents