Query optimization to reduce query time and memory usage

Here is a Jaeger trace of on of the queries:

query_trace.json (115.4 KB)