Use flattened maps instead of nested maps in UNWIND clause - Amazon Neptune

Use flattened maps instead of nested maps in UNWIND clause

Deep nested structure can restrict the ability of the query engine to generate an optimal query plan. To partially alleviate this issue, the following defined patterns will create optimal plans for the following scenarios:

  • Scenario 1: UNWIND with a list of cypher literals, which includes NUMBER, STRING and BOOLEAN.

  • Scenario 2: UNWIND with a list of flattened maps, which includes only cypher literals (NUMBER, STRING, BOOLEAN) as values.

When writing a query containing UNWIND clause, use the above recommendation to improve performance.

Scenario 1 example:

UNWIND $ids as x MATCH(t:ticket {`~id`: x})

With parameters:

parameters={ "ids": [1, 2, 3] }

An example for Scenario 2 is to generate a list of nodes to CREATE or MERGE. Instead of issuing multiple statements, use the following pattern to define the properties as a set of flattened maps:

UNWIND $props as p CREATE(t:ticket {title: p.title, severity:p.severity})

With parameters:

parameters={ "props": [ {"title": "food poisoning", "severity": "2"}, {"title": "Simone is in office", "severity": "3"} ] }

Instead of nested node objects like:

UNWIND $nodes as n CREATE(t:ticket n.properties)

With parameters:

parameters={ "nodes": [ {"id": "ticket1", "properties": {"title": "food poisoning", "severity": "2"}}, {"id": "ticket2", "properties": {"title": "Simone is in office", "severity": "3"}} ] }