From 282a6ea6eb0bba2b0f85386ed2a9864628e8ace3 Mon Sep 17 00:00:00 2001 From: oXtxNt9U <120286271+oXtxNt9U@users.noreply.github.com> Date: Thu, 17 Oct 2024 16:28:23 +0900 Subject: [PATCH] add index, include resigned in ranking order --- .../source/migrations/1697617471901-CreateIndexes.ts | 3 +++ ...9064427168-CreateUpdateValidatorRankingFunction.ts | 11 +++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/api-database/source/migrations/1697617471901-CreateIndexes.ts b/packages/api-database/source/migrations/1697617471901-CreateIndexes.ts index 229d6c21a..65f615c43 100644 --- a/packages/api-database/source/migrations/1697617471901-CreateIndexes.ts +++ b/packages/api-database/source/migrations/1697617471901-CreateIndexes.ts @@ -49,6 +49,8 @@ export class CreateIndexes1697617471901 implements MigrationInterface { CREATE INDEX wallets_balance ON wallets(balance); CREATE INDEX wallets_attributes ON wallets using GIN(attributes); + CREATE INDEX wallets_validators ON wallets ((attributes->>'validatorPublicKey')) + WHERE (attributes ? 'validatorPublicKey'); `); } @@ -98,6 +100,7 @@ export class CreateIndexes1697617471901 implements MigrationInterface { DROP INDEX wallets_balance; DROP INDEX wallets_attributes; + DROP INDEX wallets_validators; `); } } diff --git a/packages/api-database/source/migrations/1729064427168-CreateUpdateValidatorRankingFunction.ts b/packages/api-database/source/migrations/1729064427168-CreateUpdateValidatorRankingFunction.ts index d853529ca..2e094fb97 100644 --- a/packages/api-database/source/migrations/1729064427168-CreateUpdateValidatorRankingFunction.ts +++ b/packages/api-database/source/migrations/1729064427168-CreateUpdateValidatorRankingFunction.ts @@ -8,17 +8,20 @@ export class CreateUpdateValidatorRankingFunction1729064427168 implements Migrat RETURNS VOID AS $$ BEGIN WITH all_validators AS ( - SELECT address, (attributes->>'validatorVoteBalance')::numeric AS vote_balance + SELECT + address, + (attributes->>'validatorVoteBalance')::numeric AS vote_balance, + COALESCE((attributes->>'validatorResigned')::boolean, FALSE) AS is_resigned FROM wallets - WHERE attributes ? 'validatorPublicKey' AND COALESCE((attributes->>'validatorResigned')::boolean, FALSE) IS NOT TRUE + WHERE attributes ? 'validatorPublicKey' ), ranking AS ( SELECT address, vote_balance, - ROW_NUMBER() OVER (ORDER BY vote_balance DESC, address ASC) AS rank + ROW_NUMBER() OVER (ORDER BY is_resigned ASC, vote_balance DESC, address ASC) AS rank FROM all_validators - ORDER BY 2 DESC, 1 ASC + ORDER BY is_resigned ASC, vote_balance DESC, address ASC ) UPDATE wallets SET