cardano-graphql icon indicating copy to clipboard operation
cardano-graphql copied to clipboard

Slow _or queries

Open xray-robot opened this issue 4 years ago • 4 comments

Individually it takes milliseconds, but together it takes a few seconds. How can it be solved?

query getTransactions {
  transactions_aggregate(
    where: {
      _or: [
        { outputs: { address: { _in: ["addr1q9acqkgp6ah0xl6dt6gxtrmj8ee4ehnzfeq9rcuwrldalfsl94rky7hgymnt04zzmn696ksga7526ycypga0p0q5scfquhr65g"] } } }
        { inputs: { address: { _in: ["addr1q9acqkgp6ah0xl6dt6gxtrmj8ee4ehnzfeq9rcuwrldalfsl94rky7hgymnt04zzmn696ksga7526ycypga0p0q5scfquhr65g"] } } }
      ]
    }
  ) {
    aggregate {
      count
    }
  }
}
query getTransactions {
  transactions(
    where: {
      _or: [
        { outputs: { address: { _in: ["addr1q9acqkgp6ah0xl6dt6gxtrmj8ee4ehnzfeq9rcuwrldalfsl94rky7hgymnt04zzmn696ksga7526ycypga0p0q5scfquhr65g"]} } }
        { inputs: { address: { _in: ["addr1q9acqkgp6ah0xl6dt6gxtrmj8ee4ehnzfeq9rcuwrldalfsl94rky7hgymnt04zzmn696ksga7526ycypga0p0q5scfquhr65g"] } } }
      ]
    }
  ) {
    fee
    hash
    includedAt
  }
}

xray-robot avatar Oct 27 '21 09:10 xray-robot

I've typically found it's better to break up queries like this and join the results in code.

Using an async (Promise.all in NodeJS) task will allow you to run both queries at the same time instead of waiting for one to finish before running the next query.

CyberCyclone avatar Nov 04 '21 20:11 CyberCyclone

I've typically found it's better to break up queries like this and join the results in code.

Using an async (Promise.all in NodeJS) task will allow you to run both queries at the same time instead of waiting for one to finish before running the next query.

For transaction aggregation it doesn't work, we can't find a proper address tx count because the same address can be in input and output at the same time. I believe that _or should count the intersection of txs arrays.

It also causes some pagination problems for transaction queries. But maybe you have a solution?

xray-robot avatar Nov 05 '21 08:11 xray-robot

I've faced with the same issue. Individually it takes milliseconds, but together it takes a 30+ seconds

mahnunchik avatar Mar 15 '22 17:03 mahnunchik

just came into the same issue, you could try something like:

query transactionsCount($address: String!) {
  outputs: transactions_aggregate(where: {outputs: {address: {_eq: $address}}}) {
    aggregate {
      count
    }
  }
  inputs: transactions_aggregate(where: {inputs: {address: {_eq: $address}}}) {
    aggregate {
      count
    }
  }
  intersect: transactions_aggregate(where: {
    _and: {
      inputs: {address: {_eq: $address}},
      outputs: {address: {_eq: $address}}
    }}) {
    aggregate {
      count
    }
  }
}

totalTx: outputs + inputs - intersect

< 1s vs 20s+ on a simple 7 transaction count address

infnada avatar Jan 05 '23 10:01 infnada