Skip to content

Commit

Permalink
indexer-alt: simplify event indexing
Browse files Browse the repository at this point in the history
## Description

Instead of having a separate table for each component of the cascading
index, have a single table, and add multiple indices to it for each of
the cascading cases.

This should reduce the footprint and ingress to the DB, but mildly
increases the risk that the DB picks a bad query plan.

## Test plan

Run all the existing tests, and also run an experiment to confirm that
the DB can successfully plan queries against this kind of schema.

Our initial fear was that if we had multiple indices on a single table,
then the DB may pick the wrong index, and there is still a chance that
might happen if we add indices for disparate filters to the same table
(i.e. we combine the event emit module and event struct instantiation
fields into one table), but we can guarantee that the reader will only
issue one query to each of these merged tables, and it should entirely
overlap with one of its indices.
  • Loading branch information
amnn committed Oct 19, 2024
1 parent bb72581 commit 3b81f17
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 397 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
CREATE TABLE IF NOT EXISTS ev_emit_pkg
(
package BYTEA,
tx_sequence_number BIGINT,
sender BYTEA NOT NULL,
PRIMARY KEY(package, tx_sequence_number)
);

CREATE INDEX IF NOT EXISTS ev_emit_pkg_tx_sequence_number
ON ev_emit_pkg (tx_sequence_number);

CREATE INDEX IF NOT EXISTS ev_emit_pkg_sender
ON ev_emit_pkg (sender, package, tx_sequence_number);

CREATE TABLE IF NOT EXISTS ev_emit_mod
(
package BYTEA,
Expand All @@ -27,50 +13,11 @@ ON ev_emit_mod (tx_sequence_number);
CREATE INDEX IF NOT EXISTS ev_emit_mod_sender
ON ev_emit_mod (sender, package, module, tx_sequence_number);

CREATE TABLE IF NOT EXISTS ev_struct_pkg
(
package BYTEA,
tx_sequence_number BIGINT,
sender BYTEA NOT NULL,
PRIMARY KEY(package, tx_sequence_number)
);

CREATE INDEX IF NOT EXISTS ev_struct_pkg_tx_sequence_number
ON ev_struct_pkg (tx_sequence_number);

CREATE INDEX IF NOT EXISTS ev_struct_pkg_sender
ON ev_struct_pkg (sender, package, tx_sequence_number);

CREATE TABLE IF NOT EXISTS ev_struct_mod
(
package BYTEA,
module TEXT,
tx_sequence_number BIGINT,
sender BYTEA NOT NULL,
PRIMARY KEY(package, module, tx_sequence_number)
);

CREATE INDEX IF NOT EXISTS ev_struct_mod_tx_sequence_number
ON ev_struct_mod (tx_sequence_number);

CREATE INDEX IF NOT EXISTS ev_struct_mod_sender
ON ev_struct_mod (sender, package, module, tx_sequence_number);

CREATE TABLE IF NOT EXISTS ev_struct_name
(
package BYTEA,
module TEXT,
name TEXT,
tx_sequence_number BIGINT,
sender BYTEA NOT NULL,
PRIMARY KEY(package, module, name, tx_sequence_number)
);

CREATE INDEX IF NOT EXISTS ev_struct_name_tx_sequence_number
ON ev_struct_name (tx_sequence_number);
CREATE INDEX IF NOT EXISTS ev_emit_pkg
ON ev_emit_mod (package, tx_sequence_number);

CREATE INDEX IF NOT EXISTS ev_struct_name_sender
ON ev_struct_name (sender, package, module, name, tx_sequence_number);
CREATE INDEX IF NOT EXISTS ev_emit_pkg_sender
ON ev_emit_mod (sender, package, tx_sequence_number);

CREATE TABLE IF NOT EXISTS ev_struct_inst
(
Expand All @@ -81,11 +28,29 @@ CREATE TABLE IF NOT EXISTS ev_struct_inst
instantiation BYTEA,
tx_sequence_number BIGINT,
sender BYTEA NOT NULL,
PRIMARY KEY(package, module, instantiation, tx_sequence_number)
PRIMARY KEY(package, module, name, instantiation, tx_sequence_number)
);

CREATE INDEX IF NOT EXISTS ev_struct_inst_tx_sequence_number
ON ev_struct_inst (tx_sequence_number);

CREATE INDEX IF NOT EXISTS ev_struct_inst_sender
ON ev_struct_inst (sender, package, module, instantiation, tx_sequence_number);
ON ev_struct_inst (sender, package, module, name, instantiation, tx_sequence_number);

CREATE INDEX IF NOT EXISTS ev_struct_name
ON ev_struct_inst (package, module, name, tx_sequence_number);

CREATE INDEX IF NOT EXISTS ev_struct_name_sender
ON ev_struct_inst (sender, package, module, name, tx_sequence_number);

CREATE INDEX IF NOT EXISTS ev_struct_mod
ON ev_struct_inst (package, module, tx_sequence_number);

CREATE INDEX IF NOT EXISTS ev_struct_mod_sender
ON ev_struct_inst (sender, package, module, tx_sequence_number);

CREATE INDEX IF NOT EXISTS ev_struct_pkg
ON ev_struct_inst (package, tx_sequence_number);

CREATE INDEX IF NOT EXISTS ev_struct_pkg_sender
ON ev_struct_inst (sender, package, tx_sequence_number);
59 changes: 0 additions & 59 deletions crates/sui-indexer-alt/src/handlers/ev_emit_pkg.rs

This file was deleted.

60 changes: 0 additions & 60 deletions crates/sui-indexer-alt/src/handlers/ev_struct_mod.rs

This file was deleted.

61 changes: 0 additions & 61 deletions crates/sui-indexer-alt/src/handlers/ev_struct_name.rs

This file was deleted.

59 changes: 0 additions & 59 deletions crates/sui-indexer-alt/src/handlers/ev_struct_pkg.rs

This file was deleted.

4 changes: 0 additions & 4 deletions crates/sui-indexer-alt/src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ use crate::{
};

pub mod ev_emit_mod;
pub mod ev_emit_pkg;
pub mod ev_struct_inst;
pub mod ev_struct_mod;
pub mod ev_struct_name;
pub mod ev_struct_pkg;
pub mod kv_checkpoints;
pub mod kv_objects;
pub mod kv_transactions;
Expand Down
9 changes: 2 additions & 7 deletions crates/sui-indexer-alt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ use clap::Parser;
use sui_indexer_alt::{
args::Args,
handlers::{
ev_emit_mod::EvEmitMod, ev_emit_pkg::EvEmitPkg, ev_struct_inst::EvStructInst,
ev_struct_mod::EvStructMod, ev_struct_name::EvStructName, ev_struct_pkg::EvStructPkg,
kv_checkpoints::KvCheckpoints, kv_objects::KvObjects, kv_transactions::KvTransactions,
ev_emit_mod::EvEmitMod, ev_struct_inst::EvStructInst, kv_checkpoints::KvCheckpoints,
kv_objects::KvObjects, kv_transactions::KvTransactions,
tx_affected_objects::TxAffectedObjects, tx_balance_changes::TxBalanceChanges,
},
Indexer,
Expand All @@ -29,11 +28,7 @@ async fn main() -> Result<()> {
let mut indexer = Indexer::new(args.indexer_config, cancel.clone()).await?;

indexer.pipeline::<EvEmitMod>().await?;
indexer.pipeline::<EvEmitPkg>().await?;
indexer.pipeline::<EvStructInst>().await?;
indexer.pipeline::<EvStructMod>().await?;
indexer.pipeline::<EvStructName>().await?;
indexer.pipeline::<EvStructPkg>().await?;
indexer.pipeline::<KvCheckpoints>().await?;
indexer.pipeline::<KvObjects>().await?;
indexer.pipeline::<KvTransactions>().await?;
Expand Down
Loading

0 comments on commit 3b81f17

Please sign in to comment.