From f57981cb2da0be0dbfd5b1d6f8937cb95e4dd55a Mon Sep 17 00:00:00 2001 From: Alexander Diemand Date: Mon, 6 Jul 2026 10:54:49 +0200 Subject: [PATCH 1/2] order of index columns reduces index size Signed-off-by: Alexander Diemand --- cardano-db/src/Cardano/Db/Schema/Core/StakeDelegation.hs | 2 +- schema/migration-2-0029-20240117.sql | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cardano-db/src/Cardano/Db/Schema/Core/StakeDelegation.hs b/cardano-db/src/Cardano/Db/Schema/Core/StakeDelegation.hs index a7f2dc829..5ac9e5eaa 100644 --- a/cardano-db/src/Cardano/Db/Schema/Core/StakeDelegation.hs +++ b/cardano-db/src/Cardano/Db/Schema/Core/StakeDelegation.hs @@ -265,7 +265,7 @@ data EpochStake = EpochStake type instance Key EpochStake = EpochStakeId instance DbInfo EpochStake where - uniqueFields _ = ["addr_id", "pool_id", "epoch_no"] + uniqueFields _ = ["epoch_no", "addr_id", "pool_id"] unnestParamTypes _ = [ ("addr_id", "bigint[]") , ("pool_id", "bigint[]") diff --git a/schema/migration-2-0029-20240117.sql b/schema/migration-2-0029-20240117.sql index 7d3766705..91279d707 100644 --- a/schema/migration-2-0029-20240117.sql +++ b/schema/migration-2-0029-20240117.sql @@ -15,7 +15,7 @@ BEGIN EXECUTE 'ALTER TABLE "instant_reward" DROP COLUMN "earned_epoch"' ; EXECUTE 'ALTER TABLE "instant_reward" ADD COLUMN "earned_epoch" bigint NOT NULL GENERATED ALWAYS AS ((CASE WHEN spendable_epoch >= 1 then spendable_epoch-1 else 0 end)) STORED' ; EXECUTE 'ALTER TABLE "instant_reward" DROP COLUMN "pool_id"' ; - EXECUTE 'ALTER TABLE "instant_reward" ADD CONSTRAINT "unique_instant_reward" UNIQUE("addr_id","earned_epoch","type")' ; + EXECUTE 'ALTER TABLE "instant_reward" ADD CONSTRAINT "unique_instant_reward" UNIQUE("earned_epoch","addr_id","type")' ; EXECUTE 'DELETE FROM "reward" WHERE "pool_id" IS NULL' ; EXECUTE 'ALTER TABLE "reward" ALTER COLUMN "pool_id" SET NOT NULL' ; From a02378633882166fcd4f3b86dc9998a77806789d Mon Sep 17 00:00:00 2001 From: Alexander Diemand Date: Mon, 6 Jul 2026 12:47:42 +0200 Subject: [PATCH 2/2] more indexes; faster autoanalyze for better query planning Signed-off-by: Alexander Diemand --- schema/migration-4-0010-20260704.sql | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 schema/migration-4-0010-20260704.sql diff --git a/schema/migration-4-0010-20260704.sql b/schema/migration-4-0010-20260704.sql new file mode 100644 index 000000000..6053ed73a --- /dev/null +++ b/schema/migration-4-0010-20260704.sql @@ -0,0 +1,10 @@ + +CREATE INDEX IF NOT EXISTS idx_redeemer_tx_id ON redeemer(tx_id); +CREATE INDEX IF NOT EXISTS idx_collateral_tx_out_tx_id ON collateral_tx_out(tx_id); +CREATE INDEX IF NOT EXISTS idx_reference_tx_in_tx_in_id ON reference_tx_in(tx_in_id); +CREATE INDEX IF NOT EXISTS idx_tx_metadata_tx_id_key ON tx_metadata(tx_id, key); +DROP INDEX IF EXISTS idx_tx_metadata_tx_id; + +-- faster autoanalyze on heavy tables: +ALTER TABLE public.block SET (autovacuum_analyze_scale_factor = 0.02, autovacuum_analyze_threshold = 5000); +ALTER TABLE public.tx SET (autovacuum_analyze_scale_factor = 0.02, autovacuum_analyze_threshold = 5000);