CREATE TEMPORARY GRAPH¶
The CREATE TEMPORARY GRAPH statement creates a temporary graph.
Temporary graphs reside in the memory of the Graph service that creates them. They are lost when that service exits and can be accessed only through sessions connected to that service.
After a temporary graph is created, if the graph type or graph on which it is based is altered or dropped, the temporary graph remains unaffected.
Scope of use¶
- After a temporary graph is created, you can perform
INSERT,SET, andDELETEoperations on it in subsequent queries. - You can create a temporary graph in the following ways:
- Based on an existing graph
- Based on a subgraph
- As an empty graph based on an existing named graph type
- As an empty graph based on an anonymous graph type defined in the statement
Note
To check or compact the memory usage of temporary graphs in NebulaGraph Database Developer Edition, you can call the built-in procedure temporary_graph_compact. For more information, run SHOW PROCEDURE temporary_graph_compact to view the procedure details.
Prerequisites¶
- When creating a temporary graph based on an existing graph, a subgraph, or a named graph type, first create the referenced graph or graph type. For more information, see CREATE GRAPH and CREATE GRAPH TYPE. When using an anonymous graph type, you do not need to create a graph or graph type beforehand.
- To create a temporary graph based on an existing graph or a subgraph, you must be granted to perform the action
MATCH ANY GRAPHon the corresponding schema or the actionMATCHon the graph being referenced. For more information, see Roles and privileges.
- To create a temporary graph based on an anonymous graph type, you must be granted to perform the action
CREATE GRAPH TYPEon the default schema (/default_schema).
Permission rules¶
- The newly created temporary graph is owned by the user executing this statement. The owner of the temporary graph cannot be altered.
- Only the user with the role
ADMINand the owner can perform DQL and DML operations on the temporary graph.
Syntax¶
- create_temporary_graph_statement
create_temporary_graph_statement ::=
'CREATE' ('TEMPORARY'|'TEMP') 'GRAPH' ('IF NOT EXISTS')? temp_graph_name
( ('AS COPY OF' ( (schema_reference '/')? graph_name | 'GRAPH' '{' statement_block '}' ) )
| ('TYPED'? (schema_reference '/')? graph_type_name )
| ('TYPED'? '{' graph_type_specification '}') )
Parameters¶
IF NOT EXISTS¶
This clause checks if a temporary graph with the same name already exists. If it does already exist, the temporary graph is not created and no exception is thrown.
If you do not specify this clause, an exception is thrown if a temporary graph with the same name already exists.
temp_graph_name¶
The name of the temporary graph to be created. temp_graph_name must be a regular identifier that starts with a number sign (#). For more information about identifiers, see Rules for naming identifiers.
schema_reference¶
The path of the referenced schema.
In this statement, the two occurrences of schema_reference specify the schema path of the referenced graph and the schema path of the referenced graph type, respectively.
Both absolute and relative schema paths can be used in the statement. When the schema path is provided, the temporary graph is created based on the referenced graph or the referenced graph type. For information about setting and viewing the schema, see SESSION SET and SHOW CURRENT_SCHEMA.
graph_name¶
The name of the source graph on which the temporary graph is based. This graph must already exist.
When creating a temporary graph based on a graph, the graph type of the graph must contain at least one node type.
When a temporary graph is created from an existing graph, the source graph data is copied into the temporary graph. Later changes to the temporary graph do not affect the source graph.
statement_block¶
A block of one or more statements used to retrieve the subgraph on which the temporary graph is based. The subgraph may be empty, meaning it contains no graph elements, but it must not contain dangling edges. For more information, see Statement block.
graph_type_name¶
The name of the graph type on which the temporary graph is based. The graph type must already exist in the catalog.
Temporary graphs created from graph types are empty graphs. You can populate them later by using INSERT, IMPORT, or similar statements.
graph_type_specification¶
Define node types, edge types, and properties directly inside {...} instead of referencing an existing named graph type. This definition is called an anonymous graph type. You do not need to run CREATE GRAPH TYPE first, and the TYPED keyword is optional.
The rules for defining node types, edge types, and properties are the same as those in CREATE GRAPH TYPE. The temporary graph is initially empty. You can populate it later with INSERT or IMPORT.
Usage notes¶
- After a temporary graph is created, you can declare it as the working graph with the
USEclause and then run queries or DML operations on it.
Caution
Temporary graphs have additional restrictions on property types. If the referenced graph or graph type contains any of the following property types, creating the temporary graph fails:
SETMAPGEOGRAPHYLIST<SET/MAP/GEOGRAPHY>
Examples¶
The examples based on existing graphs or named graph types use the example dataset. The anonymous graph type example does not require this dataset.
Create temporary graphs based on existing graphs¶
Create a temporary graph based on a graph in the current schema¶
CREATE TEMPORARY GRAPH IF NOT EXISTS #movie_temporary_graph AS COPY OF movie
The preceding example creates a temporary graph named #movie_temporary_graph based on the graph movie. Based on this temporary graph, you can perform the following operations:
-
Declare the temporary graph as the working graph and query the number of edges in the graph.
''' USE #movie_temporary_graph MATCH ()-[]->() RETURN count(*) AS num GROUP BY () '''Return:
+-------+ | num | +-------+ | 10175 | +-------+
-
Insert a node into the temporary graph
#movie_temporary_graph.''' USE #movie_temporary_graph INSERT (v@Director{id: 11, name: "Alice"}) '''
-
Update a node in the temporary graph
#movie_temporary_graph.''' USE #movie_temporary_graph MATCH (v@Director{id: 11}) SET v.name = "Bob" '''
-
Delete a node from the temporary graph
#movie_temporary_graph.''' USE #movie_temporary_graph MATCH (v@Director{id: 11}) DELETE v '''
Create a temporary graph in standalone mode based on a graph in the specified schema¶
CREATE TEMPORARY GRAPH IF NOT EXISTS #my_temporary_graph AS COPY OF /default_schema/my_graph
The preceding example creates a temporary graph named #my_temporary_graph based on the graph my_graph created in the Examples section of CREATE GRAPH.
Create a temporary graph based on a subgraph¶
CREATE TEMPORARY GRAPH IF NOT EXISTS #sub_movie_temp AS COPY OF GRAPH { USE movie MATCH (v) RETURN v }
The preceding example creates a temporary graph based on the subgraph returned by the MATCH statement.
Create an empty temporary graph based on a graph type¶
'''
CREATE TEMPORARY GRAPH IF NOT EXISTS #temp_movie TYPED movie_type
'''
Create an empty temporary graph based on an anonymous graph type¶
Run the following statement in the Graph service. You do not need to create a graph type beforehand:
'''
CREATE TEMPORARY GRAPH #person_temp_graph {
NODE person (:Person {id INT64 PRIMARY KEY, name STRING})
}
'''
The example creates an empty temporary graph named #person_temp_graph with a node type named person, the label Person, the primary key property id, and the string property name. You can also add the TYPED keyword before { with the same result.
After creation, run SHOW CREATE GRAPH #person_temp_graph to view the graph structure, or declare the working graph with USE #person_temp_graph and insert data.
Create temporary graphs based on a graph or graph type in the specified schema¶
The following example assumes that /my_schema already exists and that the example dataset has been imported into it.
'''
// Based on a graph in the specified schema
CREATE TEMPORARY GRAPH IF NOT EXISTS #temp_movie AS COPY OF /my_schema/movie
'''
'''
// Based on a graph type in the specified schema
CREATE TEMPORARY GRAPH IF NOT EXISTS #temp_movie TYPED /my_schema/movie_type
'''