gsql/ Query Operations
Last Updated: October 20, 2018How to define, compile, and execute GSQL queries for high-performance graph analytics.
Query Operations
A GSQL query is a sequence of retrieval and computation statements executed as a single transaction.
1. Creating a Query
Use CREATE QUERY to define a procedure. Queries can accept parameters and return values.
gsqlterminalCREATE QUERY my_query (STRING name, INT age) FOR GRAPH MyGraph { # Body of the query PRINT "Hello " + name; }
Key Keywords:
OR REPLACE: Updates an existing query definition.DISTRIBUTED: Optimizes the query for execution across a cluster.RETURNS: Specifies the data type returned when used as a subquery.
2. AD-HOC vs. Compiled Execution
TigerGraph offers two ways to run queries:
Interpreted Mode (INTERPRET)
Ideal for development and ad-hoc analysis. It runs immediately without compilation.
bashterminalINTERPRET QUERY () FOR GRAPH MyGraph { PRINT "Quick check"; }
Installed Mode (INSTALL + RUN)
Required for production. Compilation optimizes the query for maximum performance and creates a REST endpoint.
bashterminalINSTALL QUERY my_query RUN QUERY my_query("Alice", 30)
3. Dynamic Querying
You can parameterize not just data, but the schema itself. This allows a single query to work on different vertex/edge types or attributes.
gsqlterminalCREATE QUERY dynamic_search (STRING v_type, STRING attr_name) { Start = {v_type}; V = SELECT s FROM Start:s POST-ACCUM s.setAttr(attr_name, "Updated"); }
4. Query Lifecycle Commands
SHOW QUERY queryName: View the source code.DROP QUERY queryName: Remove the query from the catalog.INSTALL QUERY ALL: Compiles all uninstalled queries in one batch.