Query to slow, how to optimize query

What I want to do

Hello, developers.
we have a problem. we run such a query

{
        mfLjOe(func: type(A)) @filter(eq(field1, "value1")){
            b as ~A
        }
        
            nCVefd(func: uid(b)) {
                B @filter(type(B) AND eq(field2, "value2")) {
                    c as ~B
                }
            }
            
    end(func: uid(c)) {
        count(uid)
    }
}

What I did

when data rise to exceed 100 million, the query too slow and always timeout. how can we optimize the query ?

Dgraph metadata

dgraph version

v21.03

the relationship of the data node likes:
image

In the root, switch the type and eq filter, that should at least help optimize the query.

that’s doesn’t work, still always timeout

1 Like
{
        mfLjOe(func: type(A)) @filter(eq(field1, "value1")){
            b as ~A
        }
        
            nCVefd(func: uid(b)) {
                B @filter(type(B) AND eq(field2, "value2")) {
                    c as ~B
                }
            }
            
    end(func: uid(c)) {
        count(uid)
    }
}

Your query looks really absurd. A is a type, but then it’s also being used as a field ? (same applies for B). You can try something as follows instead →

{
  mfLjOe(func: has(~A)) @filter(eq(dgraph.type, "B") and eq(field1, "value1")) {
		b as ~A
	}
  
  nCVefd(func: uid(b)) @filter(eq(field2, "value2")){
    c as ~B
  
  }
             
  end(func: uid(c)) {
        count(uid)
    }
}

Also try indexing your index-able objects (string, date time,etc.) for performance improvement

@kaustubh
thanks, that’s improved a little, but still not enough.
i think the performance bottleneck is at uid() function, it spend too much time.