Skip to content

Investigation into DB queries happening during Syncing #1721

@Cmdv

Description

@Cmdv

Description

This is an investigation into the database queries being ran during syncing which is the slowest process in db-sync.
Currently to sync from nothing to the tip of the chain with no data omitted from the configurations. So procedure can take days! (an exact duration hasn't been ascertained as I haven't been able to keep it running in a single go!)

First place of investigation was to use a tool called DbBadger which looks at the output of the postgresql logs and forms a report.

DbBadger Report Overview

Time frame for this report is about 40 minutes of syncing time which was through epoch 352 on mainnet.

SCR-20240604-eaz

The first surprising results was that the high volume of queries were actually SELECT at a whopping 84%

Looking closer at those SELECT queries here is a list of the most prominent offenders:

Number of times executed id query
1,427,906 1 SELECT "stake_address"."id" FROM "stake_address" WHERE "stake_address"."hash_raw" = ?;
774,307 2 SELECT "multi_asset"."id" FROM "multi_asset" WHERE ("multi_asset"."policy" = ?) AND ("multi_asset"."name" = ?);
696,813 3 SELECT "tx"."id", "tx"."hash", "tx"."block_id", "tx"."block_index", "tx"."out_sum", "tx"."fee", "tx"."deposit", "tx"."size", "tx"."invalid_before", "tx"."invalid_hereafter", "tx"."valid_contract", "tx"."script_size" FROM "tx" WHERE "tx"."hash" = ?;
118,598 4 SELECT "script"."id" FROM "script" WHERE "script"."hash" = ?;

Queries in codebase

I'd imagine each query will require it's own PR, and I will add more as we proceed. For now here is a look at 1:

queryStakeAddress
             |--> queryStakeAddrWithCacheRetBs
             |                              |--> queryStakeAddrWithCache
             |                              |                      |--> adjustEpochRewards
             |                              |                      |                   |--> insertBlockLedgerEvents
             |                              |                      |--> deleteReward
             |                              |
             |                              |--> queryRewardAccountWithCacheRetBs
             |                                                      |--> queryOrInsertRewardAccount
             |                                                                           |--> insertWithdrawals
             |                                                                           |--> queryOrInsertStakeAddress
             |
             |--> queryStakeAddrAux
                              |--> queryStakeAddrWithCacheRetBs
                                                          |--> queryStakeAddrWithCache
                                                                                  |--> adjustEpochRewards
                                                                                  |--> deleteReward

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions