Use indexes to speed up graph queries in PolarDB for PostgreSQL. Three index types are available, each optimized for a different query pattern.
| Index type | Best for |
|---|---|
| ID index | Queries that traverse relationships between nodes, e.g., MATCH ()-[]->() |
| GIN index | Queries that match nodes by label and property value, e.g., MATCH (:Label {property: value}) |
| Property index | Queries that filter results in a WHERE clause, e.g., WHERE o.property = value |
Prerequisites
Before you begin, ensure that you have:
Installed the
ganos_graphextension using a high-privilege account:CREATE EXTENSION IF NOT EXISTS ganos_graph;
If installation fails with ERROR: invalid extension name: "ganos_graph", contact us for assistance.Create an index
ID index
An ID index speeds up queries that traverse relationships between nodes.
When to use
Use an ID index when your queries match relationship patterns across the graph:
MATCH ()-[]->()Syntax
SELECT age_create_id_index(<graph_name>, <is_unique>);Parameters
| Parameter | Description | Required |
|---|---|---|
<graph_name> | The name of the graph. | Yes |
<is_unique> | (Optional) Specifies if the indexed values must be unique. | No |
Example
SELECT age_create_id_index('my_graph');GIN index
A generalized inverted index (GIN) speeds up queries that search for nodes by their properties.
When to use
Use a GIN index when your queries match nodes by a specific label and property value:
MATCH (:Label {property: value})Syntax
SELECT age_create_gin_index('<graph_name>')Parameters
| Parameter | Description | Required |
|---|---|---|
<graph_name> | The name of the graph. | Yes |
Example
SELECT age_create_gin_index('my_graph');Property index
A property index (often a B-tree index) speeds up queries that filter nodes by a property value in a WHERE clause.
When to use
Use a property index when your queries match a pattern and then filter results by a property:
MATCH (o:Label)-[]->()
WHERE o.property = valueSyntax
SELECT age_create_prop_index('<graph_name>','<tag_name>','<property_name>');Parameters
| Parameter | Description | Required |
|---|---|---|
<graph_name> | The name of the graph. | Yes |
<label_name> | The name of the label containing the property. | Yes |
<property_name> | The name of the property to index. | Yes |
Example
SELECT age_create_prop_index('my_graph','my_label', 'name');