Skip to content

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 tag player.
  • Property indexes apply to property-based queries. For example, you can use the age property to retrieve the VID of all vertices that meet age == 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 replace i_T.
  • In the MATCH statement, i_T cannot replace i_TA for querying properties.
  • In the LOOKUP statement, i_T may replace i_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 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.

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 255 bytes. Strings longer than 255 bytes will be truncated.

If you must use indexes, we suggest that you:

  1. Import the data into Nebula Graph.

  2. Create indexes.

  3. Rebuild indexes.

  4. 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, Nebula Graph 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. Nebula Graph implements the creation in the next heartbeat cycle. To make sure the creation is successful, take one of the following approaches:

  • 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 is case-sensitive and allows letters, numbers, or underlines. Keywords and reserved words are not allowed.
<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 player_index on player();
nebula> CREATE EDGE INDEX 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 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 var_string(p1 string);
nebula> CREATE TAG INDEX 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 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);

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 player_index_1 on player(name(10), age);

Caution

Creating composite property indexes across multiple tags or edge types is not supported.

Nebula Graph follows the left matching principle to select indexes when composite property indexes are used in LOOKUP or MATCH statements. That is, columns in the WHERE conditions must be in the first N columns of the index. For example:

# This example creates a composite property index for the first 3 properties of tag t.
nebula> CREATE TAG INDEX example_index ON t(p1, p2, p3);

# Note: The index match is not successful because it does not start from p1.
nebula> LOOKUP ON t WHERE p2 == 1 and p3 == 1; 

# The index match is successful.
nebula> LOOKUP ON t WHERE p1 == 1;  
# The index match is successful because p1 and p2 are consecutive.
nebula> LOOKUP ON t WHERE p1 == 1 and p2 == 1;  
# The index match is successful because p1, p2, and p3 are consecutive.
nebula> LOOKUP ON t WHERE p1 == 1 and p2 == 1 and p3 == 1; 

Last update: August 30, 2021
Back to top