Looking for Some Best Practices for Optimizing Queries in Dgraph?

Hello

I am relatively new to Dgraph and have been exploring its powerful capabilities for graph based querying. While the documentation is immensely helpful…, I wanted to gather insights from experienced users here.

Could you share your top best practices for writing efficient and optimized queries in Dgraph: ?? Specifically, I would like to understand:

Index Selection :- How do you decide which indexes are most suitable for different use cases: ??
Avoiding Over-fetching :- Tips for structuring queries to retrieve only the necessary data without overloading the system.
Query Debugging :- What tools or techniques do you recommend for diagnosing slow queries: ??
Real-world Scenarios :- Any examples where you improved query performance significantly and what strategies you used: ??

Looking forward to learning from your experiences. Your guidance will not only help me but also serve as a resource for others in the community. I have also read this thread https://discuss.dgraph.io/t/how-to-improve-query-response-time-servicenow but still need some more help.

Thanks in advance !!

With Regards
Marcelo

Assuming you are working in DQL (Dgraph Query Language).

Indexes choice is based on the type of functions you need to use in your queries.
In the function documentation at Functions - Query language
You will see that each searching/filtering function requires an index
For example you can see

Term matching
allofterms
Syntax Example: allofterms(predicate, "space-separated term list")

Schema Types: string

Index Required: term

Matches strings that have all specified terms in any order; case insensitive.

So if your use case is using a search by allofterms on a predicate then you need to set a term index on that predicate.

When executing a DQL query, the result contains an extensions part.

  "extensions": {
    "server_latency": {
      "parsing_ns": 363333,
      "processing_ns": 1339500,
      "encoding_ns": 206125,
      "assign_timestamp_ns": 2395125,
      "total_ns": 4605125
    },
    "txn": {
      "start_ts": 10468
    },
    "metrics": {
      "num_uids": {
        "Category.name": 8,
        "Project.category": 20,
        "Project.id": 6,
        "Project.title": 26,
        "_total": 60,
        "dgraph.type": 0
      }
    }
  }

The metrics part provides the number of predicate ‘visited’.
When you have differentt options to do a query (it’s often the case for complex queries), a first step is to see which query is the most efficient in terms of predicates visited. You can verify the impact on performance with the server_latency info.

The blog post Cracking DQL: Variable Propagation is inspired by a real use case where I helped a customer improving a query by a factor > 10x.
It’s using variable propagation, which is an advanced feature of DQL.

1 Like