Skip to content

Commit 5c0db9d

Browse files
committed
tapdb: universe indices for optimized querying
1 parent e111765 commit 5c0db9d

File tree

6 files changed

+115
-135
lines changed

6 files changed

+115
-135
lines changed

tapdb/migrations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const (
2222
// daemon.
2323
//
2424
// NOTE: This MUST be updated when a new migration is added.
25-
LatestMigrationVersion = 23
25+
LatestMigrationVersion = 24
2626
)
2727

2828
// MigrationTarget is a functional option that can be passed to applyMigrations
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DROP INDEX IF EXISTS idx_mssmt_nodes_stats;
2+
3+
DROP INDEX IF EXISTS idx_universe_leaves_composite;
4+
5+
DROP INDEX IF EXISTS idx_universe_roots_composite;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- Most impactful for query_asset_stats which currently has highest latency
2+
-- Supports the common join pattern and filters on proof_type.
3+
CREATE INDEX IF NOT EXISTS idx_universe_leaves_asset
4+
ON universe_leaves(asset_genesis_id, universe_root_id);
5+
6+
-- Helps with the join conditions we frequently see
7+
-- This is especially useful for query_universe_leaves and improves join efficiency.
8+
CREATE INDEX IF NOT EXISTS idx_mssmt_nodes_composite
9+
ON mssmt_nodes(namespace, key, hash_key, sum);
10+
11+
-- Optimizes the common namespace_root lookups along with proof_type filtering
12+
-- This helps with fetch_universe_root and roots-related queries.
13+
CREATE INDEX IF NOT EXISTS idx_universe_roots_composite
14+
ON universe_roots(namespace_root, proof_type, asset_id);

tapdb/sqlc/querier.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tapdb/sqlc/queries/universe.sql

Lines changed: 47 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ FROM universe_roots
66
JOIN mssmt_roots
77
ON universe_roots.namespace_root = mssmt_roots.namespace
88
JOIN mssmt_nodes
9-
ON mssmt_nodes.hash_key = mssmt_roots.root_hash AND
10-
mssmt_nodes.namespace = mssmt_roots.namespace
9+
ON mssmt_nodes.hash_key = mssmt_roots.root_hash
10+
AND mssmt_nodes.namespace = mssmt_roots.namespace
1111
JOIN genesis_assets
12-
ON genesis_assets.asset_id = universe_roots.asset_id
12+
ON genesis_assets.asset_id = universe_roots.asset_id
1313
WHERE mssmt_nodes.namespace = @namespace;
1414

1515
-- name: UpsertUniverseRoot :one
@@ -30,7 +30,7 @@ WITH root_id AS (
3030
WHERE namespace_root = @namespace_root
3131
)
3232
DELETE FROM universe_events
33-
WHERE universe_root_id = (SELECT id from root_id);
33+
WHERE universe_root_id = (SELECT id FROM root_id);
3434

3535
-- name: DeleteUniverseRoot :exec
3636
DELETE FROM universe_roots
@@ -54,25 +54,23 @@ DELETE FROM universe_leaves
5454
WHERE leaf_node_namespace = @namespace;
5555

5656
-- name: QueryUniverseLeaves :many
57-
SELECT leaves.script_key_bytes, gen.gen_asset_id, nodes.value genesis_proof,
58-
nodes.sum sum_amt, gen.asset_id
59-
FROM universe_leaves leaves
60-
JOIN mssmt_nodes nodes
61-
ON leaves.leaf_node_key = nodes.key AND
62-
leaves.leaf_node_namespace = nodes.namespace
63-
JOIN genesis_info_view gen
57+
SELECT leaves.script_key_bytes, gen.gen_asset_id, nodes.value AS genesis_proof,
58+
nodes.sum AS sum_amt, gen.asset_id
59+
FROM universe_leaves AS leaves
60+
JOIN mssmt_nodes AS nodes
61+
ON leaves.leaf_node_key = nodes.key
62+
AND leaves.leaf_node_namespace = nodes.namespace
63+
JOIN genesis_info_view AS gen
6464
ON leaves.asset_genesis_id = gen.gen_asset_id
6565
WHERE leaves.leaf_node_namespace = @namespace
66-
AND
67-
(leaves.minting_point = sqlc.narg('minting_point_bytes') OR
68-
sqlc.narg('minting_point_bytes') IS NULL)
69-
AND
70-
(leaves.script_key_bytes = sqlc.narg('script_key_bytes') OR
71-
sqlc.narg('script_key_bytes') IS NULL);
66+
AND (leaves.minting_point = sqlc.narg('minting_point_bytes') OR
67+
sqlc.narg('minting_point_bytes') IS NULL)
68+
AND (leaves.script_key_bytes = sqlc.narg('script_key_bytes') OR
69+
sqlc.narg('script_key_bytes') IS NULL);
7270

7371
-- name: FetchUniverseKeys :many
7472
SELECT leaves.minting_point, leaves.script_key_bytes
75-
FROM universe_leaves leaves
73+
FROM universe_leaves AS leaves
7674
WHERE leaves.leaf_node_namespace = @namespace
7775
ORDER BY
7876
CASE WHEN sqlc.narg('sort_direction') = 0 THEN leaves.id END ASC,
@@ -84,14 +82,14 @@ SELECT * FROM universe_leaves;
8482

8583
-- name: UniverseRoots :many
8684
SELECT universe_roots.asset_id, group_key, proof_type,
87-
mssmt_roots.root_hash root_hash, mssmt_nodes.sum root_sum,
88-
genesis_assets.asset_tag asset_name
85+
mssmt_roots.root_hash AS root_hash, mssmt_nodes.sum AS root_sum,
86+
genesis_assets.asset_tag AS asset_name
8987
FROM universe_roots
9088
JOIN mssmt_roots
9189
ON universe_roots.namespace_root = mssmt_roots.namespace
9290
JOIN mssmt_nodes
93-
ON mssmt_nodes.hash_key = mssmt_roots.root_hash AND
94-
mssmt_nodes.namespace = mssmt_roots.namespace
91+
ON mssmt_nodes.hash_key = mssmt_roots.root_hash
92+
AND mssmt_nodes.namespace = mssmt_roots.namespace
9593
JOIN genesis_assets
9694
ON genesis_assets.asset_id = universe_roots.asset_id
9795
ORDER BY
@@ -329,8 +327,9 @@ SELECT
329327
SUM(CASE WHEN event_type = 'SYNC' THEN 1 ELSE 0 END) AS sync_events,
330328
SUM(CASE WHEN event_type = 'NEW_PROOF' THEN 1 ELSE 0 END) AS new_proof_events
331329
FROM universe_events
332-
WHERE event_type IN ('SYNC', 'NEW_PROOF') AND
333-
event_timestamp >= @start_time AND event_timestamp <= @end_time
330+
-- BETWEEN is inclusive for both start and end values.
331+
WHERE event_type IN ('SYNC', 'NEW_PROOF')
332+
AND event_timestamp BETWEEN @start_time AND @end_time
334333
GROUP BY day
335334
ORDER BY day;
336335

@@ -367,7 +366,7 @@ FROM federation_uni_sync_config
367366
ORDER BY group_key NULLS LAST, asset_id NULLS LAST, proof_type;
368367

369368
-- name: UpsertFederationProofSyncLog :one
370-
INSERT INTO federation_proof_sync_log as log (
369+
INSERT INTO federation_proof_sync_log AS log (
371370
status, timestamp, sync_direction, proof_leaf_id, universe_root_id,
372371
servers_id
373372
) VALUES (
@@ -401,66 +400,47 @@ DO UPDATE SET
401400
timestamp = EXCLUDED.timestamp,
402401
-- Increment the attempt counter.
403402
attempt_counter = CASE
404-
WHEN @bump_sync_attempt_counter = true THEN log.attempt_counter + 1
403+
WHEN @bump_sync_attempt_counter = TRUE THEN log.attempt_counter + 1
405404
ELSE log.attempt_counter
406405
END
407406
RETURNING id;
408407

409408
-- name: QueryFederationProofSyncLog :many
410409
SELECT
411410
log.id, status, timestamp, sync_direction, attempt_counter,
412-
413411
-- Select fields from the universe_servers table.
414-
server.id as server_id,
412+
server.id AS server_id,
415413
server.server_host,
416-
417414
-- Select universe leaf related fields.
418-
leaf.minting_point as leaf_minting_point_bytes,
419-
leaf.script_key_bytes as leaf_script_key_bytes,
420-
mssmt_node.value as leaf_genesis_proof,
421-
genesis.gen_asset_id as leaf_gen_asset_id,
422-
genesis.asset_id as leaf_asset_id,
423-
415+
leaf.minting_point AS leaf_minting_point_bytes,
416+
leaf.script_key_bytes AS leaf_script_key_bytes,
417+
mssmt_node.value AS leaf_genesis_proof,
418+
genesis.gen_asset_id AS leaf_gen_asset_id,
419+
genesis.asset_id AS leaf_asset_id,
424420
-- Select fields from the universe_roots table.
425-
root.asset_id as uni_asset_id,
426-
root.group_key as uni_group_key,
427-
root.proof_type as uni_proof_type
428-
429-
FROM federation_proof_sync_log as log
430-
431-
JOIN universe_leaves as leaf
421+
root.asset_id AS uni_asset_id,
422+
root.group_key AS uni_group_key,
423+
root.proof_type AS uni_proof_type
424+
FROM federation_proof_sync_log AS log
425+
JOIN universe_leaves AS leaf
432426
ON leaf.id = log.proof_leaf_id
433-
434427
-- Join on mssmt_nodes to get leaf related fields.
435-
JOIN mssmt_nodes mssmt_node
436-
ON leaf.leaf_node_key = mssmt_node.key AND
437-
leaf.leaf_node_namespace = mssmt_node.namespace
438-
428+
JOIN mssmt_nodes AS mssmt_node
429+
ON leaf.leaf_node_key = mssmt_node.key
430+
AND leaf.leaf_node_namespace = mssmt_node.namespace
439431
-- Join on genesis_info_view to get leaf related fields.
440-
JOIN genesis_info_view genesis
432+
JOIN genesis_info_view AS genesis
441433
ON leaf.asset_genesis_id = genesis.gen_asset_id
442-
443-
JOIN universe_servers as server
434+
JOIN universe_servers AS server
444435
ON server.id = log.servers_id
445-
446-
JOIN universe_roots as root
436+
JOIN universe_roots AS root
447437
ON root.id = log.universe_root_id
448-
449-
WHERE (log.sync_direction = sqlc.narg('sync_direction')
450-
OR sqlc.narg('sync_direction') IS NULL)
451-
AND
452-
(log.status = sqlc.narg('status') OR sqlc.narg('status') IS NULL)
453-
AND
454-
438+
WHERE (log.sync_direction = sqlc.narg('sync_direction') OR sqlc.narg('sync_direction') IS NULL)
439+
AND (log.status = sqlc.narg('status') OR sqlc.narg('status') IS NULL)
455440
-- Universe leaves WHERE clauses.
456-
(leaf.leaf_node_namespace = sqlc.narg('leaf_namespace')
457-
OR sqlc.narg('leaf_namespace') IS NULL)
458-
AND
459-
(leaf.minting_point = sqlc.narg('leaf_minting_point_bytes')
460-
OR sqlc.narg('leaf_minting_point_bytes') IS NULL)
461-
AND
462-
(leaf.script_key_bytes = sqlc.narg('leaf_script_key_bytes')
463-
OR sqlc.narg('leaf_script_key_bytes') IS NULL);
441+
AND (leaf.leaf_node_namespace = sqlc.narg('leaf_namespace') OR sqlc.narg('leaf_namespace') IS NULL)
442+
AND (leaf.minting_point = sqlc.narg('leaf_minting_point_bytes') OR sqlc.narg('leaf_minting_point_bytes') IS NULL)
443+
AND (leaf.script_key_bytes = sqlc.narg('leaf_script_key_bytes') OR sqlc.narg('leaf_script_key_bytes') IS NULL);
464444

465445
-- name: DeleteFederationProofSyncLog :exec
466446
WITH selected_server_id AS (

0 commit comments

Comments
 (0)