Performance optimization
This topic describes how to optimize the performance of Graph Database (GDB). You can use these methods to quickly identify performance issues and improve query efficiency.
Set the query scope
The query and analysis engine in GDB provides query methods based on data entry. When you query a large volume of data, you can specify a search scope. This scope can include a specific label, a start and end range for a property, or the number of iterations.
Example:
Recommended: Limit the filtered property to a range between 10 and 30.
g.V().hasLabel("person").has("age",P.gt(10).and(lt(30))).limit(5)Standard method
g.V().has("age",P.gt(10))
Query the shortest path
GDB uses the Depth-First Search (DFS) policy to find the shortest path. If your graph data is highly connected, you can add conditions to your shortest path query.
Example:
Recommended: Limit the maximum depth of the shortest path.
g.V($startV).repeat(both().simplePath()).until(hasId($endV).or().loops().is(gt($depth))).hasId($endV).path()Standard method
g.V($startV).repeat(both().simplePath()).until(hasId($endV))
To run shortest path queries based on weight, you can sort the results by edge weight.
Example:
g.V(fromVertexId) .repeat(outE().inV().simplePath()) .until(hasId(toVertexId).or().loops().is(gt(deepLimit))) .hasId(toVertexId).path().as('p') .map(unfold().coalesce(values('weight'),constant(0.0)).sum()) .as('cost').select('cost','p').order().by(select("cost"),Order.incr)
Avoid super vertices
GDB has an auto-index mechanism and a powerful statistical analysis engine to optimize execution. However, you should still use rules to avoid super vertices in your application.
Merged statements for query and modification
To modify a property during a Gremlin query, you can use a merged statement.
Example:
g.V("test").property("nums", union(values("nums"), constant(1)).sum())Analyze query timeouts
If your scenario is complex and queries frequently time out, you can use the profile() statement to analyze the performance of each step. This analysis helps you understand the data distribution involved in the query execution. You can identify time-consuming operations, such as queries on super vertices, random queries on large amounts of data, and property filtering.
Sample query request:
g.V().out().limit(10).profile()Sample response:
==>Traversal Metrics
Step Count Traverses Time (ms) % Dur
=============================================================================================================
GraphDbGraphStep(vertex,[]) 29 29 1.657 77.43
VertexStep(OUT,vertex) 11 11 0.410 19.18
RangeGlobalStep(0,10) 10 10 0.072 3.39
>TOTAL - - 2.140 -The TraversalMetrics object returned by the profile() command contains the following information:
Step: The step being analyzed in the current traversal. For more information, see Gremlin (Chinese) or .Count: The number of traversers that pass through this step.Traverses: The number of deduplicated traversals that are filtered by this step.Time (ms): The total time spent executing this step, in milliseconds.% Dur: The percentage of the total time spent on this step.
DMS does not support the profile() command. To use this command, you can connect to your instance using the Gremlin Console. For more information, see Connect to an instance using the Gremlin Console.