CREATE INDEX¶
Use CREATE INDEX
to add native indexes for existing tags, edge types or properties.
NOTE: For how to create text-based indexes, see Deploy full-text index.
Most graph queries start the traversal from a list of vertices or edges that are identified by their properties. Indexes make these global retrieval operations efficient on large graphs.
Prerequisites¶
Before you create an index, make sure that the relative tag or edge type is created. For how to create tags or edge types, see CREATE TAG and CREATE EDGE.
Must-read for using index¶
Correct use of indexes can speed up queries, but indexes can dramatically reduce the write performance. The performance reduction can be as much as 90% or even more. DO NOT use indexes in production environments unless you are fully aware of their influences on your service.
If you must use indexes, we suggest that you:
- Import data into Nebula Graph.
- Create indexes.
- Rebuild the indexes.
The preceding workflow minimizes the negative influences of using indexes.
Syntax¶
CREATE {TAG | EDGE} INDEX [IF NOT EXISTS] <index_name> ON {<tag_name> | <edge_name>} ([prop_name_list])
index_name
: The name of the index. It must be unique in a graph space. A recommended way of naming isi_tagName_propName
.
IF NOT EXISTS
: Creating an existent index results in an error. You can use theIF NOT EXISTS
option to conditionally create the index and avoid the error.
-
prop_name_list
:- To index a variable string property, you must use the
prop_name(length)
syntax to specify an index length.NOTE: Long indexes decrease the scan performance of the Storage Service and use more memory. We suggest that you set the indexing length the same as that of the longest string to be indexed. The longest indexing length is 255. Strings longer than 255 are truncated.
- To index a fixed-length string property, you must use the
prop_name
syntax, and the index length is the string length you set. - To index a tag or an edge type, ignore the
prop_name_list
in the parentheses.DON'T: DO NOT index a tag or an edge type if you have indexed any properties in the tag or edge type.
- To index a variable string property, you must use the
Implementation of the operation¶
Nebula Graph implements the creation of the index asynchronously in the next heartbeat cycle. To make sure the creation is successful, take one of the following approaches:
- Find the new index in the result of
SHOW TAG/EDGE INDEXES
. - Wait for two heartbeat cycles, i.e., 20 seconds.
To change the heartbeat interval, modify the heartbeat_interval_secs
parameter in the [configuration files] for all services.
Create tag/edge type indexes¶
The following statement creates an index on the player
tag.
nebula> CREATE TAG INDEX player_index on player();
The following statement creates indexes on the edge type like
.
nebula> CREATE EDGE INDEX like_index on like();
After indexing a tag or an edge type, you can use the LOOKUP
statement to retrieve the VID of all vertices with the tag, or the source vertex ID, destination vertex ID, and ranks of all edges with the edge type. For more information, see List vertices or edges with a tag or an edge type.
Create single-property indexes¶
nebula> CREATE TAG INDEX player_index_0 on player(name(10));
The preceding statement creates an index for the name
property on all vertices carrying the player
tag. This statement creates an index using the first 10 characters of the name property.
nebula> CREATE TAG var_string(p1 string);
nebula> CREATE TAG INDEX var ON var_string(p1(10));
nebula> CREATE TAG fix_string(p1 FIXED_STRING(10));
nebula> CREATE TAG INDEX fix ON fix_string(p1);
nebula> CREATE EDGE INDEX follow_index_0 on follow(degree);
The preceding statement creates an index for the degree
property on all edges carrying the follow
edge type.
Create composite property indexes¶
An index on multiple properties is called a composite index.
NOTE: Creating index across multiple tags is not supported.
Consider the following example:
nebula> CREATE TAG INDEX player_index_1 on player(name(10), age);
This statement creates a composite index for the name
and age
property on all vertices carrying the player
tag.
Using index¶
After the index is created and data is inserted, you can use the LOOKUP statement to query the data.
You do not need to specify which indexes to use in a query, Nebula Graph figures that out by itself.