DELETE when using FOREACH in neo4j

I need to REMOVE a relationship of a specific node type that iterates through FOREACH.

More details:

PROFILE MATCH (n:Label1)-[r1:REL1]-(a:Label2) 
WHERE a.prop1 = 2 
WITH n 
WITH COLLECT(n) AS rows 
WITH [a IN rows WHERE a.prop2 < 1484764200] AS less_than_rows, 
[b IN rows WHERE b.prop2 = 1484764200 AND b.prop3 < 2] AS other_rows 
WITH size(less_than_rows) + size(other_rows) AS count, less_than_rows, other_rows
FOREACH (sub IN less_than_rows | 
  MERGE (sub)-[r:REL2]-(:Label2) 
  DELETE r 
  MERGE(l2:Label2{id:540}) 
  MERGE (sub)-[:APPEND_TO {s:0}]->(l2) 
  SET sub.prop3=1, sub.prop2=1484764200) 
WITH DISTINCT other_rows, count 
FOREACH (sub IN other_rows | 
  MERGE(l2:Label2{id:540}) 
  MERGE (sub)-[:APPEND_TO {s:0}]->(l2) 
  SET sub.prop3=sub.prop3+1)
RETURN count

Since FOREACH does not support MATCH, I used MERGE to achieve it. But I execute it very slowly (it takes about 1 minute). But if I break out with FOREACH (stop updating), it gives about 1 second.

Problem :: Obviously a problem with FOREACH or within operations with FOREACH. I want to delete a specific relation, create another relation and set some properties in node.

Note :: I showed a general query because there is another way to achieve the same requirement (from this FOREACH, I tried CASE WHEN)

+4
source share
2

:

  • MERGE(l2:Label2 {id:540}) FOREACH, . . , , node , MATCH.
  • MERGE (sub)-[:APPEND_TO {s:0}]->(l2) , , , s 0. s 0, . , s (reset to) 0, {s:0} SET s; MERGE, .

( , , ):

PROFILE
MATCH (n:Label1)-[:REL1]-(a:Label2)
WHERE a.prop1 = 2
WITH COLLECT(n) AS rows
WITH
  [a IN rows WHERE a.prop2 < 1484764200] AS less_than_rows, 
  [b IN rows WHERE b.prop2 = 1484764200 AND b.prop3 < 2] AS other_rows 
WITH size(less_than_rows) + size(other_rows) AS count, less_than_rows, other_rows
MERGE(l2:Label2 {id:540}) 
FOREACH (sub IN less_than_rows | 
  MERGE (sub)-[r:REL2]-(:Label2) 
  DELETE r 
  MERGE (sub)-[r2:APPEND_TO]->(l2)
  SET r2.s = 0, sub.prop3 = 1, sub.prop2 = 1484764200) 
WITH DISTINCT l2, other_rows, count 
FOREACH (sub IN other_rows | 
  MERGE (sub)-[r3:APPEND_TO]->(l2) 
  SET r3.s = 0, sub.prop3 = sub.prop3+1)
RETURN count;

s 0 APPEND_TO, SET ON CREATE:

PROFILE
MATCH (n:Label1)-[:REL1]-(a:Label2)
WHERE a.prop1 = 2
WITH COLLECT(n) AS rows
WITH
  [a IN rows WHERE a.prop2 < 1484764200] AS less_than_rows, 
  [b IN rows WHERE b.prop2 = 1484764200 AND b.prop3 < 2] AS other_rows 
WITH size(less_than_rows) + size(other_rows) AS count, less_than_rows, other_rows
MERGE(l2:Label2 {id:540}) 
FOREACH (sub IN less_than_rows | 
  MERGE (sub)-[r:REL2]-(:Label2) 
  DELETE r 
  MERGE (sub)-[r2:APPEND_TO]->(l2)
  ON CREATE SET r2.s = 0
  SET sub.prop3 = 1, sub.prop2 = 1484764200) 
WITH DISTINCT l2, other_rows, count 
FOREACH (sub IN other_rows | 
  MERGE (sub)-[r3:APPEND_TO]->(l2)
  ON CREATE r3.s = 0
  SET sub.prop3 = sub.prop3+1)
RETURN count;
+2

FOREACH UNWIND . OPTIONAL MATCH MERGE, MERGE, . , :

PROFILE 
MATCH (n:Label1)-[:REL1]-(a:Label2) 
WHERE a.prop1 = 2 
WITH COLLECT(n) AS rows 
WITH [a IN rows WHERE a.prop2 < 1484764200] AS less_than_rows, 
[b IN rows WHERE b.prop2 = 1484764200 AND b.prop3 < 2] AS other_rows 
WITH size(less_than_rows) + size(other_rows) AS count, less_than_rows, other_rows
// faster to do it here, only 1 row so it executes once
MERGE(l2:Label2{id:540}) 
UNWIND less_than_rows as sub
OPTIONAL MATCH (sub)-[r:REL2]-(:Label2) 
DELETE r 
MERGE (sub)-[:APPEND_TO {s:0}]->(l2) 
SET sub.prop3=1, sub.prop2=1484764200
WITH DISTINCT other_rows, count, l2
UNWIND other_rows as sub
MERGE (sub)-[:APPEND_TO {s:0}]->(l2) 
SET sub.prop3=sub.prop3+1
RETURN count
0

Source: https://habr.com/ru/post/1667262/


All Articles