# Optimize query variables based on requested data

**URL:** <https://discuss.dgraph.io/t/optimize-query-variables-based-on-requested-data/9316>\
**Category:** Dgraph\
**Tags:** kind:enhancement, kind:feature, area:querylang, area:querylang:vars\
**Created:** [August 9, 2020, 2:06pm UTC](https://discuss.dgraph.io/t/optimize-query-variables-based-on-requested-data/9316 "2020-08-09T14:06:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zura](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/zura/32/3046_2.png) [@zura](https://discuss.dgraph.io/u/zura)\
**Post date:** [August 9, 2020, 2:06pm UTC](https://discuss.dgraph.io/t/optimize-query-variables-based-on-requested-data/9316/1 "2020-08-09T14:06:53Z")

</div>

This query hangs:

```auto
{
  v as var(func: has(name))
  result(func: uid(v), first: 2) {
    uid
  }
}

```

This one doesn’t(obviously):

```auto
{
  result(func: has(name), first: 2) {
    uid
  }
}

```

I guess first query hangs since Dgraph has to find first all nodes that match the func(has name) in the var, and then it returns first 2 results.

It would be great if Dgraph would evaluate vars based on blocks that return the value. So that in this case it would have to find only 2 nodes.

Above written example is just for simplicity but there are real cases when this could be problematic.

For example if we have pagination hence `first` and `offset`/`after` is dynamic(maybe using graphql vars). In such case we wouldn’t be able to share same `func` for multiple queries. For example:

```auto
query test($f1: int, $f2: int, $o1: int, $o2: int) {
  matches as var(func: term(name, 'text'))
  q1(func: uid(matches), first: $f1, offset: $o1) {
    ...
  }
  q2(func: uid(matches), first: $f2, offset: $o2) {
    ...
  }
}

```

Above query will hang if too many nodes are found with term ‘text’.

Right now would have to write:

```auto
query test($f1: int, $f2: int, $o1: int, $o2: int) {
  q1(func: term(name, 'text'), first: $f1, offset: $o1) {
    ...
  }
  q2(func: term(name, 'text'), first: $f2, offset: $o2) {
    ...
  }
}

```

Which is slower than it can be since it has to find nodes with term ‘text’ twice.

---

<div class="post-metadata">

**Author:** ![zura](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/zura/32/3046_2.png) [@zura](https://discuss.dgraph.io/u/zura)\
**Post date:** [August 9, 2020, 2:28pm UTC](https://discuss.dgraph.io/t/optimize-query-variables-based-on-requested-data/9316/2 "2020-08-09T14:28:34Z")

</div>

Same thing is if you have two queries with different filters. Right now reusing func is a bad idea…

---

<div class="post-metadata">

**Author:** ![Neeraj](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/neeraj/32/3104_2.png) [@Neeraj](https://discuss.dgraph.io/u/Neeraj)\
**Post date:** [August 9, 2020, 2:42pm UTC](https://discuss.dgraph.io/t/optimize-query-variables-based-on-requested-data/9316/3 "2020-08-09T14:42:05Z")

</div>

Hey @zura,

That’s a great suggestion.

@pawan are we working on anything similar to this? If not, shall we create a feature requst?

---

<div class="post-metadata">

**Author:** ![pawan](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/pawan/32/1946_2.png) [@pawan](https://discuss.dgraph.io/u/pawan)\
**Post date:** [August 10, 2020, 8:27am UTC](https://discuss.dgraph.io/t/optimize-query-variables-based-on-requested-data/9316/4 "2020-08-10T08:27:51Z")

</div>

This is an interesting idea @zura and can be worked upon as part of improving the query planning. We are not planning to work on this right now but might take it up in the next quarter.
