CREATE INDEX¶
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.
For how to create full-text indexes, see Deploy full-text index.
Must-read for using indexes¶
The concept and using restrictions of indexes are comparatively complex. You can use it together with LOOKUP
and MATCH
statements.
You can use CREATE INDEX
to add native indexes for the existing tags, edge types, or properties. They are usually called as tag indexes, edge type indexes, and property indexes.
- Tag indexes and edge type indexes apply to queries related to the tag and the edge type, but do not apply to queries that are based on certain properties on the tag. For example, you can use
LOOKUP
to retrieve all the vertices with the tagplayer
.
- Property indexes apply to property-based queries. For example, you can use the
age
property to retrieve the VID of all vertices that meetage == 19
.
If a property index i_TA
is created for the property A
of the tag T
, the indexes can be replaced as follows (the same for edge type indexes):
- The query engine can use
i_TA
to replacei_T
.
- In the
MATCH
statement,i_T
cannot replacei_TA
for querying properties.
-
In the
LOOKUP
statement,i_T
may replacei_TA
for querying properties.Legacy version compatibility
In previous releases, the tag or edge type index in the
LOOKUP
statement cannot replace the property index for property queries.
Although the same results can be obtained by using alternative indexes for queries, the query performance varies according to the selected index.
Caution
Indexes can dramatically reduce the write performance. The performance can be greatly reduced. DO NOT use indexes in production environments unless you are fully aware of their influences on your service.
Indexes cannot make queries faster. It can only locate a vertex or an edge according to properties or count the number of vertices or edges.
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 index length is 256 bytes.
If you must use indexes, we suggest that you:
-
Import the data into NebulaGraph.
-
Create indexes.
-
After the index is created and the data is imported, you can use LOOKUP or MATCH to retrieve the data. You do not need to specify which indexes to use in a query, NebulaGraph figures that out by itself.
Note
If you create an index before importing the data, the importing speed will be extremely slow due to the reduction in the write performance.
Keep --disable_auto_compaction = false
during daily incremental writing.
The newly created index will not take effect immediately. Trying to use a newly created index (such as LOOKUP
orREBUILD INDEX
) may fail and return can't find xxx in the space
because the creation is implemented asynchronously. To make sure the follow-up operations work as expected, Wait for two heartbeat cycles, i.e., 20 seconds. To change the heartbeat interval, modify the heartbeat_interval_secs
in the configuration files for all services.
Danger
After creating a new index, or dropping the old index and creating a new one with the same name again, you must REBUILD INDEX
. Otherwise, these data cannot be returned in the MATCH
and LOOKUP
statements.
Syntax¶
CREATE {TAG | EDGE} INDEX [IF NOT EXISTS] <index_name> ON {<tag_name> | <edge_name>} ([<prop_name_list>]) [COMMENT '<comment>'];
Parameter | Description |
---|---|
TAG | EDGE |
Specifies the index type that you want to create. |
IF NOT EXISTS |
Detects if the index that you want to create exists. If it does not exist, a new one will be created. |
<index_name> |
The name of the index. It must be unique in a graph space. A recommended way of naming is i_tagName_propName . The name of the index starts with a letter, supports 1 to 4 bytes UTF-8 encoded characters, such as English letters (case-sensitive), digits, and Chinese characters, but does not support special characters except underscores. To use special characters or reserved keywords as identifiers, quote them with backticks. For more information, see Keywords and reserved words. |
<tag_name> | <edge_name> |
Specifies the name of the tag or edge associated with the index. |
<prop_name_list> |
To index a variable-length string property, you must use prop_name(length) to specify the index length. To index a tag or an edge type, ignore the prop_name_list . |
COMMENT |
The remarks of the index. The maximum length is 256 bytes. By default, there will be no comments on an index. |
Create tag/edge type indexes¶
nebula> CREATE TAG INDEX IF NOT EXISTS player_index on player();
nebula> CREATE EDGE INDEX IF NOT EXISTS follow_index on follow();
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 LOOKUP.
Create single-property indexes¶
nebula> CREATE TAG INDEX IF NOT EXISTS player_index_0 on player(name(10));
The preceding example creates an index for the name
property on all vertices carrying the player
tag. This example creates an index using the first 10 characters of the name
property.
# To index a variable-length string property, you need to specify the index length.
nebula> CREATE TAG IF NOT EXISTS var_string(p1 string);
nebula> CREATE TAG INDEX IF NOT EXISTS var ON var_string(p1(10));
# To index a fixed-length string property, you do not need to specify the index length.
nebula> CREATE TAG IF NOT EXISTS fix_string(p1 FIXED_STRING(10));
nebula> CREATE TAG INDEX IF NOT EXISTS fix ON fix_string(p1);
nebula> CREATE EDGE INDEX IF NOT EXISTS follow_index_0 on follow(degree);
Create composite property indexes¶
An index on multiple properties on a tag (or an edge type) is called a composite property index.
nebula> CREATE TAG INDEX IF NOT EXISTS player_index_1 on player(name(10), age);
Caution
Creating composite property indexes across multiple tags or edge types is not supported.
Note
NebulaGraph follows the left matching principle to select indexes.