Does query variables have extra overhead?

Does query variables have extra overhead or will those queries have the same performance?

{
  q(func: ...) @filter(...) {
    ...
  }
}
{
  matching as var(func: ...)
  qFunc as var(func: uid(matching)) @filter(...)
  q(func: uid(qFunc)) {
    ...
  }
}

As you know I am working on a querybuilder and I believe above way will be easier to implement. I want to “extract” all the variables in separate block and I am wondering if it will harm the query performance.

Hi @zura,

Per this doc, a) query blocks are executed in parallel, and b) you cannot have a defined but unused variable. With those constraints, AFAIK, your approach is good.

1 Like

I am doing almost the same thing!

Hello @anand thanks for reply.

I have read the docs. In this case what does execution in parralel mean? Also as I know if query depends on a var then execution is sequental.

And in this case does dgraph first fetch all matching nodes, then it iterates on them, filters and assigns to qFunc. Then iterates over its nodes and returns the result. - in this case query will be slower since dgraph basically had to find, then filter, then iterate again to fetch the result whereas in first query dgraph would iterate on func results and filter on spot as well as accumulate result(at least thats the way I think it sortof works).

It would be as fast if its sortof transpiled/desugared to same thing or maybe it does filtering and everything in one iteration.

Sorry if my question is stupid. I just have no idea how Dgraph internals work :slight_smile:

1 Like

Yeah its similar. It would be great if Dgraph could optimize those. Maybe somewhere in the future it will. :slight_smile:

From how I understand it. Any blocks that do not depend on another (your 2nd block depends on the first and your 3rd on the second) get run asynchronously. In my example from my topic all of my blocks except for the last get run at the same time parallel (side by side at the same time not waiting for one to finish before running the next). Then my last block runs at as the final run.

1 Like

Yeah I know that. Of course if blocks depend on each other they can’t run in parralel. What I am asking is how that sequental evaluation works in above written case. So does it have to do extra work because of structuring query that way, or are those queries evaluated to almost the same thing?

It seems it is a very bad idea to write query like the second example, since it could hang…

More details in feature request: Optimize query variables based on requested data