问题描述
我有以下包含APOC程序的APOC触发器。这可以按需工作,但是我想知道我是否使用了很多WITH语句,这是可行的方法吗?还是有更好的方法来重构此代码。我仍然是neo4j的新手,所以不确定。
I have the following APOC trigger which contains a apoc procedure. This works as desired but I am wondering if I used a lot of WITH statements, is that the way to go? or is there a better way to refactor this code. I am still new to neo4j so not sure.
控制流程-每当创建注册节点时,它都会创建一个SPerson节点,并根据其大小(SPerson节点的数量)来创建
The control flow- Whenever the Enrollment node is created, it creates an SPerson node and depending on the size(number of SPerson nodes) it creates an enrolled or waitlist(if size>3, and deletes enrolled relation) relation with the course node.
CALL apoc.trigger.add('loadEnrollments',
"UNWIND apoc.trigger.nodesByLabel($assignedLabels, 'Enrollment') AS node
MERGE (p1:SPerson { name: node.name, cell: node.cell, created_at: node.created_at})
WITH p1, node
MATCH (c:Course {name: 'Paradigm Shifting 101'})
WITH node
MATCH (n:SPerson)
WITH node, COUNT(n) as size
CALL apoc.do.when(
size>3,
'MATCH(p1:SPerson),(c:Course)
WHERE p1.name=node.name
CREATE (p1)-[:Waitlist]->(c)
WITH p1,c
MATCH (e:Enrollment) DETACH DELETE e',
'MATCH(p1:SPerson),(c:Course)
WHERE p1.name=node.name
CREATE (p1)-[:Enrolled]->(c)
WITH p1,c
MATCH (e:Enrollment) DETACH DELETE e', {node:node}) YIELD value
DETACH DELETE node",
{ phase: 'after' });
推荐答案
您应该:
- 最小化查询必须处理的数据行数,并且
- 不要不必要地重复操作。
请牢记这些规则,该查询可能对您有效(有效):
With those rules in mind, this query may work (efficiently) for you:
CALL apoc.trigger.add(
'loadEnrollments',
"
CALL apoc.cypher.doIt('MATCH (e:Enrollment) DETACH DELETE e', {}) YIELD value
WITH value
MATCH (n:SPerson)
WITH COUNT(n) as size
MATCH (c:Course {name: 'Paradigm Shifting 101'})
UNWIND apoc.trigger.nodesByLabel($assignedLabels, 'Enrollment') AS node
MERGE (p1:SPerson {name: node.name, cell: node.cell, created_at: node.created_at})
DETACH DELETE node
CALL apoc.create.relationship(p1, CASE WHEN size > 3 THEN 'Waitlist' ELSE 'Enrolled' END, {}, c) YIELD rel
",
{ phase: 'after' }
);
这篇关于Neo4j-关于语法apoc触发器的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!