# Limit Fetch Table Size in Lookup Commands Using DQL

**URL:** <https://community.dynatrace.com/t/limit-fetch-table-size-in-lookup-commands-using-dql/46070>\
**Category:** DQL\
**Tags:** dql\
**Created:** [June 12, 2024, 8:36am UTC](https://community.dynatrace.com/t/limit-fetch-table-size-in-lookup-commands-using-dql/46070 "2024-06-12T08:36:54Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![r\_weber](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/r_weber/32/24884_2.png) [@r\_weber](https://community.dynatrace.com/u/r_weber)\
**Post date:** [June 12, 2024, 8:36am UTC](https://community.dynatrace.com/t/limit-fetch-table-size-in-lookup-commands-using-dql/46070/1 "2024-06-12T08:36:54Z")

</div>

I’m trying to make a lookup as efficient as possible, thus I’d require to provide a dynamic filter to the “fetch” command within a lookup.

Take this code:

```auto
fetch spans
| filter k8s.namespace.name == "webshop-frontend"
| filter trace.id == toUid("2ece9047533a31d3808c57165e511ad3")

// get service name
| fieldsAdd service.name = entityName(dt.entity.service)

// get the service name of the parent span service
| lookup [fetch spans
           | filter trace.id == toUid("2ece9047533a31d3808c57165e511ad3")
         ],
         sourceField:span.parent_id,
         lookupField:span.id,
         prefix: "parent."

```

The lookup’s purpose is to get the service name of the parent span of a given span.id. (I’m basically trying to build a “service flow” with timing information between services).

Now, as you can see in the code I can do this with individual traces by providing the same trace id in the query and in the lookup fetch. However this will not work without the single trace context.  
What I’d need is to limit the “fetch” within the lookup so that it only returns a small set of spans, namely the ones it is currently operating on.

Something like this:

```auto
| lookup [fetch spans
           | filter span.id == valueOf(sourceField)
         ],
         sourceField:span.parent_id,
         lookupField:span.id,
         prefix: "parent."

```

Isn’t that quite a common usecase? Having access to “outer” variable within the fetch?

kr

---

_[View the full topic](https://community.dynatrace.com/t/limit-fetch-table-size-in-lookup-commands-using-dql/46070)._
