cardano-foundation / cardano-graphql

GraphQL API for Cardano
Apache License 2.0
261 stars 103 forks source link

Querying for all assets of a wallet takes a long time #507

Open gglucass opened 3 years ago

gglucass commented 3 years ago

When querying all the assets of a wallet at the end of an epoch, cardano-graphql takes surprisingly long to return the results. On the gimbalabs discord @rcmorano and Giovanni G have confirmed this is most likely due to the heaviness of the query in the background. One would expect that it would be possible to return this data a LOT faster, however, considering that it is possible to get the end-of-epoch lovelace value very quickly with this sql query https://gist.github.com/rcmorano/c9e062def2ff126569c796e89c2844c7#file-cdbs-functions-psql-L43

Does anyone have any suggestions on how it might be possible to speed up this query on cardano-graphql?

Here's the query I've been using to get the assets right now: curl 'https://graphql-api.mainnet.dandelion.link/' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: https://graphql-api.mainnet.dandelion.link' --data-binary '{"query":"# Write your query or mutation here\nquery paymentAddressSummary(\n $addresses: [String!]!\n $atBlock: Int\n) {\n paymentAddresses (addresses: $addresses) {\n summary (atBlock: $atBlock){\n assetBalances {\n asset {\n assetId\n description\n name\n }\n quantity\n }\n }\n }\n}","variables":{"addresses":"addr1q8kpng796nnx2xazx7mknm3w825q0sejz5vah96pyax6g6g7vjzglgwng69rqwsln09juux6pvz2e2y73tcjtzazfdvqug0aq3","atBlock":5758583}}' --compressed

rhyslbw commented 3 years ago

For reference, the query in SDL

query paymentAddressSummary($addresses: [String!]!, $atBlock: Int) {
  paymentAddresses(addresses: $addresses) {
    summary(atBlock: $atBlock) {
      assetBalances {
        asset {
          assetId
          description
          name
        }
        quantity
      }
    }
  }
}
{
  "addresses": ["addr1q8kpng796nnx2xazx7mknm3w825q0sejz5vah96pyax6g6g7vjzglgwng69rqwsln09juux6pvz2e2y73tcjtzazfdvqug0aq3"],
  "atBlock": 5758583
}

The function that resolves this query is complex and then has to compute the values. You may see better performance by running fragments of the underlying queries since you can omit unused fields, depending on your use-case:

https://github.com/input-output-hk/cardano-graphql/blob/058873f7cfa6d5d287f859a58bf511b128da6494/packages/api-cardano-db-hasura/src/HasuraClient.ts#L263-L397